Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Socket send/recv size

Posted on 2006-11-21
5
Medium Priority
?
1,381 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 336 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 46

Assisted Solution

by:Kent Olsen
Kent Olsen earned 332 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 332 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-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.
Suggested Courses

963 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