使用完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)
                {
                    textBox3.AppendText("与客户建立连接!\r\n");
                    Thread thread = new Thread(new ThreadStart(round));
                    thread.Start();
                }
            }
        }
       接受数据
        private void round()
        {
            while (true)
            {
                Byte[] Rec = new byte[64];
                NetworkStream netStream = new NetworkStream(this.accsock);
                netStream.Read(Rec, 0, Rec.Length);
                string RecMessage = System.Text.Encoding.BigEndianUnicode.GetString(Rec);
                richTextBox1.AppendText(RecMessage + "\r\n");
            }
        }
   发送数据
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                Byte[] sentByte = new byte[64];
                string send = richTextBox2.Text + "\r\n";
                NetworkStream netStream = new NetworkStream(this.accsock);
                sentByte = System.Text.Encoding.BigEndianUnicode.GetBytes(send.ToCharArray());
                netStream.Write(sentByte, 0, sentByte.Length);
            }
            catch { MessageBox.Show("客户连接尚未建立,无法发送!");}
        
        }
关闭Socket
        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                this.accsock.Close();
                textBox3.AppendText("服务器关闭!\r\n");            }
            catch { MessageBox.Show("客户连接尚未建立,关闭无效!"); }
        }