急?在线等?Socket编程问题? 没看到你的 Stream 在哪里声明的呀 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 在连接服务器时声明的: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); } } 在连接服务器时声明的: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); } } 问题出在Sockets.NetworkStream已经被释放了,你可以查找 Stream 有关的代码行 C#程序题,急 asp.net 切换城市技术 关于获取网页信息的一个问题 asp.net向SQL连续插入数值 C#与ACESS的插入,更新与读取 关于遍历的操作 关于AJAX刷新的问题 关于dataGridView1_CellLeave 事件! 报表问题~ 获取textbox的值的问题 用C#实现下面功能 各位达人,ASPX向ASCX中传变量的问题,小弟菜鸟,急,在线等
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);
}
}
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);
}
}