By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,559 Members | 2,992 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,559 IT Pros & Developers. It's quick & easy.

Problem with send()

P: n/a
Hello,

I have read that send() may not send whole message and returns the
nuber of bytes sent, so that we can handle the rest of the message. Is
it true, because i have tested sending even 1MB long message by the
Internet connection and it was sent in one message - send() returned
length of whole message?

And the second question: If we really need to send the rest of the
message, what should i do if client disconnects while sending the first
part? Lets say I send 1kB. Client closes connection after 512 bytes, so
send returns 512. There is another 512 bytes to send, so server retries
but when using send() to the not connected client my server program
terminates with no message. Here is the function I use:

bool SendAll( int Client, char Buf[OUTPACKETSIZE], int NBytes )
{
int sent = 0;
int n;
while( sent < NBytes )
{
n = send( Client, Buf+sent, NBytes-sent, 0 );
if ( n == -1 ) { break; }
sent += n;
printf(" sent: %d\n",n);
}
return n==-1 ? false : true;
}

Thank you for replies.

Piotr Dabrowski

Mar 20 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
ultr opined:
I have read that send() may not send whole message and returns the
nuber of bytes sent, so that we can handle the rest of the message.
Is it true, because i have tested sending even 1MB long message by
the Internet connection and it was sent in one message - send()
returned length of whole message?
'send()` is not in Standard C, so it's off-topic here.
And the second question: If we really need to send the rest of the
message, what should i do if client disconnects while sending the
first part? Lets say I send 1kB. Client closes connection after 512
bytes, so send returns 512. There is another 512 bytes to send, so
server retries but when using send() to the not connected client my
server program terminates with no message. Here is the function I
use:
I can't help with the logic, as I honestly don't know what `send()`
does, but I do have some small quibbles:
bool SendAll( int Client, char Buf[OUTPACKETSIZE], int NBytes )
{
int sent = 0;
int n;
while( sent < NBytes )
{
n = send( Client, Buf+sent, NBytes-sent, 0 );
if ( n == -1 ) { break; }
If you don't mind multiple exit points

if ( n == -1)
return false;

may be better. As a matter of style, I'd also prefer:

if (n == -1)
break;
sent += n;
printf(" sent: %d\n",n);
}
Horizontal spacing is cheap...

What happens if `n == 0`? It seems you'll never exit the loop.
return n==-1 ? false : true;
Wouldn't:

return n != -1;

be the same, and better style? If you decide on multiple exit points,
you only need:

return true;
}

Thank you for replies.

Piotr Dabrowski


--
BR, Vladimir

The idea of male and female are universal constants.
-- Kirk, "Metamorphosis", stardate 3219.8

Mar 20 '06 #2

P: n/a
ultr wrote:
I have read that send() may not send whole message and returns the
nuber of bytes sent, so that we can handle the rest of the message.


I think send() is defined in POSIX.

http://www.opengroup.org/onlinepubs/...ions/send.html

You had better ask in comp.unix.programmer
Mar 20 '06 #3

P: n/a
Yes, it is in POSIX. I will ask there :)
Sorry for sending in not this group and thank you for your answers.

Mar 20 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.