UDP sendto/recvfrom

Posted on 2004-10-28
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
    LVL 19

    Accepted Solution

    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


    Thank you for your responds.

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

    Expert Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    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++ ( I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
    This video Micro Tutorial is the second in a two-part series that shows how to create and use custom scanning profiles in Nuance's PaperPort 14.5 ( But the ability to create custom scanning profiles a…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    933 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now