gpt4 book ai didi

c# - Socket.EndRead 0字节意味着断开连接?

转载 作者:太空狗 更新时间:2023-10-30 00:36:35 28 4
gpt4 key购买 nike

我想知道在我的 C# 异步套接字中,在 EndRead 调用中接收到 0 字节是否意味着服务器实际上已经断开了我们的连接?

我看到的许多例子都表明是这种情况,但我收到的断开连接频率比我预期的要高得多。

这段代码是否正确?或者 endResult <= 0 对连接状态没有任何意义?

private void socket_EndRead(IAsyncResult asyncResult)
{
//Get the socket from the result state
Socket socket = asyncResult.AsyncState as Socket;

//End the read
int endResult = Socket.EndRead(asyncResult);

if (endResult > 0)
{
//Do something with the data here


}
else
{
//Server closed connection?
}
}

最佳答案

From the docs :

If the remote host shuts down the Socket connection and all available data has been received, the EndRead method completes immediately and returns zero bytes.

是的,零字节表示远程关闭。

关于c# - Socket.EndRead 0字节意味着断开连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1395026/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com