日期:2014-05-17  浏览次数:21215 次

socket客户端断开重连后不能与服务器通信
 private void Form1_Load(object sender, EventArgs e)
        {
            this.comboBox1.SelectedIndex = 0;
            IPAddress[] p = Dns.GetHostAddresses(Dns.GetHostName());
            IP.Text = p[2].ToString();
            Port.Text = "8080";
        }


        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex == 0)
            {
                label2.Text = "服务器IP:";
                label3.Text = "服务器端口号:";
            }
            else
            {
                label2.Text = "本地IP:";
                label3.Text = "本地端口号:";
            }
        }
        Socket sc;
        Socket temp;
        byte[] buffer = new byte[1024];

        private void open_Click(object sender, EventArgs e)
        {
            if (open.Text == "连接")
            {
                if (comboBox1.SelectedIndex == 0)
                {
                    int port = int.Parse(Port.Text);
                    IPAddress ip = IPAddress.Parse(IP.Text);
                    IPEndPoint ipe = new IPEndPoint(ip, port);

                    sc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    try