我正在使用“ Close_Waits”挂起的TCP服务器。我需要一个示例程序来处理来自客户端的持久连接。
我尝试捕获异常,然后关闭捕获中的套接字。但是没有运气!
protected virtual void ReceiveCallBack(IAsyncResult result)
{
var wrapper = (ConnectedSocketWrapper)result.AsyncState;
try
{
int bytesRead = 0;
bytesRead = wrapper.ConnectedSocket.EndReceive(result);
LogInfo("Byte Read" + bytesRead.ToString());
if (bytesRead > 0)
{
LogInfo("inside Byte Read > 0 : " + bytesRead.ToString());
byte[] dataRead = wrapper.Buffer.Take(bytesRead).ToArray();
wrapper.ConnectedSocket.BeginReceive(wrapper.Buffer, 0, this.configuration.BytesToReceiveInReadOperation, SocketFlags.None, this.ReceiveCallBack, wrapper);
DataReceivedByServer dataReceived = new DataReceivedByServer(wrapper.Token, dataRead);
EventDispatcher.RaiseEvent(dataReceived);
if (DataReceived != null)
{
DataReceived(this, new DataReceivedEventArgs(dataRead, wrapper.Token));
}
}
else
{
EventDispatcher.RaiseEvent(new ClientDisconnected(wrapper.Token, "Read 0 bytes (client disconnected gracefully)", wrapper.ConnectedSocket.RemoteEndPoint.ToString()));
LogInfo("inside Byte Read = 0 : " + bytesRead.ToString());
Close(wrapper.ConnectedSocket);
Socket removeSocket;
connectedSockets.TryRemove(wrapper.Token, out removeSocket);
}
}
catch
{
Close(wrapper.ConnectedSocket);
throw;
}
}
我需要有一台挂起的TCP服务器来无缝处理请求。
答案 0 :(得分:0)
如果您是我,我将检查代码和/或使用此simplsockets,它非常可读。
还要检查这一个MultiProtocolAspNetCore,这是关于如何在TCP
的顶部构建ASP.NET Core
服务器。
也请阅读这份很棒的TCP/IP .NET Socket FAQ。