Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 609835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:36:33+00:00 2026-05-13T17:36:33+00:00

The recv() library function man page mention that: It returns the number of bytes

  • 0

The recv() library function man page mention that:

It returns the number of bytes received. It normally returns any data available, up to the requested amount, rather than waiting for receipt of the full amount requested.

If we are using blocking recv() call and requested for 100 bytes:

recv(sockDesc, buffer, size, 0); /* Where size is 100. */

and only 50 bytes are send by the server then this recv() is blocked until 100 bytes are available or it will return receiving 50 bytes.

The scenario could be that:

  • server crashes after sendign only 50 bytes

  • bad protocol design where server is only sending 50 bytes while client is expecting 100 and server is also waiting for client’s reply (i.e. socket close connection has not been initiated by server in which recv will return)

I am interested on Linux / Solaris platform. I don’t have the development environment to check it out myself.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T17:36:33+00:00Added an answer on May 13, 2026 at 5:36 pm

    recv will return when there is data in the internal buffers to return. It will not wait until there is 100 bytes if you request 100 bytes.

    If you’re sending 100 byte “messages”, remember that TCP does not provide messages, it is just a stream. If you’re dealing with application messages, you need to handle that at the application layer as TCP will not do it.

    There are many, many conditions where a send() call of 100 bytes might not be read fully on the other end with only one recv call when calling recv(…, 100); here’s just a few examples:

    • The sending TCP stack decided to bundle together 15 write calls, and the MTU happened to be 1460, which – depending on timing of the arrived data might cause the clients first 14 calls to fetch 100 bytes and the 15. call to fetch 60 bytes – the last 40 bytes will come the next time you call recv() . (But if you call recv with a buffer of 100 , you might get the last 40 bytes of the prior application “message” and the first 60 bytes of the next message)

    • The sender buffers are full, maybe the reader is slow, or the network is congested. At some point, data might get through and while emptying the buffers the last chunk of data wasn’t a multiple of 100.

    • The receiver buffers are full, while your app recv() that data, the last chunk it pulls up is just partial since the whole 100 bytes of that message didn’t fit the buffers.

    Many of these scenarios are rather hard to test, especially on a lan where you might not have a lot of congestion or packet loss – things might differ as you ramp up and down the speed at which messages are sent/produced.

    Anyway. If you want to read 100 bytes from a socket, use something like

    int
    readn(int f, void *av, int n)
    {
        char *a;
        int m, t;
    
        a = av;
        t = 0;
        while(t < n){
            m = read(f, a+t, n-t);
            if(m <= 0){
                if(t == 0)
                    return m;
                break;
            }
            t += m;
        }
        return t;
    }
    

    …

    if(readn(mysocket,buffer,BUFFER_SZ) != BUFFER_SZ) {
      //something really bad is going on.
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the library file name where BSD's socket(), recv(), send(), etc defined on
I have a problem with a socket library that uses WSAASyncSelect to put the
What I am trying to do is use the Detours library to hook into
So I am trying to compile the libssh2 library on linux, but when I
After reading a few related questions I've decided to use the tpl library to
Hey im writing an echo client, and for some reason the connectsock function is
I looked at this example http://msdn.microsoft.com/en-us/library/bb399420.aspx and clicked on DataContext.CreateDatabase which brought me to
I'm trying to get a simple example working using gSoap, for VS2008. I've done
I compiled libxml2 with BCC 5.5 command line compiler, now I have lots of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.