在很多WinForm的程序中,会有客户端之间相互通信的需求,或服务端与客户端通信的需求,这时就要用到TCP/IP的功能。在.NET中,主要是通过Socket来完成的,下面的例子是通过一个TcpListerner作为监听,等待TcpClient的连接,连接成功后就可以利用NetStream进行信息互通了。
在互相通信过程中,发送和接收分别在不同的线程中处理,并且还要处理UI更新。
using System.Collections.Generic; using System.IO; using System.Net; using System.Net.Http; using System.Net.Sockets; namespace WinFormsDemo15 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } /// <summary> /// 创建连接后的网络流 /// </summary> NetworkStream _stream; private void ConnectionButton_Click(object sender, EventArgs e) { if (IsServer.Checked) { AcceptTcpClient(); } else { var tcpClient = new TcpClient(IPTextBox.Text, int.Parse(PortTextBox.Text)); _stream = tcpClient.GetStream(); ReciveMessage(_stream); } } /// <summary> /// 在一个新线程中接收TcpClient连接 /// </summary> void AcceptTcpClient() { Task.Run(() => { var listener = new TcpListener(IPAddress.Any, int.Parse(PortTextBox.Text)); listener.Start(); var tcpClient = listener.AcceptTcpClient(); this.Invoke(() => { YouTextBox.Text += $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")} \r\n收到客户端连接。\r\n"; }); _stream = tcpClient.GetStream(); ReciveMessage(_stream); }); } /// <summary> /// 接收消息 /// </summary> /// <param name="stream">网络流</param> void ReciveMessage(NetworkStream stream) { Task.Run(() => { while (true) { var messageArr = new byte[1024]; stream.Read(messageArr, 0, messageArr.Length); this.Invoke(() => { var message = System.Text.Encoding.UTF8.GetString(messageArr).Trim('\0'); YouTextBox.Text += $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")} 对方说:\r\n{message}\r\n"; }); } }); } /// <summary> /// 发送消息 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void SendButton_Click(object sender, EventArgs e) { var messageArr = System.Text.Encoding.UTF8.GetBytes(MyTextBox.Text); _stream.Write(messageArr, 0, messageArr.Length); _stream.Flush(); YouTextBox.Text += $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")} 我说:\r\n{MyTextBox.Text}\r\n"; MyTextBox.Clear(); } } }
下面是演示结果:
![](/i/l/?n=23&i=blog/1101861/202302/1101861-20230207212531550-897125619.png)
标签:stream,messageArr,Text,System,间通信,窗体,var,using,WinForm From: https://www.cnblogs.com/axzxs2001/p/17099897.html