如题,我的程序在执行到 soc.ReceiveAsync(readEventArgsWithId.ReceiveSAEA);//侦听到客户端连接开始接受数据(重要)运行时会捕获到一个异常,不是所有时候都会报错,是运行一段时间后偶尔出现此现象,然后所有的客户端都连不上了。。请哪位高手指点一下。。。
/// <summary>
/// 接受连接
/// </summary>
/// <param name="acceptEventArg"></param>
private void StartAccept(SocketAsyncEventArgs acceptEventArg)
{
try
{
if (serverstate != ServerState.Running)
return;
if (acceptEventArg == null)
{
acceptEventArg = new SocketAsyncEventArgs();
acceptEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(AcceptEventArg_Completed);
}
else
{
acceptEventArg.AcceptSocket = null;
}
m_maxNumberAcceptedClients.WaitOne();
if (listenSocket != null)
{
Boolean willRaiseEvent = this.listenSocket.AcceptAsync(acceptEventArg);//接受连接异步(重要)
if (!willRaiseEvent)//同步
{
this.ProcessAccept(acceptEventArg);
}
}
}
catch (Exception ex)
{
Err.Pro(ex);
}
}
private void AcceptEventArg_Completed(object sender, SocketAsyncEventArgs e)
{
this.ProcessAccept(e);
}
private void ProcessAccept(SocketAsyncEventArgs e)
{
try
{
if (e.LastOperation != SocketAsyncOperation.Accept) //检查上一次操作是否是Accept,不是就返回
return;
string uid = GetIDByIP((e.AcceptSocket.RemoteEndPoint as IPEndPoint).Address.ToString());//根据IP获取用户的UID
if (string.IsNullOrEmpty(uid))
return;
if (readWritePool.BusyPoolContains(uid))//判断现在的用户是否已经连接,避免同一用户开两个连接
return;
SocketAsyncEventArgsWithId readEventArgsWithId = this.readWritePool.Pop(uid);
readEventArgsWithId.ReceiveSAEA.UserToken = e.AcceptSocket;
readEventArgsWithId.SendSAEA.UserToken = e.AcceptSocket;
Interlocked.Increment(ref this.m_numConnectedSockets);//修改连接数
OnConnectChanged(m_numConnectedSockets);
Socket soc = (Socket)readEventArgsWithId.ReceiveSAEA.UserToken;
Boolean willRaiseEvent = soc.ReceiveAsync(readEventArgsWithId.ReceiveSAEA);//侦听到客户端连接开始接受数据(重要)
if (!willRaiseEvent)
{
this.ProcessReceive(readEventArgsWithId.ReceiveSAEA);
}
}
catch (Exception ex)
{
string uid = GetIDByIP((e.AcceptSocket.RemoteEndPoint as IPEndPoint).Address.ToString());//根据IP获取用户的UID
Err.Pro(ex);
Err.Pro(new Exception("UID:" + uid));
}
finally
{
this.StartAccept(e);
}
}
请问如何解决的。