Network Socket Learning Notes (1.1)

xiaoxiao2021-03-06  14

Binding socket

// Binding socket sockaddr_in service; service.sin_family = AF_INET; service.sin_addr.s_un.s_addr = inet_addr ("127.0.0.1"); service.sin_port = htons (27015);

Bind (socketsvr, (sockaddr *) & service, sizeof (service));

// Call listen to monitor the message from the client // below is the reference to the documentation with the MSDN Description // TO Accept Connections, a socket is first created with the socket function and bound to a local address with the bind to a local address with the bind function, // a backlog for incoming connections is specified with listen, // and then the connections are accepted with the accept function. // Sockets that are connection oriented, those of type SOCK_STREAM for example, // are used with listen. The socket s IS PUT INTO Passive Mode Where incoming Connection Requests Are Acknowledge by the process. // The following is my translation // In order to receive the message, start with a socket to create a socket object, then use the bind function to put the local Address and Create // Socket Object Help to // Blaklog indicates the maximum number of listeners allowed simultaneously, and then use the Accept function to accept the message //-oriented socket-oriented socket in the message queue, use the SOCK_STREAM type as an example, should be used Listen Method // When the external connection request is confirmed and placed in the end queue, Sockets enters the passive mode.

Step 4: Monitoring, Accepting and Treating Sockets

When the server-side Socket object bind is completed, the server side must establish a listening queue to receive the client's connection request. The listen () function enables the server-side socket to enter the listening state, and sets the maximum number of connections that can be established (the current maximum limit is 5, the minimum is 1). This function call successfully returns 0, otherwise returns socket_error. Int Pascal Far Listen (Socket S, Int Backlog); Parameters: S: Sockets need to be established; Backlog: Maximum connection number; server-side Socket call Listen (), if the client calls the connect () function If the application is applied, the Server side must call the Accept () function, so that the server side and the client are formally completed the connection action of the communication program. In order to know when the client proposes a connection request, the server-side socket is called the Accept () function to complete the establishment of the connection, let the system actively to inform us that the client proposes a connection request. . This function call successfully returns 0, otherwise returns socket_error.

Code

Listen (socketsvr, 5); // accept function Using SockAddr_in Client; int clientlen = sizeof (sockaddr_in); while (1) {// accept function call successfully returns a SOKET object, so it is necessary to build when using accept A Socket Object // If you establish a failure reference MSDN socket receive; Receive = Accept (socketsvr, (socketsvr, (socketdr *) & client, & clientlen; // establish a memory buffer to store the received string char buffer [50]; /// Format the memory area sprintf (buffer, "% s: welcome to dark blue space", inet_ntoa (client.sin_addr); // Call the Send Send Message Send (Receive, Buffer, Sizeof (Buffer) 1,0); // Call the RECV Receive Message Char RecvBuffer [100]; Recv (Receive, RecvBuffer, 100, 0); Printf ("% s", recvbuffer; clossoSocket;} About the complete server-side code

#include #include

int main () {WORD wVersionRequested; WSADATA wsaData; int err; wVersionRequested = MAKEWORD (1, 1); err = WSAStartup (wVersionRequested, & wsaData); if (err = 0!) {return 0;} if (LOBYTE (wsaData. !! wVersion) = 1 || hIBYTE (wsaData.wVersion) = 1) {WSACleanup (); return 0;} SOCKET SocketSvr; SocketSvr = socket (AF_INET, SOCK_STREAM, 0); sockaddr_in service; service.sin_family = AF_INET; service .s_addr.s_un.s_addr = inet_addr ("127.0.0.1"); service.sin_port = HTONS (27015); Bind (socketsvr, (sockaddr *) & service, sizeof (service)); Listen (socketsvr, 5);

sockaddr_in client; int ClientLen = sizeof (sockaddr_in); while (1) {SOCKET Receive; Receive = accept (SocketSvr, (SOCKADDR *) & client, & ClientLen); char buffer [50]; sprintf (buffer, "% s: welcome To dark blue space ", INET_NTOA (Client.sin_ADDR); Send (Receive, Buffer, Sizeof (Buffer) 1,0); Char Recvbuffer [100]; RECV (RecV (Recvbuffer, 100, 0); Printf ("% s ", recvbuffer); clossoSocket;} return 0;}

Finally finished

Get the client to see the client in the evening. MSDN is exhausted.

Another point is not to forget to include WS2_32.lib. Header file when they are connected.

Get the client to see the client in the evening. MSDN is exhausted.

Another point is not to forget to include WS2_32.lib. Header file when they are connected.

转载请注明原文地址:https://www.9cbs.com/read-50162.html

New Post(0)