How to abort socket's BeginReceive()?

后端 未结 7 2062
夕颜
夕颜 2020-12-05 06:17

Naturally, BeginReceive() will never end if there\'s no data. MSDN suggests that calling Close() would abort BeginReceive().

7条回答
  •  情歌与酒
    2020-12-05 07:07

    For TCP socket connections, you can use the Connected property to determine the state of the socket before trying to access any disposed methods. Per MSDN:

    "The Connected property gets the connection state of the Socket as of the last I/O operation. When it returns false, the Socket was either never connected, or is no longer connected."

    Since it says "no longer connected" it implies that a Close() was previously called on the socket. If you check whether the socket is Connected at the start of the receive callback, there will be no exception.

提交回复
热议问题