Code Comments
Programming Forum and web based access to our favorite programming groups.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
Post Follow-up to this messageAlef 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
Post Follow-up to this messageAlef 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
Post Follow-up to this messageAlef 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.
Post Follow-up to this messageMå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
Post Follow-up to this message
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
Post Follow-up to this message> > 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
Post Follow-up to this message> > > 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;
Post Follow-up to this message"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.
Post Follow-up to this message"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
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.