Troubleshoot sending and receiving multi-packet issues in Winsock

Source: Internet
Author: User

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 (&AMP;CRFD); Fd_set (Recvsock, &AMP;CRFD); Select (0, &AMP;CRFD, NULL, NULL, &AMP;TD); if (Fd_isset (Recvsock, &AMP;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 (&AMP;CRFD); td = {0, 0}; Fd_set (Recvsock, &AMP;CRFD); Select (0, &AMP;CRFD, NULL, NULL, &AMP;TD); if (Fd_isset (Recvsock, &AMP;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.