IPHostEntry iph = new IPHostEntry();
iph = Dns.GetHostByName(Dns.GetHostName());
string ddd = iph.AddressList[0].ToString();
try
{
int port = 2000;
string host = ddd;
IPAddress ip = IPAddress.Parse(host);
IPEndPoint ipe = new IPEndPoint(ip, port);
informationBox.Text = "服务已开启,正在监听.....";
TcpListener server = new TcpListener(ip, port);
server.Start();
client = server.AcceptTcpClient();
stream = client.GetStream();
th = new Thread(new ThreadStart(readData));
th.Start();
Byte[] data = Encoding.Default.GetBytes("连接成功!");
stream.Write(data, 0, data.Length);
}
catch (ArgumentNullException d)
{
informationBox.Text = informationBox.Text + "\r\n" + d.Message;
}
catch (SocketException d)
{
informationBox.Text = informationBox.Text + "\r\n" + d.Message;
}
代码是这样,可在运行到client = server.AcceptTcpClient();的时候程序好象进入了死循环似的 这怎么解决啊?
iph = Dns.GetHostByName(Dns.GetHostName());
string ddd = iph.AddressList[0].ToString();
try
{
int port = 2000;
string host = ddd;
IPAddress ip = IPAddress.Parse(host);
IPEndPoint ipe = new IPEndPoint(ip, port);
informationBox.Text = "服务已开启,正在监听.....";
TcpListener server = new TcpListener(ip, port);
server.Start();
client = server.AcceptTcpClient();
stream = client.GetStream();
th = new Thread(new ThreadStart(readData));
th.Start();
Byte[] data = Encoding.Default.GetBytes("连接成功!");
stream.Write(data, 0, data.Length);
}
catch (ArgumentNullException d)
{
informationBox.Text = informationBox.Text + "\r\n" + d.Message;
}
catch (SocketException d)
{
informationBox.Text = informationBox.Text + "\r\n" + d.Message;
}
代码是这样,可在运行到client = server.AcceptTcpClient();的时候程序好象进入了死循环似的 这怎么解决啊?
建议使用异步方式侦听
简单如
client = server.BeginXXXX();
th.Start(); 不能放到这里的,要独立出来