How can I check whether a (TCP) socket is (dis)connected in C#?

后端 未结 4 995
南笙
南笙 2020-12-04 10:19

How should I check a (TCP) socket to find out whether it is connected?

I have read about the Socket.Connected property in MSDN, but it says it only show

4条回答
  •  春和景丽
    2020-12-04 10:57

    I don't really understand his statement about calling Receive() to make sure that the remote endpoint has actually received all the data I sent.

    The post by @PeteDuniho isn't about establishing the state of the connection, it is about terminating the connection in such a way that you know when the peer has received all your data.

    (Do sockets block receiving until the sending buffer is empty?)

    No, but if you shutdown the socket and then read until EOS, you are waiting for the peer to read all the data until he gets EOS and then closes the socket. So you have a guarantee that all the data has got into the peer application.

提交回复
热议问题