Solved

Socket send/recv size

Posted on 2006-11-21
5
1,373 Views
Last Modified: 2010-08-05
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
Comment
Question by:chsalvia
5 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 84 total points
ID: 17988641
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
 
LVL 45

Assisted Solution

by:Kdo
Kdo earned 83 total points
ID: 17989058

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
 
LVL 22

Expert Comment

by:grg99
ID: 17990504
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
 

Author Comment

by:chsalvia
ID: 17994349
>> 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
 
LVL 22

Assisted Solution

by:grg99
grg99 earned 83 total points
ID: 17994878
>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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

708 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

18 Experts available now in Live!

Get 1:1 Help Now