recv() socket function returning data with length as 0

后端 未结 8 2065
甜味超标
甜味超标 2020-12-24 07:55

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

相关标签:
8条回答
  • 2020-12-24 08:41

    To correct numerous misstatements in the existing answers:

    1. 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.

    1. 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.

    1. 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.

    0 讨论(0)
  • 2020-12-24 08:48

    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.

    0 讨论(0)
提交回复
热议问题