Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I'm sending a large amount of data through a CSocket, more then 5Mb. Therefore, I splitt the data into chunks of 1024 bytes each. Apparently the connection is done correctly because I can send any data as long as I don't split it. The process of sending the data in chunks is the following:
1. The sender first sends a long with the number of byte that the next package will have.
2. The receiver keeps this value and enters into a while loop of Receive calls until it receives the total amount of bytes.
3. Meanwhile the sender enters on a while loop of Send calls until it sends all the bytes.

The problem is that it sends the first 4 of 5 chuncks of 1024 bytes and then, the send function returns repeatidly the error 10014 in the while loop. Once I mange to send almost almost 2Mb before it returns 10014!!!

The code of Sender part: (NetSocket is a CSocket)

C++
int NetSocket::Send(const void *packet, const size_t &size)
{
  size_t totalBytesToSend = size;
  size_t totalBytesSent = 0;

  do
  {
    int cbLeftToSend = totalBytesToSend - totalBytesSent;

    if(cbLeftToSend > MAX_MESSAGE_LENGTH) // MAX_MESSAGE_LENGTH = 1024
      cbLeftToSend = MAX_MESSAGE_LENGTH;
      
    int cbBytesSent = 0;

    do
    { 
      const char *start = &(((char*)packet)[totalBytesSent]);

      cbBytesSent = CSocket::Send(start,cbLeftToSend,0);

      if(cbBytesSent == SOCKET_ERROR)
      {
        netAPIError = GetLastError(); // This is where I get netAPIError = 10014 !!!
        return totalBytesSent;
      }
      else
      {
        totalBytesSent += cbBytesSent;  
        cbLeftToSend -= cbBytesSent;
      }
    }
    while(cbLeftToSend > 0);
         
  }
  while(totalBytesSent < totalBytesToSend);

  return totalBytesSent;
}


The code on the receiver Socket:

C++
size_t NetSocket::Receive()
{
  if(_rcvdPck)
    DeleteReceivedPacket();

  int pckSize;
  int totalRcvdBytes = CSocket::Receive(&pckSize, 4);
  totalRcvdBytes = 0;

  char *pck = new char[pckSize];

  while(totalRcvdBytes < pckSize)
  {
    int dataRead = CSocket::Receive(&pck[totalRcvdBytes], pckSize - totalRcvdBytes);
    totalRcvdBytes += dataRead;
  }

   _rcvdPck = (NetPacket*)(pck);

  return 1; // success
}

Can you see anything wrong ni the code?
Thank you for helping me.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900