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