recv() problem, need help!!

This is a discussion on recv() problem, need help!! 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, need help!!

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
FLY135
 
Posts: n/a
Default Re: recv() problem, need help!!

TCP is streaming so there is no guarantee that the print statement will
print out packets the same way that you sent them. You shouldn't lose
any data, but you might find the lines broken differently.

Reply With Quote
  #3 (permalink)  
Old 04-06-2006
jmeissen@aracnet.com
 
Posts: n/a
Default Re: recv() problem, need help!!

In article <1136845453.689810.324220@g47g2000cwa.googlegroups .com>,
FLY135 <fly_135@hotmail.com> wrote:
>TCP is streaming so there is no guarantee that the print statement will
>print out packets the same way that you sent them. You shouldn't lose
>any data, but you might find the lines broken differently.
>


There is so much wrong with that statement that you were better off
keeping silent. What you probably meant was that it's a buffered
transfer with a block size that might not match the original request, so
received data might not arrive in a single block.

As for the original code,... <sigh> The "loop" example assumes the
recv() request will be satisfied by a null-terminated string. Bad, bad
bad. And please,.. "buffer[0] = '\0'; is SO much better than using
"strcpy(buffer,"");".

--
John Meissen jmeissen@aracnet.com
Reply With Quote
  #4 (permalink)  
Old 04-06-2006
Rick Jones
 
Posts: n/a
Default Re: recv() problem, need help!!

jmeissen@aracnet.com wrote:
> In article <1136845453.689810.324220@g47g2000cwa.googlegroups .com>,
> FLY135 <fly_135@hotmail.com> wrote:
>>TCP is streaming so there is no guarantee that the print statement will
>>print out packets the same way that you sent them. You shouldn't lose
>>any data, but you might find the lines broken differently.
>>


> There is so much wrong with that statement that you were better off
> keeping silent. What you probably meant was that it's a buffered
> transfer with a block size that might not match the original request, so
> received data might not arrive in a single block.


However, even if the TCP segment size (aka block) does match the
original request, and one could get lucky, an application programmer
should never work to that, and should indeed treat a TCP connection as
a byte stream and utterly ignore the implementation detail that TCP
sends data in segments. It is quite correct to describe TCP as
providing a byte-stream semantic, and I suspect that is what was meant
to be conveyed by "TCP is streaming"

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
  #5 (permalink)  
Old 04-10-2006
steve_schefter@hotmail.com
 
Posts: n/a
Default Re: recv() problem, need help!!

jmeissen@aracnet.com wrote:
> In article <1136845453.689810.324220@g47g2000cwa.googlegroups .com>,
> FLY135 <fly_135@hotmail.com> wrote:
> >TCP is streaming so there is no guarantee that the print statement will
> >print out packets the same way that you sent them. You shouldn't lose
> >any data, but you might find the lines broken differently.
> >

>
> There is so much wrong with that statement that you were better off
> keeping silent. What you probably meant was that it's a buffered
> transfer with a block size that might not match the original request, so
> received data might not arrive in a single block.


What's incorrect about FLY135's statement? TCP is indeed streaming.

Steve

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
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 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 02:18 AM.


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