WINDOWS SOCKET programming example
Important points:
Server side
1. Load socket library;
2. Set up socket;
3. Server address filling;
4. Bind the socket and address of the server;
5. Enable monitoring;
6.accept waits for the connection to obtain the client socket;
Bottom line: bind server address to server socket, ready to accept client connection.
Client
1. Load socket library;
2. Establish client socket;
3. Fill in the sever address; / / note that there is no wrong writing here, or you need to know the server address
4. Actively connect to the server (client and server addresses are required);
Bottom line: client socket requests connection from server address.
Code
#include<WinSock2.h> #include<stdio.h> #pragma comment(lib, "ws2_32.lib") int main(){ WSADATA wsaData; WSAStartup(MAKEWORD(2,2),&wsaData); if(LOBYTE(wsaData.wVersion)!=2 || HIBYTE(wsaData.wVersion)!=2) { printf("Request version failed"); return -1; } SOCKET serverSocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); if(INVALID_SOCKET == serverSocket) { printf("Socket creation failed"); return -1; } SOCKADDR_IN serverAddr = {0}; //Initialization protocol address serverAddr.sin_family = AF_INET;//Must be consistent with the IP protocol for socket creation serverAddr.sin_port = htons(8080); //Server port, through which clients connect //ip address is a string in dotted format, which is used to convert integers with INET addr serverAddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1"); if(SOCKET_ERROR == bind(serverSocket , (SOCKADDR*)&serverAddr , sizeof(serverAddr))) { printf("Binding failed"); closesocket(serverSocket); //Release socket WSACleanup(); //Close socket return -1; } printf("Binding success"); if(SOCKET_ERROR == listen(serverSocket, 10)) //10 indicates the maximum listening queue size { printf("Monitoring failed"); closesocket(serverSocket); //Release socket WSACleanup(); //Close socket return -1; } printf("Monitoring success"); SOCKADDR_IN clientAddr = {0}; int len = sizeof(clientAddr); SOCKET clientSocket = accept(serverSocket,(SOCKADDR*)&clientAddr, &len); //Blocking function. If the connection is successful, clientAddr will be filled by the client address, and a clientSocket will be returned for receiving and sending if(clientSocket == INVALID_SOCKET) { printf("connection failed"); closesocket(serverSocket); //Release socket WSACleanup(); //Close socket return -9; } printf("Successful connection"); printf("Client(%s)\n" , inet_ntoa(clientAddr.sin_addr)); char recvBuff[128] = {}; while(1) { memset(recvBuff, 0 , sizeof(recvBuff)); if(recv(clientSocket , recvBuff, sizeof(recvBuff)-1 , 0) >0) //Blocking function { printf(">>%s\n" , recvBuff); } char sendBuff[128] = {}; memset(sendBuff, 0 , sizeof(sendBuff)); printf("Please input:\n"); scanf("%s" , sendBuff, sizeof(sendBuff)-1); //Primary transceiver send(clientSocket , sendBuff, strlen(sendBuff), 0); } closesocket(clientSocket); closesocket(serverSocket); WSACleanup(); return 0; }
#include<WinSock2.h> //The socket interface is stored here #include<stdio.h> #pragma comment(lib, "ws2_32.lib") //This is the socket implementation library, which means to connect to this library int main(){ WSADATA wsaData; WSAStartup(MAKEWORD(2,2),&wsaData); if(LOBYTE(wsaData.wVersion)!=2 || HIBYTE(wsaData.wVersion)!=2) { printf("Request version failed"); return -1; } SOCKET clientSocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); if(INVALID_SOCKET == clientSocket) { printf("Socket creation failed"); return -1; } SOCKADDR_IN serverAddr = {0}; //Initialization protocol address serverAddr.sin_family = AF_INET;//Must be consistent with the IP protocol for socket creation serverAddr.sin_port = htons(8080); //Server port, through which clients connect //ip address is a string in dotted format, which is used to convert integers with INET addr serverAddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1"); if(SOCKET_ERROR == connect(clientSocket , (sockaddr *)&serverAddr , sizeof(serverAddr))) { printf("connection failed"); closesocket(clientSocket); //Release socket WSACleanup(); //Close socket return -2; } printf("Successful connection"); char sendBuff[128] = {}; while(1) { memset(sendBuff, 0 , sizeof(sendBuff)); printf("Please input:\n"); scanf("%s" , sendBuff, sizeof(sendBuff)-1); //Primary transceiver send(clientSocket , sendBuff, strlen(sendBuff), 0); char recvBuff[128] = {}; memset(recvBuff, 0 , sizeof(recvBuff)); if(recv(clientSocket , recvBuff, sizeof(recvBuff)-1 , 0) >0) //Blocking function { printf(">>%s\n" , recvBuff); } } closesocket(clientSocket); WSACleanup(); return 0; }