473,395 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 2168
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Pete Loveall | last post by:
I have a server application that monitors a private local queue for messages. The message sent to it has a label and a response queue defined. It works correctly when the queue is accessed via...
15
by: Steve Horrillo | last post by:
I can't figure out why this script won't insert the subject in the email and why can't I control the font and size being used? I'm not sure where to post this. Let me know where if this is OT. ...
6
by: Tom | last post by:
I try to write a simple tcp based job queue server. It's purpose is to get commands from a client and store them in a queue (STL). The server has a thread which checks periodically the queue,...
0
by: Lynne | last post by:
I am using the C# asynchronous socket functionality for a server, and it appears to work fine if I just receive data and echo it back to the client. The problem occurs when I try to handle sending...
4
by: Sačo Zagoranski | last post by:
Hi! I'm writing a simple 3D First person shooter game. It is a multiplayer game, where all the players connect to one server.
4
by: yaron | last post by:
Hi, I have a problem when sending data over TCP socket from c# client to java server. the connection established ok, but i can't send data from c# client to java server. it's work ok with...
0
by: Shoveler | last post by:
I've got an odd problem here that I've been beating my head on for days. I've written a class that uses a pre-established connection for communication, meaning I can use this class for a server or...
9
by: darthghandi | last post by:
I am trying to create a server application using asynchronous sockets. I run into a problem when I try to connect to my server using a non-.net program. I can establish the connection, and send...
5
by: nick048 | last post by:
Hi to All, Foollowing Your suggestion, I have written this code: #include <stdio.h> #include <string.h> #include <malloc.h> #define BUFLEN 100
1
by: the_ricka | last post by:
Hi all, I'm fairly new to python, but very excited about it's potential. I'm trying to write a simple program that will accept input from a command line and send email. The parameters I used on...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.