The TCP-based socket programming server program process is as follows:
1. Create a socket
Socketsocksrv = socket (af_inet, sock_stream, 0 );
2. Bind the socket to the local address and port
Sockaddr_inaddrsrv;
Addrsrv. sin_addr.s_un.s_addr = htonl (inaddr_any); // converts a u_long value to a network byte sequence.
Addrsrv. sin_family = af_inet;
Addrsrv. sin_port = htons (6000 );
BIND (socksrv, (sockaddr *) & addrsrv, sizeof (sockaddr); // bind the created socket
3. Set the created socket to the listening mode and wait for receiving client requests.
Listen (socksrv, 5 );
4. Wait for the customer's request to arrive. When the request arrives, it receives the connection request and returns a new socket for the connection.
Socketsockconn = accept (socksrv, (sockaddr *) & addrclient, & Len );
5. Use the returned socket to communicate with the client
Send (sockconn, szsendbuf, lstrlen (szsendbuf) + 1, 0 );
Recv (sockconn, szrecvbuf, 100, 0 );
6. Return and wait for another customer's request
Closesocket (sockconn );
7. Disable socket
The TCP-based socket programming client process is as follows:
1. Create a socket
Socketsockclient = socket (af_inet, sock_stream, 0 );
2. Send a connection request to the server
Sockaddr_inaddrsrv;
Addrsrv. sin_addr.s_un.s_addr = inet_addr ("192.168.0.1 ");
Addrsrv. sin_family = af_inet;
Addrsrv. sin_port = htons (6000 );
Connect (sockclient, (sockaddr *) & addrsrv, sizeof (sockaddr ));
3. Communicate with the server
Recv (sockclient, szrecvbuf, 100, 0 );
Send (...)
4. Disable socket
The UDP-based socket programming receiver sequence process is as follows:
1. Create a socket
Socketsocksrv = socket (af_inet, sock_dgram, 0 );
2. Bind the socket to the local address and port
Sockaddr_inaddrsrv;
Addrsrv. sin_family = af_inet;
Addrsrv. sin_port = htons (6000 );
Addrsrv. sin_addr.s_un.s_addr = htonl (inaddr_any );
BIND (socksrv, (sockaddr *) & addrsrv, sizeof (sockaddr ));
3. Waiting for receiving data
Sockaddr_inaddrclient;
Int Len = sizeof (sockaddr );
Tchar szrecvbuf [100];
Recvfrom (socksrv, szrecvbuf, lstrlen (szrecvbuf) + 1, 0, (sockaddr *) & addrclient, & Len );
4. Disable socket
The client sequence for UDP-based socket programming is as follows:
1. Create a socket
Socketsockclient = socket (af_inet, sock_dgram, 0 );
2. send data to the server
Sockaddr_inaddrsrv;
Addrsrv. sin_addr.s_un.s_addr = inet_addr ("192.168.0.1 ");
Addrsrv. sin_family = af_inet;
Addrsrv. sin_port = htons (6000 );
Sendto (sockclient, text ("hello"), lstrlen (text ("hello") + 1, 0, (sockaddr *) & addrsrv, sizeof (sockaddr ));
3. Disable socket
About the number of bytes sent by Recv and send:
The send (sockconn, szfilebuffer, nlength, 0) function sends nlenth bytes. If nlength is greater than the szfilebuffer size, the extra bytes are supplemented with \ 0. If it is smaller than the szfilebuffer size, the szfilebuffer string is sent to intercept nlength bytes.
The Recv (sockconn, szfilebuffer, nlength, 0) function principle is the same as above, except that when nlength is greater than the length of szfilebuffer, It is not filled with \ 0.
At the same time, the Recv function will send and receive the same \ 0.