//服务器端的监听器
private TcpListener tcpl = null; //用监听器获取连接进来的套接字(Socket)
Socket newClient = tcpl.AcceptSocket();代码如上,每次断点调试的时候 走到 Socket newClient = tcpl.AcceptSocket(); 这一句的时候就会停住, 直到监听到有连接的时候才会继续往下执行, 这样很不友好因为窗体一直无响应。。请问如何解决呢、??
private TcpListener tcpl = null; //用监听器获取连接进来的套接字(Socket)
Socket newClient = tcpl.AcceptSocket();代码如上,每次断点调试的时候 走到 Socket newClient = tcpl.AcceptSocket(); 这一句的时候就会停住, 直到监听到有连接的时候才会继续往下执行, 这样很不友好因为窗体一直无响应。。请问如何解决呢、??
我很纠结,也很茫然!
不好意思,占地了!
/// <summary>
/// 接受客户端信息
/// </summary>
private void ReceiveLicentInfo()
{
try
{
//循环监听接受客户端发来的消息
while (true)
{
//byte[] byteData = new byte[1024];
string strInfo = null;
//int index = serviceSocket.Receive(byteData);
strInfo = sr.ReadLine();
Console.WriteLine("在这里打印接受到的数据,然后在与客户端发送的数据对比");
if (strInfo != null)
{
GetTextBox(strInfo);
}
Thread.Sleep(50);//程序暂停50毫秒,减少cpu的使用率
}
}
catch (Exception se)
{
connSocket.Close();
MessageBox.Show("服务器:" + se.Message);
}
}
Socket newClient = tcpl.AcceptSocket(); 放在新线程中而不是主线程Thread listenthread = new Thread(new ThreadStart(AcceptCon));
listenthread.Start();
private void AcceptCon()
{
while(true)
{
Socket newClient = tcpl.AcceptSocket();
}
}
或是
Socket newClient = tcpl.BeginAcceptSocket();