.Net套接字-10054错误
我们有一个windows服务,已经在生产中愉快地运行了一年多。就在最近,它开始制造麻烦。我没有任何套接字编程的经验,但我必须尝试找出问题的原因-太棒了 该服务安装在中央服务器上。它“监听”来自全国各地商店中大约400台服务器的数据 启动时,该服务将获取它需要从中收集数据的存储列表,以及存储服务器的IP地址。然后,它在列表中循环并执行以下代码:.Net套接字-10054错误,.net,sockets,udp,.net,Sockets,Udp,我们有一个windows服务,已经在生产中愉快地运行了一年多。就在最近,它开始制造麻烦。我没有任何套接字编程的经验,但我必须尝试找出问题的原因-太棒了 该服务安装在中央服务器上。它“监听”来自全国各地商店中大约400台服务器的数据 启动时,该服务将获取它需要从中收集数据的存储列表,以及存储服务器的IP地址。然后,它在列表中循环并执行以下代码: IPEndPoint lep = new IPEndPoint(lipa.AddressList[lipa.AddressList.GetUpperBou
IPEndPoint lep = new IPEndPoint(lipa.AddressList[lipa.AddressList.GetUpperBound(0)], (int)portNumber);
PosListener posListener = new PosListener(lep,this.pendingBacklog,storeTable,messageList);
PosListner的构造函数如下所示:
internal PosListener(IPEndPoint lep, int pendingBacklog, Hashtable storeTable, ArrayList messageList) : base(lep.Address.AddressFamily,SocketType.Stream,ProtocolType.Tcp)
{
ITraceState trState = PosApplication.Trace.StartProc("PosListener");
try
{
// Setup listener
this.ngcIPAddress = lep.Address.ToString();
this.ngcPort = lep.Port;
this.storeTable = storeTable;
this.storeLock = new ReaderWriterLock();
this.messageList = messageList;
this.messageLock = new ReaderWriterLock();
this.handlerList = new ArrayList();
this.handlerLock = new ReaderWriterLock();
this.asyncCallback = new AsyncCallback(this.CallbackAccept);
this.Bind(lep);
this.Listen(pendingBacklog);
// Start listening
PosApplication.PosSocketsEventLog.WriteEntry("Starting Listener on NGC Port "+this.ngcIPAddress+":"+this.ngcPort);
PosApplication.Trace.WriteDebug("Starting Listener on NGC Port "+this.ngcIPAddress+":"+this.ngcPort);
this.BeginAccept(this.asyncCallback,null);
}
catch (Exception e)
{
ExceptionManager.Publish(e);
}
finally
{
trState.EndProc();
}
}
Socket connection = this.EndAccept(ar);
我的理解是,构造函数注册CallbackAccept方法,以便在被监视的套接字上发现流量时执行
此回调方法粘贴在下面:
private void CallbackAccept(IAsyncResult ar)
{
ITraceState trState = PosApplication.Trace.StartProc("CallbackAccept");
try
{
// Get the socket that handles the client connection
Socket connection = this.EndAccept(ar);
// Start listening for the next client connection
this.BeginAccept(this.asyncCallback,null);
string storeIPAddress = ((IPEndPoint)connection.RemoteEndPoint).Address.ToString();
int storePort = ((IPEndPoint)connection.RemoteEndPoint).Port;
PosApplication.Trace.WriteDebug("Listener "+this.ngcIPAddress+":"+this.ngcPort+" received connection request from "+storeIPAddress+":"+storePort);
// Check the remote end point has a recognised Store IP Address
this.storeLock.AcquireReaderLock(-1);
bool isAcceptable;
try
{
isAcceptable = this.storeTable.Contains(storeIPAddress);
}
finally
{
this.storeLock.ReleaseReaderLock();
}
// Close connection and throw exception if the remote end point
// does not have a recognised Store IP Address
if (!isAcceptable)
{
connection.Shutdown(SocketShutdown.Both);
connection.Close();
CrmServiceException ce = new CrmServiceException(
"Client",
"ConfigurationError",
"PosSocketsService.UnSupportedStoreIPAddress",
PosApplication.Trace,
this.ngcIPAddress,
this.ngcPort.ToString(),
storeIPAddress,
storePort.ToString());
throw ce;
}
// Setup a connection handler
this.handlerLock.AcquireWriterLock(-1);
try
{
IPosHandler posHandler = PosApplication.ConstructIPosHandler(this,connection);
this.handlerList.Add(posHandler);
}
finally
{
this.handlerLock.ReleaseWriterLock();
}
}
catch (ObjectDisposedException)
{
// this object has been disposed by another thread
}
catch (SocketException e)
{
ExceptionManager.Publish(e);
PosApplication.Trace.WriteDebug("Unexpected Socket Exception Closing Listener "+this.ngcIPAddress+":"+this.ngcPort);
this.Dispose();
}
catch (Exception e)
{
ExceptionManager.Publish(e);
}
finally
{
trState.EndProc();
}
}
有关例外情况的详情如下:
internal PosListener(IPEndPoint lep, int pendingBacklog, Hashtable storeTable, ArrayList messageList) : base(lep.Address.AddressFamily,SocketType.Stream,ProtocolType.Tcp)
{
ITraceState trState = PosApplication.Trace.StartProc("PosListener");
try
{
// Setup listener
this.ngcIPAddress = lep.Address.ToString();
this.ngcPort = lep.Port;
this.storeTable = storeTable;
this.storeLock = new ReaderWriterLock();
this.messageList = messageList;
this.messageLock = new ReaderWriterLock();
this.handlerList = new ArrayList();
this.handlerLock = new ReaderWriterLock();
this.asyncCallback = new AsyncCallback(this.CallbackAccept);
this.Bind(lep);
this.Listen(pendingBacklog);
// Start listening
PosApplication.PosSocketsEventLog.WriteEntry("Starting Listener on NGC Port "+this.ngcIPAddress+":"+this.ngcPort);
PosApplication.Trace.WriteDebug("Starting Listener on NGC Port "+this.ngcIPAddress+":"+this.ngcPort);
this.BeginAccept(this.asyncCallback,null);
}
catch (Exception e)
{
ExceptionManager.Publish(e);
}
finally
{
trState.EndProc();
}
}
Socket connection = this.EndAccept(ar);
异常类型: System.Net.Sockets.SocketException 错误代码:10054 消息:已创建现有连接 被远程主机强制关闭 SocketErrorCode:连接重置 国家错误代码:10054 数据: System.Collections.ListDictionaryInternal TargetSite:System.Net.Sockets.Socket EndAccept(字节[]ByRef,Int32 ByRef, 系统名称(IAsyncResult) 帮助链接:空 资料来源:系统 堆栈跟踪信息
在 System.Net.Sockets.Socket.EndAccept(字节[])& 缓冲区、Int32和ByTestTransferred, IAsyncResult(结果) 在 System.Net.Sockets.Socket.EndAccept(IAsyncResult 异步(结果) 在 Fujitsu.eCrm.Seoul.PosSocketsService.PosListener.CallbackAccept(IAsyncResult ar)在 C:\Inetpub\wwwroot\PosSocketsService\PosListener.cs:line 109 第109行如下:
internal PosListener(IPEndPoint lep, int pendingBacklog, Hashtable storeTable, ArrayList messageList) : base(lep.Address.AddressFamily,SocketType.Stream,ProtocolType.Tcp)
{
ITraceState trState = PosApplication.Trace.StartProc("PosListener");
try
{
// Setup listener
this.ngcIPAddress = lep.Address.ToString();
this.ngcPort = lep.Port;
this.storeTable = storeTable;
this.storeLock = new ReaderWriterLock();
this.messageList = messageList;
this.messageLock = new ReaderWriterLock();
this.handlerList = new ArrayList();
this.handlerLock = new ReaderWriterLock();
this.asyncCallback = new AsyncCallback(this.CallbackAccept);
this.Bind(lep);
this.Listen(pendingBacklog);
// Start listening
PosApplication.PosSocketsEventLog.WriteEntry("Starting Listener on NGC Port "+this.ngcIPAddress+":"+this.ngcPort);
PosApplication.Trace.WriteDebug("Starting Listener on NGC Port "+this.ngcIPAddress+":"+this.ngcPort);
this.BeginAccept(this.asyncCallback,null);
}
catch (Exception e)
{
ExceptionManager.Publish(e);
}
finally
{
trState.EndProc();
}
}
Socket connection = this.EndAccept(ar);
我们看到的行为是当服务启动时POSListners都启动了。然后在一段短时间后,每一个都被关闭——同时引发相同的10054异常
监听器似乎是由显示在受监视端口上的数据触发的,但是当回调方法尝试创建套接字以便读取数据时,windows无法建立套接字
有谁能建议采取什么措施来尝试找出问题的根本原因吗?我觉得这个设计很奇怪。阿法伊可以看出,你正在发布400个单独的监听。通常,套接字服务器将发出一次侦听,然后使用发送的一些标识数据或源IP区分传入的客户端连接(“哪个服务器刚刚连接?”)
我看不出这里到底出了什么问题,但我质疑这个设计。我的猜测可能是服务器上的资源限制,可能是服务器负载配置文件在代码运行后发生了更改。这种设计对我来说非常奇怪。阿法伊可以看出,你正在发布400个单独的监听。通常,套接字服务器将发出一次侦听,然后使用发送的一些标识数据或源IP区分传入的客户端连接(“哪个服务器刚刚连接?”)
我看不出这里到底出了什么问题,但我质疑这个设计。我猜可能是服务器上的资源限制,可能是因为代码运行后服务器负载配置文件发生了更改。发生这种情况是因为您的一个客户端正在强制(如果您愿意,不正常)断开与服务器的连接 举个例子,在典型的GUI上应该有连接/断开按钮。但是,如果用户突然连接并关闭应用程序,套接字将没有时间终止与服务器的连接。当防火墙启动或存在线路连接问题时,可能会发生这种情况
在任何情况下,我建议您通过自己(服务器端)关闭套接字来处理带有特定错误代码的SocketException。发生这种情况的原因是您的一个客户端被强制(如果您愿意的话)断开与服务器的连接 举个例子,在典型的GUI上应该有连接/断开按钮。但是,如果用户突然连接并关闭应用程序,套接字将没有时间终止与服务器的连接。当防火墙启动或存在线路连接问题时,可能会发生这种情况
在任何情况下,我建议您通过自己(服务器端)关闭套接字来处理带有特定错误代码的SocketException。您可以关闭所有400台机器并逐个尝试,您还可以缩进代码以使其格式正确-我知道这两项任务都很艰巨。对不起,他是对的。您现在对代码进行格式化(或未格式化)的方式使示例无法阅读。请修复。你可以关闭所有的400台机器,然后一个接一个地尝试,你也可以缩进你的代码,使其格式正确-这两个巨大的任务,我知道。对不起,他是对的。您现在对代码进行格式化(或未格式化)的方式使示例无法阅读。请修复。该问题表示所有连接都失败,而不仅仅是一个。该问题表示所有连接都失败,而不仅仅是一个。