Problem with TCP/IP Sockets read returning 0's - Redhat 7.2 - glibc

Problem with TCP/IP Sockets read returning 0's - Redhat 7.2 - glibc

Post by Jim Lamber » Wed, 22 Jan 2003 04:34:24

(Originally posted to comp.os.linux.development)

Hello All,

I have an application that reads and writes from TCP/IP sockets. I know that
according to the spec, TCP/IP sockets should be 100% guaranteed. That is, if
I write "hello world" to one side of the socket, I should be able to always
read "hello world" from the receiving side of the socket.

What we are seeing is that occasionally a read of the socket will return the
correct size of bytes (ie. 11 as in the "hello world" example), but the data
will be all hex 0's. We prefill the receiving data area with bogus
characters and immediately check it after the read and it will be all 0's so
we know the received data is 0's. Now it doesn't do this all the time, just
occasionally and usually only when we are doing a lot of writes on the
socket with no time delays between the writes.

If we insert a delay of some time, either a sleep() or usleep(), etc., then
the 0's go away. It is almost as if we are overrunning the buffer for the
socket. I just thought with TCP/IP that this was impossible.  The
application receives messages from client programs and is relaying them
using TCP/IP sockets to another program. It is trying to relay them as fast
as possible, ie. write them out one after the other on the socket.

As the subject indicates, this is on Redhat 7.2 using the glibc libraries
and gcc as the compiler. The computers using the sockets are on the same
network hub.  If it is a buffer problem, shouldn't the write() return an
error indicating to us that we are overflowing the network buffer? We tried
doing read() and recv() on the receiving end and the problem appeared with
both functions.

Any help would be appreciated.



Problem with TCP/IP Sockets read returning 0's - Redhat 7.2 - glibc

Post by Kasper Dupon » Wed, 22 Jan 2003 06:33:33

I doubt this is a bug in the TCP implementation. Most likely it is a
bug in your program. Are you sure you always call read and write with
a correct pointer for the buffer? Are you sure you always call with
the correct size? And are you sure you check the return code from the
read and write calls. They might read or write less bytes than

And btw. I never use send or recv on TCP sockets. I don't see much
reason for that, just stick with the simpler read and write functions
which work just fine on TCP sockets.

Kasper Dupont -- der bruger for meget tid p? usenet.



1. TCP-IP socket: read function returns 0

My problem is:

1) I open a server socket on host A;
2) I open a client socket on host B;
3) They open communication but..
4) sometimes my socket is locked by the read function which
   reads a 0-value (= 0 bytes read).

How can I unlock the socket? Can I refuse 0-byte connections?
At the moment I close the socket if read function returns 0.

Thank you.


Sent via
Share what you know. Learn what you don't.

2. Your thoughts on Portsentry

3. can't start nfsd with tcp on Redhat 7.2

4. Cyclades Card

5. Problem with read(2) and sockets ? (TCP/IP)

6. ${1+"$@"}

7. Can't see created sockets with netstat in RedHat 7.2 (newbie)

8. How to list network interfaces?

9. Linux 'read' on socket returns connection refused?

10. RedHat 7.1 and 7.2 can't read partition table

11. TCP/IP problems with Mandrake 7.2 (C++)

12. Odd TCP/IP socket 'drops' characters

13. POSIX semaphores and Kernel 2.4.7, glibc 2.2.4 (RedHat 7.2)