The difference between network programming under windows and network programming under Linux

1. The difference between network programming under Windows and network programming under Linux

(1) The header files required for Socket network sockets are different:
  Windows system: generally use #include <winsock2.h>
  Linux system: generally use #include <sys/socket.h>
(2) Network initialization
  Windows system: You need to use WSAStartup to start the use of Winsock DLL and initialize the network. The WSAStartup function must be the first Windows Sockets function called by an application or DLL. It allows an application or DLL to specify the desired version of Windows Sockets and to retrieve details of a specific Windows Sockets implementation. An application or DLL can only issue further Windows Sockets functions after a successful call to WSAStartup. Generally, we can complete the network initialization by defining an initialization function (PS: the library ws2_32.lib needs to be added to the project before using WSAStartup)

//初始化网络库
#pragma comment(lib,"ws2_32.lib")
void InitNetwork() {
	WORD wVersionRequested;
	WSADATA wsaData;
	int err;
	wVersionRequested = MAKEWORD(2, 2);

	err = WSAStartup(wVersionRequested, &wsaData);
	if (err != 0)
	{
		return;
	}
}

  Linux system: No need for network initialization operation
(3) There is no write read function under windows, and there is no concept of everything as a file (very important)

//linux系统下
// socket_fd 套接字;
// num			是一个int类型的变量
write(socket_fd, &num, sizeof(num));
read(socket_fd, &num, sizeof(num));
	
//因为windows下没有一切皆文件的概念,所以write函数和linux系统下不同,上面代码可以改为
send(socket_fd, (char *)&num, sizeof(num), 0);
recv(socket_fd, (char *)&num, sizeof(num), 0);

(4) Close the socket socket
closesocket() under Windows,
close() under Linux
(5) Set non-blocking
Windows system: ioctlsocket()
Linux system: fcntl() (need to include the header file <fcntl.h>)
(6) Get error code
Windows system: getlasterror()/WSAGetLastError()
Linux system: errno variable

2. Network programming steps under Linux system (Take TCP as an example)

Server:
​ Create a socket socket()
​ Bind the socket to the server network information structure bind()
​ Set the socket to the listening state listen()
​ Block and wait for the client's connection request accept() (blocking function)
to communicate recv()/send()
to close the socket close()

Client:
Create socket socket()
Send client connection request connect()
Communicate send()/recv()
Close socket close()

3. Network programming under Windows (Take TCP as an example):

Windows provides both upper-level network API functions and lower-level API functions.

1. For API functions using the upper layer: If the csocket class is used to define an object obj, then the steps for network programming are as follows:
Client:
obj.Create()
obj.Connect()
obj.Receive() or obj. Send()
obj.Close()
server side:
first call AfxSocketInit() to detect protocol stack installation
obj.Create()
obj.Listen()
obj.Accpet()
obj.Receive() or obj.Send()
obj.Close ()

2. For the use of the underlying API functions, the steps are as follows:
Client:
Initialize the network WSAStartup()
​ Create a socket socket()
​ Send a client connection request connect()
​ Perform communication send()/recv(
) close the socket close()

Server side:
Initialize the network WSAStartup()
Create a socket socket()
Bind the socket with the server network information structure bind()
Set the socket to the listening state listen()
Block and wait for the connection request from the client accept() (blocking function)
communicate recv()/send()
close socket close()

Reference: https://www.cnblogs.com/gildoringlorin/p/3951317.html

Guess you like

Origin blog.csdn.net/AAAA202012/article/details/130237485