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 ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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. |
|
|||
|
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. |
|
|||
|
"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 |
|
|||
|
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 |
|
|||
|
"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 |
|
|||
|
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. |
![]() |
| Thread Tools | |
| Display Modes | |
|
|