Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


UDP sendto/recvfrom

Posted on 2004-10-28
Medium Priority
Last Modified: 2007-12-19

Can I do the following in the Windows C++ programming :

I am sending this from Client side:
      iBytesSent = sendto( socket, BufData,
                  20, 0, (SOCKADDR*) &service,sizeof(service) );

And receiving this at Server end....

      bytesRecv  = recvfrom(socket, RecData, 6,0,
                  (SOCKADDR*) &service, &addrLen);

What I am trying to do here is sent 20 bytes and receive 6 bytes. I am getting -1 error when I do this.

Here is the error detail by WSAGetLastError():

            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                               was smaller than the datagram itself.

It work in TCP but I need to do it in UDP. Please let me know if this is possible in UDP.

Question by:raza
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 19

Accepted Solution

drichards earned 200 total points
ID: 12446859
You can do it, but you will always get that error telling you that the datagram was too long for the buffer.  The reason it gives you an error is that the unreceived data will be thrown away.  In TCP, data is a stream that you can send and receive in different size chunks.  UDP sends datagrams that are expected to be read in their entirety.  If it's OK for you to lose the rest of the bytes, you can recive only the first 6 and ignore the WSAEMSGZIZE error.

Author Comment

ID: 12447372

Thank you for your responds.

How do I know how many bytes were recevied.
LVL 19

Expert Comment

ID: 12448293
The return value of recvfrom tells you how many bytes you actually read.  If you want to know how long the incoming datagram is, I think you can peek at the input queue using MSG_PEEK in the flags parameter.  This will return to you the length of the incoming data and leave the data in the queue so you can allocate a large enough buffer and then do a normal recvfrom.

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

636 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