Solved

Socket send/recv size

Posted on 2006-11-21
5
1,375 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

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…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

773 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