Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Winsock error: WSAEMSGSIZE (10040): ->buffer used to receive a datagram into was smaller than the datagram itself.

Posted on 2003-05-03
3
Medium Priority
?
5,549 Views
Last Modified: 2013-11-13
Background Info:

I am trying to write a program on Windows(WinSock) that creates 2 SOCK_DGRAMs that are binded to the same machine(My PC: 127.0.0.1). These two sockets need to work as follows:
1) listeningSocket(non-blocking mode)
2) sendingSocket(blocking mode/dont care)

I am trying to send a file thru the "sendingSocket" and
recieve the same file thru the "listeningSocket".

My file is 14990 bytes in size.
I read the file into an array sendBuffer[14990]
I have a corresponding recvBuffer[14990]
I determined the max buffer size of my system(8192 bytes) by doing a getsockopt().

Then i divide the total file size into chunks of 8192 bytes and send each chunk thru the "sendingSocket" (In this case i get 2 chunks: 1 whole chunk of 8192 bytes + 1 partial chunk of 6798 bytes).........this works fine!!

Similarly i receive these chunks from the listeningSocket. I receive the first whole chunk of 8192 bytes sucessfully..no problems!. But when I try to receive the partial chunk of 6798 bytes...i get the following error

WSAEMSGSIZE (10040): Message too long -> A message sent on a datagram socket was larger than the internal message buffer or some other network limit, or the buffer used to receive a datagram into was smaller than the datagram itself.

Problem/Question:
Why does recvfrom() fail on the partial chunk? I have also provided(below) the code for the send and receive functions.

please advise


-------- start code
//#####################################################################

int recv()
{
      // start receiving data from sendbuffer to bufrecv       
      int recv,totalrecv =0;
      SOCKADDR_IN from;     /* Sender's address. */
        fromlen = sizeof(from);
      
      getsockopt(listeningSocket,        
               SOL_SOCKET,        
               SO_RCVBUF,                        
               (char *)&sockbufsize,  
               &size);

      //following forloop receives byte chunks of size sockbufsize(8192 bytes)
      for (j=0; j<numread/sockbufsize; j++)
      {
            recv = recvfrom(listeningSocket,
                        bufrecv+j*sockbufsize,
                        sockbufsize,
                        MSG_PEEK,
                        (LPSOCKADDR)&from,
                        &fromlen);
            
            //check for receive errors
            if (recv == SOCKET_ERROR)
            {
                  nret = WSAGetLastError();
                  ReportError(nret, "recvfrom()");
                  WSACleanup();
                  return NETWORK_ERROR;
            }

            totalrecv = totalrecv + recv;
                              
      }

      // now take care of the last byte chunk that is less than sockbufsize(8192 bytes)
      
/***********Something wrong with the following statement...dont know why "recvfrom" fails and gives Error 10040 i.e "datagram read is larger than the buffer supplied"  Size of the datagram read -> "numread%sockbufsize".......this is exactly equal to the buffer supplied -> (bufrecv+j*sockbusize))******************/
      
      recv = recvfrom(listeningSocket,
                  bufrecv+j*sockbufsize,
                  numread%sockbufsize,
                  MSG_PEEK,
                  (LPSOCKADDR)&from,
                  &fromlen);


      
      //check for receive errors
      if (recv == SOCKET_ERROR)
      {
            nret = WSAGetLastError();
            ReportError(nret, "recvfrom()");
            WSACleanup();
            return NETWORK_ERROR;
      }
            
      totalrecv=totalrecv+recv;
      
}
//#####################################################################
int send()
{
      // Start sending and receiving data
          ///////  first determine the MTU ////////

      int    sockbufsize = 0;
      int    size = sizeof(int);

      int returntype = getsockopt(senderSocket,        
                         SOL_SOCKET,        
                        SO_SNDBUF,                        
                        (char *)&sockbufsize,  
                        &size                            
                        );


        ////// Send data from the sendbuffer in chunks //////
      
      //following for loop sends byte chunks of size sockbufsize(8192 bytes) from sendbuf
      for (i=0; i<numread/sockbufsize; i++)
      {
            sent = sendto(  senderSocket,
                        bufsend+i*sockbufsize,
                        sockbufsize,
                        MSG_DONTROUTE,
                        (LPSOCKADDR)&serverInfo,
                        sizeof(struct sockaddr));
            
            //check for send errors
            if (sent == SOCKET_ERROR)
            {
                  nret = WSAGetLastError();
                  ReportError(nret, "sendto()");
                  WSACleanup();
                  return NETWORK_ERROR;
            }

            totalsent=totalsent+sent;
      }
      
      // now take care of the last byte chunk that is less than sockbufsize(8192 bytes)

      sent = sendto(  senderSocket,
                  bufsend+i*sockbufsize,
                  numread%sockbufsize,
                  MSG_DONTROUTE,
                  (LPSOCKADDR)&serverInfo,
                  sizeof(struct sockaddr));

      //check for send errors
      if (sent == SOCKET_ERROR)
            {
                  nret = WSAGetLastError();
                  ReportError(nret, "sendto()");
                  WSACleanup();
                  return NETWORK_ERROR;
            }

      totalsent=totalsent+sent;

}



//#####################################################################

-------- end code

0
Comment
Question by:psudame
3 Comments
 

Accepted Solution

by:
jtblackburn earned 290 total points
ID: 8451631
Try this.  I think memset will do it for you.

memset( RcvBuff, 0, sizeof( RcvBuff) );
nRet = recv( rsock, RcvBuff, sizeof( RcvBuff), 0);


where nRet = the error number

make sure you include memory.h

Good Luck

Jonathan
0
 

Expert Comment

by:CleanupPing
ID: 9472849
psudame:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 5

Expert Comment

by:dc197
ID: 23186243
This may also help with some background info:

http://www.winsock-error.com/10040.aspx
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question