socket 服务端为什么会随客户端关闭需关闭?

hanzhaorui 2007-10-29 10:19:01
服务器端部分代码如下,为什么客户端连接上后一关闭服务器端也会关闭?
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;
}
}
...全文
138 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
wzd24 2007-10-29
  • 打赏
  • 举报
回复
发到这上面不可以吗?
wzd24@126.com
hanzhaorui 2007-10-29
  • 打赏
  • 举报
回复
如有高手请把您的邮箱说一下,我把源码发于您帮我解决一下.

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