Problem with send()

U

ultr

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
 
V

Vladimir S. Oka

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;
 
U

ultr

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,781
Messages
2,569,615
Members
45,296
Latest member
HeikeHolli

Latest Threads

Top