我在这个主题上找不到多少,但是最近我使用SocketAsyncEventArgs对象实现了一个套接字服务器。我遵循了以下示例:代码工程示例
我修改了数据处理类以满足我的需要,并且没有接触到多少(如果有的话)套接字处理部分。
当我在我的机器(win7)上本地测试我的服务时,它似乎运行良好,一切都很好。在windows server 2008上运行它时也是如此。在windows server 2008上运行时,mem使用量在50k(从任务管理器中查看)中徘徊。
当我在windows 2003上运行该服务时,所有.net更新/补丁都通过.net 4进行,我得到一个错误:
Safe handle has been closed
at System.Net.Sockets.Socket.AcceptAsync(SocketAsyncEventArgs e)
at SocketAsyncServer.SocketListener.StartAccept() in 289
at SocketAsyncServer.SocketListener.ProcessAccept(SocketAsyncEventArgs acceptEventArgs) 367
at SocketAsyncServer.SocketListener.AcceptEventArg_Completed(Object sender, SocketAsyncEventArgs e) in 326
at System.Net.Sockets.SocketAsyncEventArgs.OnCompleted(SocketAsyncEventArgs e)
at System.Net.Sockets.SocketAsyncEventArgs.ExecutionCallback(Object ignored)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Net.Sockets.SocketAsyncEventArgs.FinishOperationSuccess(SocketError socketError, Int32 bytesTransferred, SocketFlags flags)
at System.Net.Sockets.SocketAsyncEventArgs.CompletionPortCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped)
at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)这涉及的代码行是:第326行。
private void AcceptEventArg_Completed(object sender, SocketAsyncEventArgs e)
{
//Any code that you put in this method will NOT be called if
//the operation completes synchronously, which will probably happen when
//there is some kind of socket error. It might be better to put the code
//in the ProcessAccept method.
ProcessAccept(e);
}第289项
bool willRaiseEvent = listenSocket.AcceptAsync(acceptEventArg);此服务器与远程客户端连接以发送数据(小型遥测有效载荷),在该服务器中,我们解析数据并发送回响应,并在套接字连接时继续此操作。
我愿意接受建议或尝试的事情。另一个注意事项是,当我在win2k3上运行此服务时,mem使用天空火箭将600+Kb与服务器2008上的50-60Kb范围进行比较,并引发上述错误。
发布于 2013-12-05 14:41:29
在阅读了提供给示例的链接中的多页评论后,我发现了这个问题。
在SocketListener类中,有一个名为"HandleBadAccept“的方法:
private void HandleBadAccept(SocketAsyncEventArgs acceptEventArgs)
{
var acceptOpToken = (acceptEventArgs.UserToken as AcceptOpUserToken);
//This method closes the socket and releases all resources, both
//managed and unmanaged. It internally calls Dispose.
acceptEventArgs.AcceptSocket.Close();
acceptEventArgs.AcceptSocket = null; //added to handle win2k3 issue
//Put the SAEA back in the pool.
poolOfAcceptEventArgs.Push(acceptEventArgs);
}我不得不加上
acceptEventArgs.AcceptSocket = null;它现在在windows 2003机器上运行良好。
https://stackoverflow.com/questions/20387819
复制相似问题