recv() problem?

This is a discussion on recv() problem? within the Linux Networking forums, part of the Linux Forums category; Hi, I'm kind of new for networking programming and need to write some basic socket connection (in C) using ...


Go Back   Usenet Forums > Linux Forums > Linux Networking

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-08-2006
michal.shmueli@gmail.com
 
Posts: n/a
Default recv() problem?

Hi,
I'm kind of new for networking programming and need to write some
basic socket connection (in C) using 2 linux machines that do the
following:
The client sends request (query) to the server, then the server needs
to send the query results to the client. So I established the
connection, and the client seems to get the query correctly and send
the results back to the client. The only problem is that the client is
either getting part of the data- when I'm using:
/*********************************************
RECEIVES THE OUTPUT FROM THE SERVER
**********************************************/
strcpy(buf, "");
if ((numbytes=recv(sockfd, buf, MAX-1, 0)) == -1) {
error("recv");
exit(1);
}
buf[numbytes] = '\0';
printf("%s",buf);

or when I'm trying to use a while loop it got stuck forever...:

/*********************************************
RECEIVES THE OUTPUT FROM THE SERVER - loop
**********************************************/
strcpy(buf, "");
while(recv(sockfd, buf, MAX-1, 0) >0) {
printf("%s",buf);
}


as for the server, I'm using:
/**********************************
SENDS THE OUPUT
***********************************/
if (send(new_fd, rdata1, MAX, 0) == -1){
//error
}
close(new_fd);
exit(0);

what is wrong with this? How can I terminate the recv() in such a way
that it should stop after getting all the data back? Any help will be
appreciated.

Thanks.

Reply With Quote
  #2 (permalink)  
Old 01-09-2006
Hans-Juergen Lange
 
Posts: n/a
Default Re: recv() problem?

michal.shmueli@gmail.com wrote:
> Hi,
> I'm kind of new for networking programming and need to write some
> basic socket connection (in C) using 2 linux machines that do the
> following:
> The client sends request (query) to the server, then the server needs
> to send the query results to the client. So I established the
> connection, and the client seems to get the query correctly and send
> the results back to the client. The only problem is that the client is
> either getting part of the data- when I'm using:
> /*********************************************
> RECEIVES THE OUTPUT FROM THE SERVER
> **********************************************/
> strcpy(buf, "");
> if ((numbytes=recv(sockfd, buf, MAX-1, 0)) == -1) {
> error("recv");
> exit(1);
> }
> buf[numbytes] = '\0';
> printf("%s",buf);
>
> or when I'm trying to use a while loop it got stuck forever...:
>
> /*********************************************
> RECEIVES THE OUTPUT FROM THE SERVER - loop
> **********************************************/
> strcpy(buf, "");
> while(recv(sockfd, buf, MAX-1, 0) >0) {
> printf("%s",buf);
> }
>
>
> as for the server, I'm using:
> /**********************************
> SENDS THE OUPUT
> ***********************************/
> if (send(new_fd, rdata1, MAX, 0) == -1){
> //error
> }
> close(new_fd);
> exit(0);
>
> what is wrong with this? How can I terminate the recv() in such a way
> that it should stop after getting all the data back? Any help will be
> appreciated.
>
> Thanks.
>

select() or poll() is your friend.
Reply With Quote
  #3 (permalink)  
Old 01-09-2006
David Schwartz
 
Posts: n/a
Default Re: recv() problem?


"Hans-Juergen Lange" <Hans-Juergen.Lange@gmx.de> wrote in message
news:42eodcF1ie2flU1@uni-berlin.de...
> michal.shmueli@gmail.com wrote:


>> what is wrong with this? How can I terminate the recv() in such a way
>> that it should stop after getting all the data back? Any help will be
>> appreciated.
>>
>> Thanks.


> select() or poll() is your friend.


I'm afraid that won't help him. How will he know when to stop waiting
for a receive indication?

DS


Reply With Quote
  #4 (permalink)  
Old 01-09-2006
Hans-Juergen Lange
 
Posts: n/a
Default Re: recv() problem?

David Schwartz wrote:
> "Hans-Juergen Lange" <Hans-Juergen.Lange@gmx.de> wrote in message
> news:42eodcF1ie2flU1@uni-berlin.de...
>
>>michal.shmueli@gmail.com wrote:

>
>
>>>what is wrong with this? How can I terminate the recv() in such a way
>>>that it should stop after getting all the data back? Any help will be
>>>appreciated.
>>>
>>>Thanks.

>
>
>>select() or poll() is your friend.

>
>
> I'm afraid that won't help him. How will he know when to stop waiting
> for a receive indication?
>
> DS
>
>


Hello,

in the original post the server closes the communication after sending
the data. This is the indication on the client side for completion.
Getting parts of the data happens because the TCP/IP stack moves the
data to the socket as the frames come in. If a sending from the server
is spread over more than a frame it gets into the socket splitted.
Using select or poll gives you the ability to see what happens on the
socket, even errors, before you call the next function. If the socket
get closed by the server you may call closesocket not recv.
There are lots of advantages in using select and/or poll.
I could give him more advice what he should have an eye on but usage of
brain is a thing I prefer.

BR
Hans-Juergen Lange

Reply With Quote
  #5 (permalink)  
Old 01-09-2006
David Schwartz
 
Posts: n/a
Default Re: recv() problem?


"Hans-Juergen Lange" <Hans-Juergen.Lange@gmx.de> wrote in message
news:42fm62F1ia7hjU1@uni-berlin.de...

> in the original post the server closes the communication after sending the
> data.


The original post didn't say that, and I don't think it's true.

> This is the indication on the client side for completion.


No, the indiciation on the client side for completion is whatever the
protocol says it is. That's not a TCP issue, it's an issue for the
higher-level protocol.

> Getting parts of the data happens because the TCP/IP stack moves the data
> to the socket as the frames come in. If a sending from the server is
> spread over more than a frame it gets into the socket splitted.
> Using select or poll gives you the ability to see what happens on the
> socket, even errors, before you call the next function. If the socket get
> closed by the server you may call closesocket not recv.


Why would you want to discard valid data?

> There are lots of advantages in using select and/or poll.


I agree, but none of them have to do with the original poster's issue.

> I could give him more advice what he should have an eye on but usage of
> brain is a thing I prefer.


I could not agree more.

DS


Reply With Quote
  #6 (permalink)  
Old 01-10-2006
FLY135
 
Posts: n/a
Default Re: recv() problem?


David Schwartz wrote:
> "Hans-Juergen Lange" <Hans-Juergen.Lange@gmx.de> wrote in message
> news:42fm62F1ia7hjU1@uni-berlin.de...
>
> > in the original post the server closes the communication after sending the
> > data.

>
> The original post didn't say that, and I don't think it's true.
>
> > This is the indication on the client side for completion.

>
> No, the indiciation on the client side for completion is whatever the
> protocol says it is. That's not a TCP issue, it's an issue for the
> higher-level protocol.


I use a timeout on my recv because in my application there is no way to
know when the connection will be closed (or simply disappear). Loss of
connectivity or device reboot (it's embedded) may cause a loss of
connection. You can send some dummy data back as a check to see if the
connection is still alive after a timeout.

In a way it's funny that for as long as TCP has been used the most
common problems are still left for the user to discover and solve over
and over.

> > There are lots of advantages in using select and/or poll.

>
> I agree, but none of them have to do with the original poster's issue.


Other than seeming to be only able to interact by reposting the same
question over and over, nobody really knows what really is the OP's
issue.

Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 04:55 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0