471,585 Members | 1,592 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,585 software developers and data experts.

Problem with send()

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
3 2019
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
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
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.

Similar topics

5 posts views Thread by Pete Loveall | last post: by
15 posts views Thread by Steve Horrillo | last post: by
6 posts views Thread by Tom | last post: by
reply views Thread by Lynne | last post: by
4 posts views Thread by Sa¹o Zagoranski | last post: by
reply views Thread by Shoveler | last post: by
5 posts views Thread by nick048 | last post: by
1 post views Thread by the_ricka | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.