C# 侦听套接字-连接套接字在EndAccept处连接而不中断调试
我在.NETC#,多线程套接字应用程序中遇到了问题 随机经过一段时间后,连接被接受,而不执行回调函数,并且不会开始接受 侦听器和回调函数的代码如下所示C# 侦听套接字-连接套接字在EndAccept处连接而不中断调试,c#,sockets,asyncsocket,C#,Sockets,Asyncsocket,我在.NETC#,多线程套接字应用程序中遇到了问题 随机经过一段时间后,连接被接受,而不执行回调函数,并且不会开始接受 侦听器和回调函数的代码如下所示 public void StartListening() { // Data buffer for incoming data. byte[] bytes = new Byte[1024]; // Establish the local endpoint for the socke
public void StartListening()
{
// Data buffer for incoming data.
byte[] bytes = new Byte[1024];
// Establish the local endpoint for the socket.
// The DNS name of the computer
// running the listener is "host.contoso.com".
IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, listenPort);//Listen on all available network interfaces
// Create a TCP/IP socket.
Socket listener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
// Bind the socket to the local endpoint and listen for incoming connections.
bool signal = true;
try
{
listener.Bind(localEndPoint);
listener.Listen(listenPort);
while (startlisten)
{
// Set the event to nonsignaled state.
allDone.Reset();
if (signal)
{
// Start an asynchronous socket to listen for connections.
string s = string.Format("Server Socket: Waiting for a connection at Port:{0}", listenPort);
DisplayMsg(s);
listener.BeginAccept(
new AsyncCallback(AcceptCallback),
listener);
}
// Wait until a connection is made before continuing.
signal = allDone.WaitOne(100);
}
listener.Close();
}
catch (Exception e)
{
if (e is System.Net.Sockets.SocketException)
{
SocketException socex = (e as SocketException);
throw new Exception(socex.Message);
}
else
{
throw new Exception(e.Message);
}
}
}
public void AcceptCallback(IAsyncResult ar)
{
// Signal the main thread to continue.
try
{
if (startlisten)
{
// Get the socket that handles the client request.
Socket listener = (Socket)ar.AsyncState;
TCPClient tcp = frm.GetClientObj();
tcp.socketForServer = listener.EndAccept(ar);
DisplayErrorMsg("Connection Request From: " + tcp.socketForServer.RemoteEndPoint.ToString());
tcp.Connected = true;
tcp.ReceiveData();
// Create the state object.
// StateObject state = new StateObject();
// state.workSocket = handler;
// handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
// new AsyncCallback(ReadCallback), state);
}
}
catch (Exception e)
{
if (e is System.Net.Sockets.SocketException)
{
SocketException socex = (e as SocketException);
DisplayErrorMsg(socex.Message);
}
else
{
DisplayErrorMsg(e.Message);
}
}
allDone.Set();
}
如果在EndAccept之前关闭侦听套接字,则会发生这种情况 要取消对BeginAccept方法的挂起调用,请关闭套接字。当在异步操作进行时调用close方法时,将调用提供给BeginAccept方法的回调。对EndAccept方法的后续调用将抛出ObjectDisposedException,以指示操作已被取消
如果在EndAccept之前关闭侦听套接字,则会发生这种情况 要取消对BeginAccept方法的挂起调用,请关闭套接字。当在异步操作进行时调用close方法时,将调用提供给BeginAccept方法的回调。对EndAccept方法的后续调用将抛出ObjectDisposedException,以指示操作已被取消
我找到了答案,它与
allDone
事件有关,应该在if循环中发出信号
if (signal)
{
allDone.Reset();
// Start an asynchronous socket to listen for connections.
string s = string.Format("Server Socket: Waiting for a connection at Port:{0}", listenPort);
DisplayMsg(s);
listener.BeginAccept(new AsyncCallback(AcceptCallback), listener);
}
// Wait until a connection is made before continuing.
signal = allDone.WaitOne(100);
我找到了答案,它与if循环中的
allDone
事件有关
if (signal)
{
allDone.Reset();
// Start an asynchronous socket to listen for connections.
string s = string.Format("Server Socket: Waiting for a connection at Port:{0}", listenPort);
DisplayMsg(s);
listener.BeginAccept(new AsyncCallback(AcceptCallback), listener);
}
// Wait until a connection is made before continuing.
signal = allDone.WaitOne(100);
为什么不捕获SocketException而不是使用
is
/as
。或者只需抛出异常(e.Message)
就可以隐藏异常,因为如果两个块最终执行相同的操作…您的tcp.ReceiveData()是阻塞还是异步的?tcp.ReceiveData是异步的而不是阻塞的。BeginAccept应该接受一个连接还是多个连接?我想问题是开始接受连接,但在一些随机连接之后,不会调用AcceptCallback。为什么不捕获SocketException而不是使用is
/as
。或者只需抛出异常(e.Message)
就可以隐藏异常,因为如果两个块最终执行相同的操作…您的tcp.ReceiveData()是阻塞还是异步的?tcp.ReceiveData是异步的而不是阻塞的。BeginAccept应该接受一个连接还是多个连接?我想问题是开始接受连接,但在一些随机连接之后,不会调用AcceptCallback。