日期:2014-05-17 浏览次数:20910 次
public void Start()
{
tcpListener = new TcpListener(ipAddr, port);
tcpListener.Start(); //打开监听
Thread listenerThead = new Thread(ListenConnect);
listenerThead.IsBackground = true;
listenerThead.Start(); //打开监听线程
}
public void ListenConnect()
{
TcpClient client = null;
while (true)
{//循环监听
IAsyncResult iar = tcpListener.BeginAcceptTcpClient(null, null);
while (!iar.IsCompleted)
{//轮询是否有用户接入,这里不需要有时间超时,因为监听要做的是一直循环等待用户接入的工作
Thread.Sleep(sleepMilliseconds);
}
try
{
client = tcpListener.EndAcceptTcpClient(iar);
}
catch (Exception)
{
throw;
}
//client = tcpListener.AcceptTcpClient();
if (client != null)
{
&