A complex socket program under Windows
The previous section demonstrated the socket program under Linux. In this section, let's take a look at the socket program under Windows. Otherwise, server.cpp is the server-side code and client is the client code.
Server side code server.cpp:
# include # include # pragma comment (lib, "ws2_32.lib") / / load ws2_32.dll int main () {/ / initialize DLL WSADATA wsaData; WSAStartup (MAKEWORD (2,2), & wsaData); / / create socket SOCKET servSock = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); / / bind socket sockaddr_in sockAddr; memset (& sockAddr, 0, sizeof (sockAddr)) / / fill every byte with 0 sockAddr.sin_family = PF_INET; / / use the IPv4 address sockAddr.sin_addr.s_addr = inet_addr ("127.0.0.1"); / / detailed IP address sockAddr.sin_port = htons (1234); / / Port bind (servSock, (SOCKADDR*) & sockAddr, sizeof (SOCKADDR)); / / enter snooping mode listen (servSock, 20); / / accept client pleading SOCKADDR clntAddr Int nSize = sizeof (SOCKADDR); SOCKET clntSock = accept (servSock, (SOCKADDR*) & clntAddr, & nSize); / / send data char * str = "Hello World!" to the client; send (clntSock, str, strlen (str) + sizeof (char), NULL); / / closed socket closesocket (clntSock); closesocket (servSock); / / terminate the use of DLL WSACleanup (); return 0;}
Client code client.cpp:
# include # pragma comment (lib, "ws2_32.lib") / / load ws2_32.dll int main () {/ / initialize DLL WSADATA wsaData; WSAStartup (MAKEWORD (2,2), & wsaData); / / create socket SOCKET sock = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); / / request sockaddr_in sockAddr; memset (& sockAddr, 0, sizeof (sockAddr)) to the server / / populate each byte 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)); / / accept the data returned by the server char szBuffer [MAXBYTE] = {0}; recv (sock, szBuffer, MAXBYTE, NULL) / / enter accepted data printf ("Message form server:% s\ n", szBuffer); / / closed socket closesocket (sock); / / terminate the use of DLL WSACleanup (); system ("pause"); return 0;}
Distinguish and compile server.cpp and client.cpp to server.exe and client.exe, run server.exe first, and then run client.exe. The result of input is:
Message form server: Hello World!
The socket program under Windows is the opposite of Linux, but the details are different:
1) the socket program under Windows is attached to Winsock.dll or ws2_32.dll and must be loaded in advance. There are two loading methods for DLL, please check: loading of static link library DLL
2) Linux uses the concept of "file descriptor", while Windows uses the concept of "file handle"; Linux does not distinguish between socket files and popular files, while Windows distinguishes; the go value of the socket () function under Linux is the int type, and under Windows is the SOCKET type, that is, the handle.
3) use read () / write () function to read and write under Linux, and use recv () / send () function to send and accept under Windows.
4) when closing socket, Linux uses the close () function, while Windows uses the closesocket () function.