Service side:
#include <stdio.h> #include <winsock2.h> #pragma comment (lib, "Ws2_32.lib" ) //load ws2_32.dll #define buf_size 100 int main (int argc, Char *argv[]) { //initialization DLL WSADATA wsaData; wsastartup ( makeword (2, 2), &wsadata); //Create socket socket servsock = socket (pf_inet, sock_stream, IPPROTO_TCP); if (Servsock == invalid_socket) { printf ("Failed socket (). \ n"); wsacleanup (); return 0; } //binding Sockets struct sockaddr_in Sockaddr; &nbsP;memset (&sockaddr, 0, sizeof (sockaddr)) //each byte is filled with 0 sockaddr.sin_family = pf_inet; //using IPV4 address sockaddr.sin_addr.s_addr &NBSP;=&NBSP;INET_ADDR ("127.0.0.1"); //specific IP address sockaddr.sin_port = htons (1234); //Port if (Bind (servsock, (sockaddr*) &sockAddr, sizeof (SOCKADDR)) == socket_error) { printf ("Failed bind (). \ n"); wsacleanup () ; return 0; } //into the Listening state listen (SERVSOCK,&NBSP;20); //loop receive client request int nsize = sizeof (SOCKADDR); char buffer[buf_size];//Buffer sockaddr clntaddr; socket clntsock; for (;;) { clntsock = accept (ServSock, (sockaddr*) &clntaddr, &nsize); sleep (10000) ;//Note here, let the program pause for 10 seconds &NBSP;&NBSP;INT&NBSP;STRLEN&NBSP;=&NBSP;RECV (clntsock, buffer, buf_size, 0);//Receive data from clients if ( Clntsock == invalid_socket) { printf ("failed accept (). \ n"); continue; } //send the client data back to send (clntsock, buffer, strlen, 0); memset (buffer, 0, buf_size);// Set Buffer //Close Socket closesocket (clntsock); } //Close Socket closesocket (servsock); //Termination DLL Use wsacleanup (); return 0;}
Client:
#include <stdio.h> #include <stdlib.h> #include <winsock2.h> #pragma Comment (lib, "ws2_32.lib") #define buf_size 100 int main (Int argc, char *agrv[]) { //initialization dll wsadata wsadata; wsastartup (Makeword (2,2), &wsadata); for (;;) { //Creating sockets socket sock = socket (PF_INET, SOCK_STREAM, &NBSP;IPPROTO_TCP); if (Sock == invalid_socket) { printf ("Failed socket (). \ n"); wsacleanup (); return 0; } / /initiate a request to the server struct sockaddr_in sockAddr; memset (&sockaddr, 0, sizeof (SOCKADDR)); //each byte is populated with 0 sockAddr.sin_family = PF_INET; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SOCKADDR.SIN_ADDR.S_ADDR&NBSP;=&NBSP;INET_ADDR ("127.0.0.1"); sockaddr.sin_port = htons (1234); if (Connect (sock, (sockaddr*) &sockaddr, sizeof (sockaddr)) == -1) { printf ("Failed conneCT (). \ n "); wsacleanup (); return 0; } // Gets the user-entered string and sends it to the server char bufsend[buf_size] = {0}; printf ("\ninput a string: "); gets (bufsend); int i; for (i=0; i<3; i++) { send ( Sock, bufsend, strlen (bufsend), 0); } //data returned by the receiving server Char bufrecv[buf_size] = {0}; int nrecv &NBSP;=&NBSP;RECV (sock, bufrecv, buf_size, 0); if (nrecv > 0) { bufRecv[nRecv] = '; ' //Output received data printf ("message from server: %s\n", bufrecv); } memset (bufsend, 0, buf_size);// Reset buffer memset (bufrecv, 0, buf_size);//Reset buffer //closing sockets closesocket (sock); } //termination of Use of dll wsacleanup (); return 0; }
Run server first, run client again, and enter the string "ABC" in 10 seconds, and wait a few seconds for the server to return data. The results of the operation are as follows:
Input a STRING:ABC
Message form SERVER:ABCABCABC
The key to this program is server.cpp line 31st code Sleep(10000);
, which allows the program to suspend execution for 10 seconds. During this time, the client sends the string "ABC" three successive times, because the server is blocked, the data can only accumulate in the buffer, after 10 seconds, the server starts to run, reads out all the backlog data from the buffer once, and returns to the client.
It is also necessary to note that the 34th line of code is client.cpp. The client executes to the recv () function, because there is no data in the input buffer, it is blocked until the server returns data for 10 seconds before it begins execution. The visual effect that the user sees is that the client pauses for a period of time before outputting the results returned by the server.
The client's send () sends three packets, and the server's recv () receives only one packet, which is a good illustration of the sticky packet problem of the data.
The sticky packet problem of TCP and the non-boundary of data