socket demonstration program under Windows

On section demonstrates  Linux  under the  socket  program, this section look at the socket program under Windows. Similarly, server.cpp for the server-side code, client to client code.

Server-side code server.cpp:

 
  1. #include <stdio.h>
  2. #include <winsock2.h>
  3. #pragma comment (lib, "ws2_32.lib") // Load ws2_32.dll
  4.  
  5. int main () {
  6. // Initialize DLL
  7. WSAData wsaData;
  8. WSAStartup( MAKEWORD(2, 2), &wsaData);
  9.  
  10. // Create a socket
  11. SOCKET servSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  12.  
  13. // Bind the socket
  14. sockaddr_in sockAddr;
  15. memset (& sockAddr, 0, sizeof (sockAddr)); // each byte are filled with 0
  16. sockAddr.sin_family = PF_INET; // use IPv4 addresses
  17. sockAddr.sin_addr.s_addr = inet_addr ( "127.0.0.1"); // IP address of the specific
  18. sockAddr.sin_port = htons (1234); // port
  19. bind(servSock, (SOCKADDR*)&sockAddr, sizeof(SOCKADDR));
  20.  
  21. // enter the listening state
  22. listen(servSock, 20);
  23.  
  24. Receiving a client request @
  25. SOCKADDR clntAddr;
  26. int nSize = sizeof(SOCKADDR);
  27. SOCKET clntSock = accept(servSock, (SOCKADDR*)&clntAddr, &nSize);
  28.  
  29. // send data to the client
  30. char *str = "Hello World!";
  31. send(clntSock, str, strlen(str)+sizeof(char), NULL);
  32.  
  33. // close the socket
  34. closesocket(clntSock);
  35. closesocket(servSock);
  36.  
  37. // terminate the use of the DLL
  38. WSACleanup();
  39.  
  40. return 0;
  41. }


Client code client.cpp:

 
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <WinSock2.h>
  4. #pragma comment (lib, "ws2_32.lib") // Load ws2_32.dll
  5.  
  6. int main () {
  7. // Initialize DLL
  8. WSAData wsaData;
  9. WSAStartup(MAKEWORD(2, 2), &wsaData);
  10.  
  11. // Create a socket
  12. SOCKET sock = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
  13.  
  14. // initiate a request to the server
  15. sockaddr_in sockAddr;
  16. memset (& sockAddr, 0, sizeof (sockAddr)); // each byte are filled with 0
  17. sockAddr.sin_family = PF_INET;
  18. sockAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
  19. sockAddr.sin_port = htons(1234);
  20. connect(sock, (SOCKADDR*)&sockAddr, sizeof(SOCKADDR));
  21.  
  22. // server receives the data returned
  23. char szBuffer[MAXBYTE] = {0};
  24. recv(sock, szBuffer, MAXBYTE, NULL);
  25.  
  26. // the output data received
  27. printf("Message form server: %s\n", szBuffer);
  28.  
  29. // close the socket
  30. closesocket(sock);
  31.  
  32. // terminate the use of DLL
  33. WSACleanup();
  34.  
  35. system("pause");
  36. return 0;
  37. }


The server.cpp and client.cpp were compiled for server.exe and client.exe, run the server.exe, then run client.exe, output is:
the Message Server form:! The Hello World

socket program under Windows and Linux ideas the same, but the details are some differences:
1) socket program under Windows relies Winsock.dll or ws2_32.dll, must be loaded in advance. There are two ways to load DLL, please see: dynamic link library DLL load

2) Linux uses the concept of "file descriptor", while Windows uses "file handle" concept; Linux does not distinguish between ordinary files and socket files, and Windows distinguish ; Linux under the socket () function returns a value of type int, and for the next Windows sOCKET type, which is the handle.

3) using the following Linux read () / write () function to read and write, and the transmission and reception functions using the recv () / send () under Windows.

4) closed socket, Linux using the close () function, while Windows uses closesocket () function.

Guess you like

Origin blog.csdn.net/Qsir/article/details/93738790