Windows socket program demonstration

Demonstrated the socket program under Linux, look at the socket program under Windows. Similarly, server.cpp is server-side code and client is client-side code.

Server-side code server.cpp:

#include <stdio.h>
#include <winsock2.h>
#Pragma comment (lib,'ws2_32.lib') / / load ws2_32.dll

int main(){
    //Initialize DLL
    WSADATA wsaData;
    WSAStartup( MAKEWORD(2, 2), &wsaData);
    
    //Create sockets
    SOCKET servSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
    
    //Binding sockets
    sockaddr_in sockAddr;
    memset(&sockAddr, 0, sizeof(sockAddr));  //Each byte is filled with 0
    sockAddr.sin_family = PF_INET;  //Use IPv4 address
    sockAddr.sin_addr.s_addr = inet_addr("127.0.0.1");  //Specific IP address
    sockAddr.sin_port = htons(1234);  //port
    bind(servSock, (SOCKADDR*)&sockAddr, sizeof(SOCKADDR));
    
    //Enter the listening state
    listen(servSock, 20);
    
    //Receiving client requests
    SOCKADDR clntAddr;
    int nSize = sizeof(SOCKADDR);
    SOCKET clntSock = accept(servSock, (SOCKADDR*)&clntAddr, &nSize);
    
    //Send data to client
    char *str = "Hello World!";
    send(clntSock, str, strlen(str)+sizeof(char), NULL);
    
    //Close
    closesocket(clntSock);
    closesocket(servSock);
    
    //Termination of DLL usage
    WSACleanup();
    
    return 0;
}

Client code client.cpp:

#include <stdio.h>
#include <stdlib.h>
#include <WinSock2.h>
#Pragma comment (lib,'ws2_32.lib') / / load ws2_32.dll

int main(){
    //Initialize DLL
    WSADATA wsaData;
    WSAStartup(MAKEWORD(2, 2), &wsaData);
    
    //Create sockets
    SOCKET sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
    
    //Initiate a request to the server
    sockaddr_in sockAddr;
    memset(&sockAddr, 0, sizeof(sockAddr));  //Each byte is filled with 0
    sockAddr.sin_family = PF_INET;
    sockAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
    sockAddr.sin_port = htons(1234);
    connect(sock, (SOCKADDR*)&sockAddr, sizeof(SOCKADDR));
    
    //Receiving data returned by server
    char szBuffer[MAXBYTE] = {0};
    recv(sock, szBuffer, MAXBYTE, NULL);
    
    //Output received data
    printf("Message form server: %s\n", szBuffer);
    
    //Close
    closesocket(sock);
    
    //Discontinue the use of DLL
    WSACleanup();
    
    system("pause");
    return 0;
}

Compiled server.cpp and client.cpp into server.exe and client.exe respectively. Run server.exe first, then client.exe. The output is as follows:
Message form server: Hello World!

The socket program under Windows has the same idea as Linux, but the details are different:

  1. The socket program under Windows depends on Windows sock.dll or ws2_32.dll and must be loaded in advance. DLL has two loading modes, please check: Loading DLL of Dynamic Link Library

  2. Linux uses the concept of "file descriptor" while Windows uses the concept of "file handle"; Linux does not distinguish between socket files and ordinary files, while Windows distinguishes; the return value of socket() function under Linux is int type, and SOCKET type under Windows, that is, handle.

  3. In Linux, read ()/ write () function is used to read and write, while in Windows, recv ()/ send () function is used to send and receive.

  4. When closing sockets, Linux uses the close() function, while Windows uses the close socket () function.

Keywords: socket Windows Linux

Added by eagleweb on Wed, 31 Jul 2019 07:53:17 +0300