A complicated socket program under Windows

In the previous section, I demonstrated the socket program under Linux. In this section, I will take a look at the socket program under Windows. Othe...

In the previous section, I demonstrated the socket program under Linux. In this section, I will take a look at the socket program under Windows. Otherwise, server.cpp is the server-side code and client is the client-side code.
server.cpp:

#include <stdio.h> #include <winsock2.h> #Pragma comment (LIB, "WS2 \ 32. Lib") / / load WS2 \ 32.dll int main() {/ / / / / / / / / / / / / / / / / / initializthe DLL wsadadata wsadadata; wsstartup (makeword (2, 2), (wsadadata); / / create socket socket socket servsocket = socket (PF \ INET, sock \ stream, ipproto \ TCP); / / bind socket socket socket socket socket [in SOCKADDR; memset (SOCKADDR, 0, sizeof (SOCKADDR)); / / fill sockaddr.sinsinkaddr.sinsinkdr.sinsinpadding (0, sizeof (SOCKADDR), 0, sizeof (SOCKADDR)); / / fill every byte with 0 to fill sockaddr.sinpaddr.sinkaddr.sinpadding.sinevery byte with 0.0 to fill sockaddr.sinevery byte with family =Use the IPv4 address SOCKADDR. Sin addr. S addr. S addr = ineaddr ("127.0.0.1"); / / the detailed IP address SOCKADDR. Sin port = htons (1234); / / port bind (servsocket, (SOCKADDR *) & SOCKADDR, sizeof (SOCKADDR)); / / enter listen (servsocket, 20); / / accept the client's request for SOCKADDR clntaddr; int nSize = sizeof (SOCKADDR); socket clntsocket = access socket socket socket socket socket socket socket socket socket socket socket = access to request the client's sockaddaddr; int nSize = sizeoof (SOCKADDR); enable = sizeof (SOCKADDR); socket clntsocket socket socket socket socket socket socket socket socket = access access = access.Pt (servsock, (SOCKADDR *) & clntaddr, & nSize); / / send data to the client char *str = "Hello World!"; send(clntSock, str, strlen(str)+sizeof(char), NULL); / / close socket (clntsock); close socket (servsock); / / stop using 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 the DLL wsadadata wsadadata; wsstartup (makeword (2, 2), (wsadadata); / / create socket socket socket socket = socket (PF \ INET, sock \ stream, ippto \ TCP); / / propose to the server to request SOCKADDR in SOCKADDR; memset (SOCKADDR, 0, sizeof (SOCKADDR)); / / fill SOCKADDR with 0 in every byte (SOCKADDR, 2, 2, and & wsadadata); / / create socket socket socket socket socket socket socket = socket (PF \ ine, socket \ stream, ippto \ TCP); / / propose to the server to request SOCKADDR in SOCKADDR; memset (SOCKADDR, 0, sizeof (SOCKADDR)); / / fill every byte. sin family Sock addr. Sin addr. S addr. Sin addr. S addr = ineaddr ("127.0.0.1"); SOCKADDR. Sin port = htons (1234); connect (socket, (SOCKADDR *) SOCKADDR, sizeo (SOCKADDR)); / / accept the data returned by the server char szBuffer[MAXBYTE] = ; recv(sock, szBuffer, MAXBYTE, NULL). / / enter the accepted data printf ("message form server:% s form server:% s (message form server:% s:% s:% s (1234)); / / accept the data char szbuffer [maxbyte] = ; rec (sock, szbuffer, maxbyte, null) returned by the server returned by the server(); / / enter the accepted data printf (" message form server:% s:% s:% s (message form server:% s:% s:% s:% s\ n ", szbuffer); / / close socket Socket (socket); / / terminate using DLL WSACleanup(); system("pause"); return 0;}

Differentiate and compile server.cpp and client.cpp into server.exe and client.exe, run server.exe first, then client.exe, the input result is:
Message form server: Hello World!
The socket program under Windows has the opposite thoughts with 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 ways to load DLLs. Please check: loading static link library DLLs
2) Linux uses the concept of "file descriptor", while windows uses the concept of "file handle"; Linux does not distinguish socket files and popular files, while Windows does; the value of socket() function under Linux is int type, while that under windows is socket type, that is, sentence handle.
3) in Linux, read() / write() function is used to read and write, while in Windows, recv() / send() function is used to send and accept.
4) when the socket is closed, Linux uses the close() function, while Windows uses the closesocket() function.

5 November 2019, 09:37 | Views: 6008

Add new comment

For adding a comment, please log in
or create account

0 comments