日期:2014-05-19  浏览次数:20823 次

关于Socket疑问!
使用完Socket后,用Close方法关闭Socket连接,为什么会出现一个出错对话框,说操作设计计算机及操作系统硬件,抛出异常System.IO.IOEXception.如果忽略此对话框,程序部分功能也能正常应用。

连接Socket时,我设计一个函数button1_Click,为什么连续连接使用button1_Click两次时(一次正常),也出现上述情况。

我关闭

使用button3_Click关闭Socket后,button1_Click使用连接Socket也出现上述情况!


谢谢各位大哥指点!

  private   void   button1_Click(object   sender,   EventArgs   e)
                {
                        try{
                              //   myIP   =   IPAddress.Parse(textBox1.Text);
                                myIP   =   IPAddress.Parse( "127.0.0.1 ");
                        }catch   {   MessageBox.Show( "你输入的IP地址格式不对,请再次输入! ");   }


                        try   {
                                Thread   thread   =   new   Thread(new   ThreadStart(accp));
                                thread.Start();          
                        }
                        catch   (Exception   ee)   {   textBox3.AppendText(ee.Message);   }
                }
                private   void   accp()
                {
                        //myServer   =   new   IPEndPoint(myIP,Int32.Parse(textBox2.Text));
                        myServer   =   new   IPEndPoint(myIP,   10);
                        this.sock   =   new   Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
                        this.sock.Bind(myServer);
                        this.sock.Listen(50);
                        textBox3.AppendText( "服务器启动\r\n ");
                        while   (true)
                        {
                                this.accsock   =   this.sock.Accept();
                                if   (this.accsock.Connected)
                                {
&nbs