public partial class Form1 : Form
{
private Server server public Form1()
{
InitializeComponent();
server = new Server();;//建立服务端对象
server.dlg = this;
textBox1.Text = server.LocalIP.ToString();
textBox4.Text = server.port.ToString();
} private void button1_Click(object sender, EventArgs e)
{
server.StartListening();//启动侦听
}
}
********************************************************************
public class Server//服务端类
{
public IPAddress LocalIP;
private string LocalName;
private TcpListener listener;
private TcpClient client;
public int port;
private int count;
private const int Max=2;
private ReomteClient[] RClient;
public Form1 dlg;
public Server()
{
LocalName = Dns.GetHostName();
LocalIP = Dns.GetHostEntry(LocalName).AddressList[0];
port=8500;
listener = new TcpListener(LocalIP, port);
RClient = new ReomteClient[Max];
count = 0;
}
public void StartListening()
{
listener.Start();
while (count<Max)
{
client = listener.AcceptTcpClient();//程序死在这里
dlg.textBox2.Text = client.Client.RemoteEndPoint.ToString();
RClient[count] = new ReomteClient(client);
RClient[count].dlg = dlg;
count++;
}
}
}
当我按下button1,会调用Server类的StartListening(),启动服务端侦听,但程序死在了listener.AcceptTcpClient()。
类似的程序在控制台上运行是正常的,不知是什么原因!在坛子里看到过相似的问题,最后的解答是listener.AcceptTcpClient()写在form_load里了,但小弟愚笨,看不懂这个解答什么意思,求大侠赐教!
{
private Server server public Form1()
{
InitializeComponent();
server = new Server();;//建立服务端对象
server.dlg = this;
textBox1.Text = server.LocalIP.ToString();
textBox4.Text = server.port.ToString();
} private void button1_Click(object sender, EventArgs e)
{
server.StartListening();//启动侦听
}
}
********************************************************************
public class Server//服务端类
{
public IPAddress LocalIP;
private string LocalName;
private TcpListener listener;
private TcpClient client;
public int port;
private int count;
private const int Max=2;
private ReomteClient[] RClient;
public Form1 dlg;
public Server()
{
LocalName = Dns.GetHostName();
LocalIP = Dns.GetHostEntry(LocalName).AddressList[0];
port=8500;
listener = new TcpListener(LocalIP, port);
RClient = new ReomteClient[Max];
count = 0;
}
public void StartListening()
{
listener.Start();
while (count<Max)
{
client = listener.AcceptTcpClient();//程序死在这里
dlg.textBox2.Text = client.Client.RemoteEndPoint.ToString();
RClient[count] = new ReomteClient(client);
RClient[count].dlg = dlg;
count++;
}
}
}
当我按下button1,会调用Server类的StartListening(),启动服务端侦听,但程序死在了listener.AcceptTcpClient()。
类似的程序在控制台上运行是正常的,不知是什么原因!在坛子里看到过相似的问题,最后的解答是listener.AcceptTcpClient()写在form_load里了,但小弟愚笨,看不懂这个解答什么意思,求大侠赐教!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货