recv returns with 0
I have a strange problem with TCP sockets. I have a client which connects to the server and sends some 8-byte string message. the send() function returns value 8. Now on the server side when the recv() function unblocks it returns 0, meaning the client has disconnected. But that is not the case since the client loops and no close() or shutdown() was called.
I've also checked the packets with sharkwire and it turns out there was no error at all. There's the 3-way handshake upon connection and then the PSH packet from the client. After that the server responses with ACK and that's that. I made a syscall trace but between connect() and recvfrom() (or sendto() on the client side) there is no single call to close() on the socket.
What could be wrong?
The code is too large to post. With syscall trace I can assume a simple tcp server/client code can be taken. I'm not doing anything special witch the code. Just initialization, connection and sending/recieving. All other functions return without errors(socket(), connect(), bind(), accept(), listen()). The problem is just with the recv() on the server side. It should not return 0 because I'm not calling close() anywhere.
You sure you couldn't trim it down? You could attach it or post it up on a file sharing site.
It's kind of hard to figure it out without the code.
I'm afraid trimming down the source takes too much time. But like I said the could could look as a simple tcp server/client.
I've made some more packet inspection and found out that after the server ACK-s the receiving packet, it then sends a RST,ACK packet back to the client and terminates the connection.
Are you passing any values for the 'flags' ?
What is the value of errno when it returns 0?
Is the socket non-blocking?
> I'm afraid trimming down the source takes too much time.
How big was the code before you noticed this problem?
If it's just appeared, then look at the differences with the previous version.
If you don't have a previous version, consider this to be a good lesson in the value of a source control system which always allows you to get back to a "known good state".
The sockets are blocking. When receiving, I pass MSG_WAITALL to recv(), the send() flag is 0. The errno value is 2.
Originally Posted by Salem
> Is the socket non-blocking?
I thought this too, but the man page said it would return -1 and set errno to EAGAIN on nonblock, not return 0.
Well there are several "modules" that use the same network library for network IO. The strange thing is while other modules work, it's only when I began writing a new module this error appeared.
Is the code multithreaded? Do you have well defined boundaries for how to handle networking code across threads and/or "modules"?
> The errno value is 2.
You have to find out what error that corresponds to on your system. When did it get set?
It's "No such file or directory" :)
Originally Posted by robwhit
When did it get set? Keep in mind that errno values stick around until you clear them.
Does the firewall on the server allow you to connect to that port with that address?
It's likely that no error codes are set since a return value of 0 basically means the connection was 'gracefully' closed at the other end. Try calling recv without the MSG_WAITALL (or any) flag set. Also, if the network library is being used in a multithreaded context, make sure there aren't any globals/statics being thrashed. Also, have you tried running the server on a different machine to see if the results were the same?