We talked about the festival socket buffer and data transfer process, you can see the receiving and sending data is irrelevant, no matter how many times data is sent, received data will be as many read () / recv () function. In other words, read () / recv () and write () / send () the number of executions may be different.
For example, write () / send () is repeatedly performed three times, each time to send the string "abc", then the read () / recv () on the target machine may be received three times, each time receiving "abc"; may receiving twice, first received "abcab", the second receiving "cabc"; also be received once the string "abcabcabc".
Suppose we want the client to send each student's student number, so the server returns the student's name, address, scores and other information, this time the problem may occur, the server can not distinguish between students' learning numbers. Transmitting a first example, the second transmission 3, the server 13 may be handled as the information returned obviously wrong.
That is, the plurality of data packets "stick pack" data problems, the client is transmitted as a data packet received. Also known as the borderless nature of the data, read () / recv () function does not know the beginning or end marker packets (in fact, there is no beginning or end flag), just treat them as a continuous stream of data to process.
For example, write () / send () is repeatedly performed three times, each time to send the string "abc", then the read () / recv () on the target machine may be received three times, each time receiving "abc"; may receiving twice, first received "abcab", the second receiving "cabc"; also be received once the string "abcabcabc".
Suppose we want the client to send each student's student number, so the server returns the student's name, address, scores and other information, this time the problem may occur, the server can not distinguish between students' learning numbers. Transmitting a first example, the second transmission 3, the server 13 may be handled as the information returned obviously wrong.
That is, the plurality of data packets "stick pack" data problems, the client is transmitted as a data packet received. Also known as the borderless nature of the data, read () / recv () function does not know the beginning or end marker packets (in fact, there is no beginning or end flag), just treat them as a continuous stream of data to process.
The following code shows the problem of stick package, the client sends data three times to the server, the server has received all the data one time.
Server-side code server.cpp:
#include <stdio.h> #include <windows.h> #pragma comment (lib, "ws2_32.lib") //加载 ws2_32.dll #define BUF_SIZE 100 int main () { WSAData wsaData; WSAStartup( MAKEWORD(2, 2), &wsaData); // Create a socket ServSock SOCKET = socket, (AF_INET, SOCK_STREAM, and 0),; // Bind the socket sockaddr_in sockAddr; Memset ( & sockAddr, 0, the sizeof (sockAddr)); // each byte are filled with 0 sockAddr.sin_family = PF_INET; // use IPv4 addresses sockAddr.sin_addr.s_addr = the inet_addr ( " 127.0.0.1 " ); // specific IP addresses sockAddr.sin_port = the htons (1234); // port bind(servSock, (SOCKADDR*)&sockAddr, sizeof(SOCKADDR)); // enter the listening state listen(servSock, 20); // receive a client request SOCKADDR clntAddr; int nSize = sizeof(SOCKADDR); Buffer char [buf_size] = {0}; // buffer SOCKET clntSock = accept(servSock, (SOCKADDR*)&clntAddr, &nSize); SLEEP ( 10000); // Note that, the program to pause 10 seconds // receiving data sent by the client, and returns as int recvLen = recv(clntSock, buffer, BUF_SIZE, 0); send(clntSock, buffer, recvLen, 0); // close the socket and cease to use the DLL closesocket(clntSock); closesocket(servSock); WSACleanup(); return 0; }
Client code client.cpp:
# The pragma Comment (lib, "ws2_32.lib") // Load ws2_32.dll # DEFINE 100 buf_size int main () { // Initialization DLL WSAData wsaData; WSAStartup(MAKEWORD(2, 2), &wsaData); // initiate a request to the server sockaddr_in sockAddr; Memset ( & sockAddr, 0, the sizeof (sockAddr)); // each byte are filled with 0 sockAddr.sin_family = PF_INET; sockAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); sockAddr.sin_port = htons(1234); // Create a socket Sock SOCKET = socket, (PF_INET, SOCK_STREAM, IPPROTO_TCP),; connect(sock, (SOCKADDR*)&sockAddr, sizeof(SOCKADDR)); // get user input string sent to the server char bufSend[BUF_SIZE] = {0}; printf("Input a string: "); gets(bufSend); for(int i=0; i<3; i++){ send(sock, bufSend, strlen(bufSend), 0); } // receiving server returns data char bufRecv[BUF_SIZE] = {0}; recv(sock, bufRecv, BUF_SIZE, 0); // the output data received printf("Message form server: %s\n", bufRecv); the closesocket (our sock); // Close the socket WSACleanup (); // terminate the use of DLL system("pause"); return 0; }