没看到你的 Stream 在哪里声明的呀

解决方案 »

  1.   

    在连接服务器时声明的:
    private void btnLogin_Click(object sender, EventArgs e)
            {
                if (state == CONNECTED)
                {
                    return;
                }            if (this.tbUserName.Text.Length == 0)
                {
                    MessageBox.Show("请输入您的用户名!", "提示信息",
                        MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    this.tbUserName.Focus();
                    return;
                }
                try
                {
                    //创建一个客户端套接字,它是Login的一个公共属性,
                    //将被传递给ChatClient窗体
                    tcpClient = new TcpClient();
                    //向指定的IP地址的服务器发出连接请求
                    tcpClient.Connect(IPAddress.Parse(txtHost.Text),
                        Int32.Parse(txtPort.Text));
                    //获得与服务器数据交互的流通道(NetworkStream)
                    Stream = tcpClient.GetStream();                //启动一个新的线程,执行方法this.ServerResponse(),
                    //以便来响应从服务器发回的信息
                    Thread thread = new Thread(new ThreadStart(this.ServerResponse)); //ServerResponse是用来处理服务器端传来的消息                thread.Start();
                    //this.ServerResponse();
                    //向服务器发送“CONN”请求命令,
                    //此命令的格式与服务器端的定义的格式一致,
                    //命令格式为:命令标志符(CONN)|发送者的用户名|
                    string cmd = "CONN|" + this.tbUserName.Text + "|";
                    //将字符串转化为字符数组
                    Byte[] outbytes = System.Text.Encoding.Default.GetBytes(
                        cmd.ToCharArray());
                    Stream.Write(outbytes, 0, outbytes.Length);            }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
      

  2.   

    在连接服务器时声明的:
    private void btnLogin_Click(object sender, EventArgs e)
            {
                if (state == CONNECTED)
                {
                    return;
                }            if (this.tbUserName.Text.Length == 0)
                {
                    MessageBox.Show("请输入您的用户名!", "提示信息",
                        MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    this.tbUserName.Focus();
                    return;
                }
                try
                {
                    //创建一个客户端套接字,它是Login的一个公共属性,
                    //将被传递给ChatClient窗体
                    tcpClient = new TcpClient();
                    //向指定的IP地址的服务器发出连接请求
                    tcpClient.Connect(IPAddress.Parse(txtHost.Text),
                        Int32.Parse(txtPort.Text));
                    //获得与服务器数据交互的流通道(NetworkStream)
                    Stream = tcpClient.GetStream();                //启动一个新的线程,执行方法this.ServerResponse(),
                    //以便来响应从服务器发回的信息
                    Thread thread = new Thread(new ThreadStart(this.ServerResponse)); //ServerResponse是用来处理服务器端传来的消息                thread.Start();
                    //this.ServerResponse();
                    //向服务器发送“CONN”请求命令,
                    //此命令的格式与服务器端的定义的格式一致,
                    //命令格式为:命令标志符(CONN)|发送者的用户名|
                    string cmd = "CONN|" + this.tbUserName.Text + "|";
                    //将字符串转化为字符数组
                    Byte[] outbytes = System.Text.Encoding.Default.GetBytes(
                        cmd.ToCharArray());
                    Stream.Write(outbytes, 0, outbytes.Length);            }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
      

  3.   

    问题出在Sockets.NetworkStream已经被释放了,你可以查找 Stream 有关的代码行