Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

while loop for recv and send to get all data doesn't work
Hi,

I'm trying to put recv and send in a while(1) loop to ensure i get all
data from the client and from the server. Within the loop i check for <=0
, if it occurs, i do a break.

However, it does not work. It seems to keep sending data and never breaks!

I use the following loop for send:

while(1) {
if(send(sockfd,buffer,strlen(buffer),0)<=0)
break;
}

and for recv

while(1) {
if (recv(sockfd,buffer,sizeof(buffer),0)<=)
break;
}
because recv gets the entire buffer, i terminate
the buffer like this buffer[strlen(buffer)-1]='\0';

Apart from the above problem, i also realize what would happen
if the buffer did not get sent completetly,iwould get double
data because the buffer would get sent twice.

What am i doing wrong,
Alef

Report this thread to moderator Post Follow-up to this message
Old Post
Alef T Veld
10-11-04 01:56 PM


Re: while loop for recv and send to get all data doesn't work
Alef T Veld wrote:
> Hi,
>
> I'm trying to put recv and send in a while(1) loop to ensure i get all
> data from the client and from the server. Within the loop i check for <=0
> , if it occurs, i do a break.
>
> However, it does not work. It seems to keep sending data and never breaks!
>
> I use the following loop for send:
>
> while(1) {
> if(send(sockfd,buffer,strlen(buffer),0)<=0)
> break;
> }
>
> and for recv
>
> while(1) {
> if (recv(sockfd,buffer,sizeof(buffer),0)<=)
> break;
> }
> because recv gets the entire buffer, i terminate
> the buffer like this buffer[strlen(buffer)-1]='\0';
>
> Apart from the above problem, i also realize what would happen
> if the buffer did not get sent completetly,iwould get double
> data because the buffer would get sent twice.
>
> What am i doing wrong,

Implementation.


Use this one:

int already_read = 0;

while(1) {
int rd = recv(sockfd, buffer + already_read, sizeof(buffer) -
already_read, 0);
switch(rd) {
case 0: return already_read;
case -1:
switch(errno) {
case EINTR:
case EAGAIN:
continue; /* Safe one */
}
return -1; /* Hard error */
}
already_read += rd;
}


--
Lev Walkin
vlm@lionet.info

Report this thread to moderator Post Follow-up to this message
Old Post
Lev Walkin
10-11-04 01:56 PM


Re: while loop for recv and send to get all data doesn't work
Alef T Veld <alef@xs2.xs4all.nl> writes:

> Hi,
>
> I'm trying to put recv and send in a while(1) loop to ensure i get all
> data from the client and from the server. Within the loop i check for <=0
> , if it occurs, i do a break.
>
> However, it does not work. It seems to keep sending data and never breaks!
>
> I use the following loop for send:
>
> while(1) {
> if(send(sockfd,buffer,strlen(buffer),0)<=0)
> break;
> }
>
> and for recv
>
> while(1) {
> if (recv(sockfd,buffer,sizeof(buffer),0)<=)
> break;
> }
> because recv gets the entire buffer, i terminate
> the buffer like this buffer[strlen(buffer)-1]='\0';
>
> Apart from the above problem, i also realize what would happen
> if the buffer did not get sent completetly,iwould get double
> data because the buffer would get sent twice.
>
> What am i doing wrong,

Here's how to do it (unless I've made some silly mistake):

#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>

size_t sendall(int fd, const void *buf, size_t len, int flags)
{
while(len > 0){
ssize_t r = send(s, buf, len, flags);
if(r >= 0){
buf += r;
len -= r;
} else if(errno != EINTR){
break;
}
}

return len;
}

This will send all the data in the buffer, if possible.  It returns
the number of bytes not sent.  The caller can examine errno to find
out what went wrong if this is not zero.

--
Måns Rullgård
mru@mru.ath.cx

Report this thread to moderator Post Follow-up to this message
Old Post
Måns Rullgård
10-11-04 01:56 PM


