Networking
cancel
Showing results for 
Search instead for 
Did you mean: 

Problem about reading data on a TCP socket

Guilhaumou Vincent2
Occasional Contributor

Problem about reading data on a TCP socket

I support an application compiled under HP10.20 and running on B2000 station under HP11.11, which is an HMI displaying data received from other equipments. This application is composed with several processes which one is dedicated to the communication between HMI processes and the application running under other equipments. The communication in this process is based on sockets and pipes : pipe with the HMI processes, socket UDP to manage the detection of clients, sockets TCP to receive and to send data from/to clients of other equipments when the connexion is established with them.
I think that the managment of the communications is classic :
- loop calling the select function to wait data from all the file descriptors of the sockets and pipes added through the FD_SET macro :
- check the return of the select function
- if the return of select function > 0, detection of the file descriptor concerned through the FD_ISSET macro
- call recv function on the file descriptor concerned
Code extract :
sel=0;
selT.tv_sec=5;
selT.tv_usec=0;
while (sel == 0)
{
sel = select(NbFd, &fdr, &fdw, &fde, &selT);
}
...
if (sel>0)
{
...
if (FD_ISSET(sock, &fdr)
ret = recv(sock, ...
Problem is that sometimes, when there is much traffic, the return of the select function (sel) is >0, the file descriptor detected is a TCP socket, but the recv function on this TCP socket returns 0 (ret). Our application treats that as a disconnexion !
How may you explain that the select function detects data on a socket (return>0) and that the recv function on this socket is unable to read the data (return=0)?
2 REPLIES
Matti_Kurkela
Honored Contributor

Re: Problem about reading data on a TCP socket

From "man 2 recv":
--------
+ If the remote side of a connection-based socket has performed an orderly shutdown and there is no more data to read (the socket has reached the end of its data stream), recv() returns 0.
--------

So treating it as a disconnection *is* exactly the right thing to do.

select() indicates the socket has received something... and the result of 0 from recv() indicates that the "something" was an end-of-stream indication.

MK
MK
Guilhaumou Vincent2
Occasional Contributor

Re: Problem about reading data on a TCP socket

OK thanks, but for the moment, nothing could explain why the equipment connected at the other side of the socket would close the socket ...
Is it the unique possible cause of the described effects, in a context of important traffic ?