问题描述:
以下代码是一个Server代码,开启侦听后就出现无法响应的假死问题;while是作用为了满足多个客户端,而现在情况是假死。如果去掉了while(true),在未开启客户端进行链接时,server也是假死状态,开启client后则正常。各路大神门请给予分析先谢过!namespace myChatToolServer
{
public partial class Form1 : Form
{
IPAddress ip ;
TcpListener listener;//开启对8500端口的侦听
Thread threadListener;
public Form1()
{
ip = new IPAddress(new byte[] { 127, 0, 0, 1 });
listener = new TcpListener(ip, 8500);//开启对8500端口的侦听
InitializeComponent(); }
delegate void aa();//创建一个代理
private void threadListenerMethod()
{
if (!textBox1.InvokeRequired)
{
textBox1.AppendText("Listen is Started...\n"); listener.Start();//开始侦听 while (true)//问题可能出现在这里?
{
TcpClient remoteClient = listener.AcceptTcpClient();
}
}
else
{
Invoke(new aa(threadListenerMethod), new object[]{});
}
} private void button1_Click(object sender, EventArgs e)//开启侦听事件
{
threadListener = new Thread(new ThreadStart(threadListenerMethod)); threadListener.Start();
}
}
}
以下代码是一个Server代码,开启侦听后就出现无法响应的假死问题;while是作用为了满足多个客户端,而现在情况是假死。如果去掉了while(true),在未开启客户端进行链接时,server也是假死状态,开启client后则正常。各路大神门请给予分析先谢过!namespace myChatToolServer
{
public partial class Form1 : Form
{
IPAddress ip ;
TcpListener listener;//开启对8500端口的侦听
Thread threadListener;
public Form1()
{
ip = new IPAddress(new byte[] { 127, 0, 0, 1 });
listener = new TcpListener(ip, 8500);//开启对8500端口的侦听
InitializeComponent(); }
delegate void aa();//创建一个代理
private void threadListenerMethod()
{
if (!textBox1.InvokeRequired)
{
textBox1.AppendText("Listen is Started...\n"); listener.Start();//开始侦听 while (true)//问题可能出现在这里?
{
TcpClient remoteClient = listener.AcceptTcpClient();
}
}
else
{
Invoke(new aa(threadListenerMethod), new object[]{});
}
} private void button1_Click(object sender, EventArgs e)//开启侦听事件
{
threadListener = new Thread(new ThreadStart(threadListenerMethod)); threadListener.Start();
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货