Re: while loop for recv and send to get all data doesn't work
Alef T Veld wrote:
> because recv gets the entire buffer, i terminate
> the buffer like this buffer[strlen(buffer)-1]='\0';
The other has told you what to do for send,
note that the above is total nonsense though, strlen
would expect a nul terminated string in the first place.
You need do do similar things on the recieve side as
on the send side, possible send the nul terminator as well and
check for that if you just send C strings, or send the
length first, and read until you got lenght bytes.

Report this thread to moderator Post Follow-up to this message
Old Post
Nils O. Selåsdal
10-11-04 09:00 PM


Re: while loop for recv and send to get all data doesn't work
Måns Rullgård <mru@mru.ath.cx> writes:

> Here's how to do it (unless I've made some silly mistake):

For receiving, just replace send() with recv().

--
Måns Rullgård
mru@mru.ath.cx

Report this thread to moderator Post Follow-up to this message
Old Post
Måns Rullgård
10-11-04 09:00 PM


Re: while loop for recv and send to get all data doesn't work

Alef T Veld wrote:
> Hi,
>
> I'm trying to put recv and send in a while(1) loop to ensure i get all
> data from the client and from the server. Within the loop i check for <=0
> , if it occurs, i do a break.
>
> However, it does not work. It seems to keep sending data and never breaks!
>
> I use the following loop for send:
>
> while(1) {
> if(send(sockfd,buffer,strlen(buffer),0)<=0)
> break;
> }
>
> and for recv
>
> while(1) {
> if (recv(sockfd,buffer,sizeof(buffer),0)<=)
> break;
> }
> because recv gets the entire buffer, i terminate
> the buffer like this buffer[strlen(buffer)-1]='\0';
>
> Apart from the above problem, i also realize what would happen
> if the buffer did not get sent completetly,iwould get double
> data because the buffer would get sent twice.
>
> What am i doing wrong,
> Alef

You are aware that your send loop has no condition for termination.  The
only time that it would quit sending is if you got an error, or the
system send buffer was temporarily full.  If you were trying to overcome
partial sends, then you have to do your own pointer and count control.

--

Fletcher Glenn


Report this thread to moderator Post Follow-up to this message
Old Post
Fletcher Glenn
10-11-04 09:00 PM


Re: while loop for recv and send to get all data doesn't work

>
> You are aware that your send loop has no condition for termination.  The
> only time that it would quit sending is if you got an error, or the
> system send buffer was temporarily full.  If you were trying to overcome
> partial sends, then you have to do your own pointer and count control.

I thought that it would break upon receiving 0, indicating the server has
closed connection or that there was no more data to send.

>
> --
>
> Fletcher Glenn

--
new

Report this thread to moderator Post Follow-up to this message
Old Post
atv
10-11-04 09:00 PM


Re: while loop for recv and send to get all data doesn't work

>
>
> Use this one:

I will, thanks, but why doesn't mine work? Should'nt it break on receiving
0 or -1?

>
> int already_read = 0;


Report this thread to moderator Post Follow-up to this message
Old Post
atv
10-11-04 09:00 PM


Re: while loop for recv and send to get all data doesn't work
"Bjorn Reese" <breese@see.signature> wrote:
 
>
> Minor point, but send() returns -1 on error, not some arbitrary
> negative number, so it may be more correct to use "r != -1".

Mmm, but that's not very defensive, is it? Imagine a broken socket lib whose
recv() sometimes returns -3. I wouldn't want to ignore this.




Report this thread to moderator Post Follow-up to this message
Old Post
Martin Carpenter
10-12-04 08:58 PM


Re: while loop for recv and send to get all data doesn't work
"Martin Carpenter" <mcarpenter@free.fr> writes:

> "Bjorn Reese" <breese@see.signature> wrote:
> 
>
> Mmm, but that's not very defensive, is it? Imagine a broken socket lib who
se
> recv() sometimes returns -3. I wouldn't want to ignore this.

And < 0 is quicker to type than == -1.

--
Måns Rullgård
mru@mru.ath.cx

Report this thread to moderator Post Follow-up to this message
Old Post
Måns Rullgård
10-12-04 08:58 PM


Sponsored Links




Last Thread Next Thread Next
Pages (2): [1] 2 »
Search this forum -> 
Post New Thread

Unix Programming archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 05:46 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.