服务器端,我们使用下面的代码: public partial class Form1 : Form { UdpClient uc = null; public Form1() { CheckForIllegalCrossThreadCalls = false; InitializeComponent(); //注意此处端口号要与发送方相同 uc = new UdpClient(8888); //开一线程 Thread th = new Thread(new ThreadStart(listen)); //设置为后台 th.IsBackground = true; th.Start(); } private void listen() { //声明终结点 IPEndPoint iep = new IPEndPoint(IPAddress.Parse("192.168.0.20"), 8888); while (true) { //获得Form1发送过来的数据包 string text = System.Text.Encoding.UTF8.GetString(uc.Receive(ref iep)); //加入ListBox this.textBox1.Text += text+Environment.NewLine; } } 客户端的代码: UdpClient uc; public Form1() { uc = new UdpClient(); InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string temp = this.textBox1.Text; //保存TextBox文本 //将该文本转化为字节数组 byte[] b = System.Text.Encoding.UTF8.GetBytes(temp); //向本机的8888端口发送数据 uc.Send(b, b.Length, System.Net.Dns.GetHostName(), 8888); }