socket 服务端为什么会随客户端关闭需关闭?
服务器端部分代码如下,为什么客户端连接上后一关闭服务器端也会关闭?
private void Form1_Load(object sender, EventArgs e)
{
this.transPort = "88";
System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;//新加防止出现“线程间操作无效: 从不是创建控件“listBox1”的线程访问它”类错误。
}
private void btnCreate_Click(object sender, EventArgs e)
{
Thread acceptThread = new Thread(new ThreadStart(BeginListen));
acceptThread.Start();
this.BeginListen();
}
//开始监听
void BeginListen()
{
this.Text = "在端口: " + this.transPort + " 监听...";
try
{
IPHostEntry hostEntry = null;
hostEntry = Dns.GetHostEntry("10.30.11.201");
this.mainSocketTest = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ipe = new IPEndPoint(hostEntry.AddressList[0], int.Parse(this.transPort));
//IPEndPoint ipe = new IPEndPoint(Dns.GetHostEntry(Dns.GetHostName()).AddressList[0], int.Parse(this.transPort));
this.mainSocketTest.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);//端口复用++新加的
this.mainSocketTest.Bind(ipe);
this.mainSocketTest.Listen(10);
this.netDelegate = new NetDelegate(this.CatchConnection);
this.asynCallDelegate = new AsyncCallback(this.EndCall);
this.netDelegate.BeginInvoke(this.asynCallDelegate, (object)"ddd");
}
catch (Exception e)
{
this.CloseAll();////////////////////////////////////////////++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
MessageBox.Show(e.Message+"1");
return;
}
}