• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1385
  • Last Modified:

Socket send/recv size

I'm sending chunks of from a client to a server.  The data is about 230K for each send.

It doesn't seem to send all the data.  It sends about half of it.  On the receiving, I do a while loop which keeps receiving until the recv function returns a value of 0.

However, on the sending end I'm not sure how to do this.  Should I come up with an arbtirary number and keep sending that number of bytes in a loop?  I'd think that would segfault, because it would probably access data beyond the end of the buffer I'm sending.  So how do you know how many bytes to send each time?  

My send function is simple, I just do:
      
   int bytes_sent = send(m_sock, buffer.data, buffer.used_bytes, MSG_NOSIGNAL);
   if (bytes_sent == -1) return false;
   else return true;

How can I loop this so it sends the all the data?
0
chsalvia
Asked:
chsalvia
3 Solutions
 
Infinity08Commented:
Something like this maybe (could still contain errors as I put this together in a hurry, but you get the general idea) :

  int bytes_sent = 0;
  int to_send = buffer.used_bytes;
  unsigned char *buf_ptr = buffer.data;
  while (to_send > 0) {
    bytes_sent = send(m_sock, buf_ptr, to_send, MSG_NOSIGNAL);
    if (bytes_sent >= 0) {
      to_send -= bytes_sent;
      buf_ptr += bytes_sent;
    }
    else break;
  }
  if (bytes_sent == -1) return false;
  else return true;
0
 
Kent OlsenData Warehouse Architect / DBACommented:

Just a thought, but the "length" field may be a 16-bit value.  If so, the send can only accommodate about 65K/message.

Try sending the message in smaller chunks, about 50K/message should do.


Kent
0
 
grg99Commented:
One good strategy might be to READ THE FINE MAN PAGES.

In particular, both send and recv have several quirky behaviors if they are having trouble sending or receiving or there is nothing to receive,  plus those behaviors are very different for blocking vs non-blocking sockets.

Not to mention that receiving until recv == 0 is not a documented way to do anything.

You gotta READ the SPECS and write code that intelligently follows the specs.  If you want things to work.

0
 
chsalviaAuthor Commented:
>> Just a thought, but the "length" field may be a 16-bit value.  If so, the send can only accommodate about 65K/message.

I never thought of that.  Well, since the send() function is really just a wrapper around the more low-level "write()" function, maybe I should just use that.  Surely the length argument in the write() function is 32-bits, since I often write huge chunks of data to disk.

>> Not to mention that receiving until recv == 0 is not a documented way to do anything.

How would you tell when to stop a recv loop then?
0
 
grg99Commented:
>How would you tell when to stop a recv loop then?

Two ways I can think of:

(1) Put some noticeable delimiters on the data, that's the way MIME does it.

(2)  First send the length, then send the data.

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now