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:
- #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 a socket
- SOCKET servSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
- // Bind the socket
- sockaddr_in sockAddr;
- memset (& sockAddr, 0, sizeof (sockAddr)); // each byte are filled with 0
- sockAddr.sin_family = PF_INET; // use IPv4 addresses
- sockAddr.sin_addr.s_addr = inet_addr ( "127.0.0.1"); // IP address of the specific
- sockAddr.sin_port = htons (1234); // port
- bind(servSock, (SOCKADDR*)&sockAddr, sizeof(SOCKADDR));
- // enter the listening state
- listen(servSock, 20);
- Receiving a client request @
- SOCKADDR clntAddr;
- int nSize = sizeof(SOCKADDR);
- SOCKET clntSock = accept(servSock, (SOCKADDR*)&clntAddr, &nSize);
- // send data to the client
- char *str = "Hello World!";
- send(clntSock, str, strlen(str)+sizeof(char), NULL);
- // close the socket
- closesocket(clntSock);
- closesocket(servSock);
- // terminate the use of the DLL
- 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 a socket
- 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 are 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));
- // server receives the data returned
- char szBuffer[MAXBYTE] = {0};
- recv(sock, szBuffer, MAXBYTE, NULL);
- // the output data received
- printf("Message form server: %s\n", szBuffer);
- // close the socket
- closesocket(sock);
- // terminate the use of DLL
- WSACleanup();
- system("pause");
- return 0;
- }
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.