Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

recv() socket function returning data with length as 0

Tags:

I am having an application which established a socket connection on a port number 5005 with another device(hardware device with a web server).

Now if my hardware device disconnects then i loose connection with the device.

  1. Does this mean that the socket i was using until now becomes invalid.

  2. Do i get any special message like null character or something when this disconnect happens.

  3. If the socket connection i was having became invalid then why doesnt the recv() socket function throw and SOCKET_ERROR. Instead why do i receive data of 0 length.

Thanks

like image 232
ckv Avatar asked Jun 22 '10 07:06

ckv


People also ask

What does it mean when recv returns 0?

Upon successful completion, recv() returns the length of the message in bytes. If no messages are available to be received and the peer has performed an orderly shutdown, recv() returns 0. Otherwise, -1 is returned and errno is set to indicate the error.

What does recv () return in C?

If successful, recv() returns the length of the message or datagram in bytes. The value 0 indicates the connection is closed. If unsuccessful, recv() returns -1 and sets errno to one of the following values: Error Code.

What does RECV return when there is still data in the buffer but the other side has closed the socket?

If no error occurs, recv returns the number of bytes received and the buffer pointed to by the buf parameter will contain this data received. If the connection has been gracefully closed, the return value is zero.

What does socket recv return Python?

The recv method receives up to buffersize bytes from the socket. When no data is available, it blocks until at least one byte is available or until the remote end is closed. When the remote end is closed and all data is read, it returns an empty byte string.


2 Answers

When recv returns a value of 0 that means the connection has been closed.

See the recv man page:

These calls return the number of bytes received, or -1 if an error occurred. The return value will be 0 when the peer has performed an orderly shutdown.

In answer to question #1, yes the socket is now invalid. You must create a new socket and connection for further communications.

Edit

Now as valdo pointed out below, there is also the possibility of having a half-closed TCP connection in which you can't receive any more but you can keep writing to the socket until you've finished sending your data. See this article for more details: TCP Half-Close. It doesn't sound like you have this situation though.

In answer to question #2, there are basically two ways to detect a closed socket. This assumes that the socket went through an orderly shutdown, meaning the peer called either shutdown or close.

The first method is to read from the socket in which case you get a return value of 0. The other method is to write to the socket, which will cause the SIG_PIPE signal to be thrown indicating a broken pipe.

In order to avoid the signal, you can set the MSG_NOSIGNAL socket option in which case send would return -1 and set errno to EPIPE.

like image 171
Robert S. Barnes Avatar answered Oct 05 '22 23:10

Robert S. Barnes


Agree with Robert S. Barnes. Except the claim that the socket is now "invalid".

It's still valid. You can use it. You can even send data to the peer. The only thing you can't do with it is call recv.

like image 21
valdo Avatar answered Oct 06 '22 01:10

valdo