请问要怎么解决啊?
我的源代码:
private void button1_Click(object sender, EventArgs e)
{
try
{
tcpListener = new TcpListener(1234);//设置监听端口
tcpListener.Start();//启动监听
MessageBox.Show("服务器已启动!");
socketForClient = tcpListener.AcceptSocket();//挂起连接
networkStream = new NetworkStream(socketForClient);//存入网络流
streamWriter = new StreamWriter(networkStream);
streamReader = new StreamReader(networkStream);
int flag = 0;
if (socketForClient.Connected)
{
if (flag == 0)
{
flag = 1;
MessageBox.Show("客户端已连接!");
}
buffer = new byte[1024];
networkStream.BeginRead(buffer, 0, 1024, new AsyncCallback(Read), null);//异步读取网络流
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
private void Read(IAsyncResult ar)
{
message = Encoding.UTF8.GetString(buffer);//接受信息
buffer = new byte[1024];
networkStream.BeginRead(buffer, 0, 1024, new AsyncCallback(Read), null);
this.textBox2.Text = message;//这一句抛异常了…当我一定要赋值啊??
}