Recently in writing their own development library to write to the socket is a headache, that is sent when the content may be larger than the buffer, and when the reception is not good to judge when to receive the data. So write a send and split after the packet received after the splicing solution. I used a select when I judged whether the data was transmitted at the end of the receipt. Here is an example of a blocking type.
First, we need to define a constant, which is the size of each package when we subcontract. As follows:
#define EACH_PACK_SIZE 1024//Single Packet size
At the time of sending, I used a split string to send the data in a packet-sending way.
bool Sendpacket (SOCKET sendsock, const char * pcontent) {unsigned int unpacksum = strlen (pcontent)/each_pack_size;//compute points Package Quantity if (strlen (pcontent)% each_pack_size! = 0) Unpacksum + = 1;char **szsendpack = (char *) calloc (unpacksum, sizeof (char)); Allocate memory to store the contents of the subcontract for (size_t i = 0; i < unpacksum; i++) {Szsendpack[i] = (char *) calloc (each_pack_size + 1, sizeof (char)); Allocate memory to each sub-package strncpy_s (Szsendpack[i], each_pack_size + 1, pcontent + i * each_pack_size, each_pack_size);//Read into the sub-packet to Array}for (SI ze_t i = 0; i < unpacksum; i++) {ErrorCode = Send (Sendsock, szsendpack[i], strlen (Szsendpack[i]), 0);//Send packet if (ErrorCode = = socket_error)//Send failed { Closesocket (Sendsock); return false;}} for (size_t i = 0; i < unpacksum; i++)//Release memory free (szsendpack[i]); return true;}
For receiving data, because of the variable length of the received data, it is inconvenient to use a char two-dimensional array, so I chose the vector to do the container. Therefore, you should refer to it before use:
#include <vector>using namespace std;
Once the reference is made, we can then process the received data.
char * Recvpacket (SOCKET recvsock) {Vector<char *> vecrecvpack;//store receive sub-packet bool Bemptypack = False;fd_set Crfd;timeval TD = {5, 0};while (True) {Fd_zero (&CRFD); Fd_set (Recvsock, &CRFD); Select (0, &CRFD, NULL, NULL, &TD); if (Fd_isset (Recvsock, &CRFD)) {Bemptypack = False;char *szrecvtmp = (char *) calloc (each_pack_size + 1, sizeof (char));//Allocate memory to buffer sub-packet ErrorCode = recv (Recvsock, SZRECVTM P, each_pack_size, 0); Vecrecvpack.push_back (szrecvtmp);//Add subcontracting to array Fd_zero (&CRFD); td = {0, 0}; Fd_set (Recvsock, &CRFD); Select (0, &CRFD, NULL, NULL, &TD); if (Fd_isset (Recvsock, &CRFD)) continue; Elsebreak;} Else{bemptypack = True;}} if (bemptypack) {//Empty packet Drop connection return char ();} Char *szrecvpack = (char *) calloc (Each_pack_size * vecrecvpack.size () + 1, sizeof (char));//Synthetic sub-package for (size_t i = 0; i < Vecrecvpack.size (); i++) {strncpy_s (Szrecvpack + (i * each_pack_size), Each_pack_size + 1, vecrecvpack[i], each_pack_size);//Read into the contents of the packet to a string} return szrecvpack;}
At this point, the packet sending and receiving is complete.
Troubleshoot sending and receiving multi-packet issues in Winsock