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 de
To correct numerous misstatements in the existing answers:
- Does this mean that the socket i was using until now becomes invalid.
No. It means the peer has closed the connection, or shut it down for output from his end. Your socket is still valid. You can call recv()
again, but all you will get is another zero. You can call send()
on it too, and if the peer has only shutdown the connection for output the data will be sent.
- Do I get any special message like null character or something when this disconnect happens.
No, you get zero return value from recv()
. That's what it's for. It is delivered out-of-band, not in the data buffer.
- If the socket connection i was having became invalid then why doesnt the
recv()
socket function throw
Because it's a C API, and there are no throws
in C, or in Unix system calls either.
and SOCKET_ERROR.
Because it isn't an error.
Instead why do i receive data of 0 length.
You don't 'receive data of 0 length'. You receive a return value of zero instead of data.
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
.