可以接受多个client. 现在连接正常,可以使用,就是Close();时有问题.
部分代码如下:
public QTCPServer(int port, ExecuteCommand executeCommand) //构造函数,启用监听线程
{
this.sessions = new ArrayList ();
this.sessionThreads = new ArrayList ();
this.executeCommand = executeCommand;
this.open = true;
IPAddress ipAddress = Dns.GetHostEntry(Dns.GetHostName()).AddressList[0];
IPEndPoint ipLocalEndPoint = new IPEndPoint(ipAddress, port);
this.listener = new TcpListener(ipLocalEndPoint);
this.listener.Start ();
this.server = new Thread ( new ThreadStart ( Run ) );//用来监听的线程;
this.server.Start ();
}
public void Run ()
{
while ( this.open )
{
Socket s=null;
s = this.listener.AcceptSocket();
if (s.Connected)
{
QTCPServerSession session = new QTCPServerSession(s,this.executeCommand);
this.sessions.Add(session);
Thread th = new Thread(new ThreadStart(session.Run)); //在开线程,处理相关事宜.
th.Start();
this.sessionThreads.Add(th);
}
}
}
public void Close ()
{
if ( this.open )
{
this.open = false;
this.listener.Stop();
this.server.Abort ();
this.server.Join();
while (this.sessions.Count > 0)
{
Thread th = (Thread)this.sessionThreads[0];
th.Abort();
th.Join ();
this.sessionThreads.Remove(th);
QTCPServerSession s = (QTCPServerSession)this.sessions[0];
s.Close();
this.sessions.Remove(s);
}
}
}Close()时,执行到this.server.Abort ();关闭线程时, Run()中的s = this.listener.AcceptSocket();就会报错 "一个封锁操作被对WSACancelBlockingCall的调用中断"关不掉了.请碰到过此情况的高手指点.
在线.谢谢
部分代码如下:
public QTCPServer(int port, ExecuteCommand executeCommand) //构造函数,启用监听线程
{
this.sessions = new ArrayList ();
this.sessionThreads = new ArrayList ();
this.executeCommand = executeCommand;
this.open = true;
IPAddress ipAddress = Dns.GetHostEntry(Dns.GetHostName()).AddressList[0];
IPEndPoint ipLocalEndPoint = new IPEndPoint(ipAddress, port);
this.listener = new TcpListener(ipLocalEndPoint);
this.listener.Start ();
this.server = new Thread ( new ThreadStart ( Run ) );//用来监听的线程;
this.server.Start ();
}
public void Run ()
{
while ( this.open )
{
Socket s=null;
s = this.listener.AcceptSocket();
if (s.Connected)
{
QTCPServerSession session = new QTCPServerSession(s,this.executeCommand);
this.sessions.Add(session);
Thread th = new Thread(new ThreadStart(session.Run)); //在开线程,处理相关事宜.
th.Start();
this.sessionThreads.Add(th);
}
}
}
public void Close ()
{
if ( this.open )
{
this.open = false;
this.listener.Stop();
this.server.Abort ();
this.server.Join();
while (this.sessions.Count > 0)
{
Thread th = (Thread)this.sessionThreads[0];
th.Abort();
th.Join ();
this.sessionThreads.Remove(th);
QTCPServerSession s = (QTCPServerSession)this.sessions[0];
s.Close();
this.sessions.Remove(s);
}
}
}Close()时,执行到this.server.Abort ();关闭线程时, Run()中的s = this.listener.AcceptSocket();就会报错 "一个封锁操作被对WSACancelBlockingCall的调用中断"关不掉了.请碰到过此情况的高手指点.
在线.谢谢
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货