Networking Forums

Networking Forums > Computer Networking > Linux Networking > recv() problem?

Reply
Thread Tools Display Modes

recv() problem?

 
 
michal.shmueli@gmail.com
Guest
Posts: n/a

 
      01-08-2006, 05:28 AM
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
 
 
 
 
prg
Guest
Posts: n/a

 
      01-09-2006, 10:03 PM

(E-Mail Removed) 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.


I'm _sure_ there are others around here with more experience programing
these sorts of things in C than I am. I don't need to code at this
level, but I do need need to refresh my memory from time to time.

Not seeing the context of the code you provided leaves me clueless --
see, I told you I don't code this stuff in C ;-)

But you may find something useful here:
Beej's Guide to Network Programming Using Internet Sockets
http://beej.us/guide/bgnet/output/html/advanced.html

hth,
prg

 
Reply With Quote
 
Steve Horsley
Guest
Posts: n/a

 
      01-10-2006, 10:02 AM
(E-Mail Removed) 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.
>


Well, it looks like you signify the end of the server response by
closing the connection, so this is what you should look for in
your client - keep reading from the socket until recv() returns
-1, at which point the connection is closed and you have all of
the response.

I'm rusty at C, but something like:
while (true) {
strcpy(buf, "");
numbytes=recv(sockfd, buf, MAX-1, 0)
if (numbytes == -1) {
break;
}
buf[numbytes] = '\0';
printf("%s", buf)
}

Don't use printf if your response might contain binary data
(including NULLs).

You might consider changing the server to send some kind of
end-of-message marker that you can look for instead if you are
sending text, for example, a blank line.

Steve
 
Reply With Quote
 
David Schwartz
Guest
Posts: n/a

 
      01-10-2006, 06:18 PM

"Steve Horsley" <(E-Mail Removed)> wrote in message
news:dq0478$mbk$(E-Mail Removed)...
> (E-Mail Removed) wrote:


> Well, it looks like you signify the end of the server response by closing
> the connection,


Where did you get that from? It didn't look like that to me at all.
Nothing the OP said suggested this.

DS


 
Reply With Quote
 
Steve Horsley
Guest
Posts: n/a

 
      01-10-2006, 06:47 PM
David Schwartz wrote:
> "Steve Horsley" <(E-Mail Removed)> wrote in message
> news:dq0478$mbk$(E-Mail Removed)...
>> (E-Mail Removed) wrote:

>
>> Well, it looks like you signify the end of the server response by closing
>> the connection,

>
> Where did you get that from? It didn't look like that to me at all.
> Nothing the OP said suggested this.
>
> DS
>
>

From here:
/**********************************
SENDS THE OUPUT
***********************************/
if (send(new_fd, rdata1, MAX, 0) == -1){
//error
}
close(new_fd);
exit(0);

I thought that either close() or exit() would do the trick.
Am I so rusty that I'm missing something?

Steve
 
Reply With Quote
 
Rick Jones
Guest
Posts: n/a

 
      01-10-2006, 08:54 PM
Steve Horsley <(E-Mail Removed)> wrote:
> David Schwartz wrote:
>> "Steve Horsley" <(E-Mail Removed)> wrote in message
>> news:dq0478$mbk$(E-Mail Removed)...
>>> (E-Mail Removed) wrote:

>>
>>> Well, it looks like you signify the end of the server response by closing
>>> the connection,

>>
>> Where did you get that from? It didn't look like that to me at all.
>> Nothing the OP said suggested this.


> From here:
> /**********************************
> SENDS THE OUPUT
> ***********************************/
> if (send(new_fd, rdata1, MAX, 0) == -1){
> //error
> }
> close(new_fd);
> exit(0);


> I thought that either close() or exit() would do the trick.
> Am I so rusty that I'm missing something?


Nope - that should indeed cause the remote to get a read return of
zero, indicating close of connection and, by implication, end of
response. Of course, it isn't a "perfect" end of response indication
since one could get a read return of zero if the server application
crashed

rick jones
--
firebug n, the idiot who tosses a lit cigarette out his car window
these opinions are mine, all mine; HP might not want them anyway...
feel free to post, OR email to rick.jones2 in hp.com but NOT BOTH...
 
Reply With Quote
 
David Schwartz
Guest
Posts: n/a

 
      01-10-2006, 10:23 PM

"Steve Horsley" <(E-Mail Removed)> wrote in message
news:dq12vi$f9$(E-Mail Removed)...

> I thought that either close() or exit() would do the trick.
> Am I so rusty that I'm missing something?


No, I am losing my mind. Sorry. You are correct.

DS


 
Reply With Quote
 
FLY135
Guest
Posts: n/a

 
      01-11-2006, 02:14 PM

Rick Jones wrote:
> Nope - that should indeed cause the remote to get a read return of
> zero, indicating close of connection and, by implication, end of
> response. Of course, it isn't a "perfect" end of response indication
> since one could get a read return of zero if the server application
> crashed


My experience is that if the server just "disappears" (i.e. crashes)
that the recv routine is hung waiting for data. Perhaps setting the
"keepalive" option will alleviate that. But if you need a fast
resolution to that problem you can implement a timeout and check the
connection with a send.

 
Reply With Quote
 
David Schwartz
Guest
Posts: n/a

 
      01-11-2006, 08:14 PM

"FLY135" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...

> Rick Jones wrote:
>> Nope - that should indeed cause the remote to get a read return of
>> zero, indicating close of connection and, by implication, end of
>> response. Of course, it isn't a "perfect" end of response indication
>> since one could get a read return of zero if the server application
>> crashed


> My experience is that if the server just "disappears" (i.e. crashes)
> that the recv routine is hung waiting for data. Perhaps setting the
> "keepalive" option will alleviate that. But if you need a fast
> resolution to that problem you can implement a timeout and check the
> connection with a send.


He wasn't talking about the server crashing. He was talking about the
server application crashing.

DS


 
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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
recv() problem, need help!! michal.shmueli@gmail.com Linux Networking 4 04-10-2006 04:33 PM
recv() problem? michal.shmueli@gmail.com Linux Networking 5 01-10-2006 01:55 PM
recv() problem? michal.shmueli@gmail.com Linux Networking 1 01-09-2006 04:33 AM
recv() problem? michal.shmueli@gmail.com Linux Networking 0 01-08-2006 07:55 AM
recv() problem?- need help michal.shmueli@gmail.com Linux Networking 0 01-08-2006 06:36 AM



1 2 3 4 5 6 7 8 9 10 11