我制作了一个 WPF 应用程序客户端,用于在 C# 中通过 TCP/IP 消息接收文本。但我必须单击 button2 才能从服务器接收数据。我想问一下如何制作一个聊天应用程序,无需单击按钮2即可直接接收文本?我的代码如下所示:
private void button1_Click(object sender, RoutedEventArgs e)
{
textBox1.Text="Client Started";
clientSocket.Connect("10.228.183.81", 5000);
textBox2.Text = "Client Socket Program - Server Connected ...";
}
private void button2_Click(object sender, RoutedEventArgs e)
{
NetworkStream serverStream = clientSocket.GetStream();
byte[] inStream = new byte[10025];
serverStream.Read(inStream, 0, (int)clientSocket.ReceiveBufferSize);
string returndata = System.Text.Encoding.ASCII.GetString(inStream);
textBox2.Text = returndata;
}
}
}