gpt4 book ai didi

c# - 将 SocketAsyncEventArgs 与 ReceiveAsync 重用会导致无限循环

转载 作者:太空狗 更新时间:2023-10-30 00:59:05 27 4
gpt4 key购买 nike

我正在编写一个只使用一个套接字的简单套接字客户端。所以我想我可以为所有接收操作重用一个 SocketAsyncEventArgs。只要连接了 socket ,它就可以正常工作。当服务器断开连接时,客户端将进入无限接收循环,在控制台中发送垃圾邮件“接收成功”。

当套接字断开连接时,e.SocketError != SocketError.Success 不应该为真吗?

部分代码如下:

private void Completed(object sender, SocketAsyncEventArgs e)
{
if (e.SocketError != SocketError.Success)
status = 0;

System.Console.WriteLine(e.LastOperation + " " + e.SocketError);

if (status == 1)
{
switch (e.LastOperation)
{
case SocketAsyncOperation.Connect:
ProcessConnect(e);
break;
case SocketAsyncOperation.Receive:
ProcessReceive(e);
break;
case SocketAsyncOperation.Send:
ProcessSend(e);
break;
default:
status = 0;
break;
}
}

if (status != 1)
CloseSocket();
}

private void ProcessReceive(SocketAsyncEventArgs e)
{
if (!socket.ReceiveAsync(e))
Completed(null, e);
}

最佳答案

如果你的套接字是为字节流设置的,那么你可能会得到一个零字节的回调,这表明套接字已经正常关闭并且不会再读取更多字节。只需在您的代码中检查这一点,例如

private void ProcessReceive( SocketAsyncEventArgs e )
{
AsyncUserToken token = e.UserToken as AsyncUserToken;
if( e.ByteTransferred > 0 && e.SocketError == SocketError.Success )
{
// .. process normally ..
}
else
{
CloseClientSocket( e );
}
}

private void CloseClientSocket( SocketAsyncEventArgs e )
{
AsyncUserToken token = e.UserToken as AsyncUserToken;
try
{
token.Socket.Shutdown( SocketShutdown.Send );
}
// throws error if it's already closed
catch( Exception ) {}
token.Socket.Close();
}

当然,您还需要阻止您的客户端尝试使用套接字,但我相信您可以解决这个问题。

关于c# - 将 SocketAsyncEventArgs 与 ReceiveAsync 重用会导致无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1366427/

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