Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Socket send/recv size

Posted on 2006-11-21
Medium Priority
1,379 Views
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
Question by:chsalvia
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 53

Accepted Solution

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

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

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

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

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

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…
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…
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.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
###### Suggested Courses
Course of the Month5 days, 7 hours left to enroll