C#winform UDP通信 发送和接收信息
来源:互联网 发布:淘宝 css 显示代码 编辑:程序博客网 时间:2024/05/21 19:21
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Net;using System.Net.Sockets;using System.Text;using System.Threading;using System.Threading.Tasks;using System.Windows.Forms;namespace UDP测试_客户端{ public partial class ClientForm2 : Form { public ClientForm2() { InitializeComponent(); } static Socket client; Thread t; Thread t2; string recv; private void btnSend_Click(object sender, EventArgs e) { EndPoint point = new IPEndPoint(IPAddress.Parse("192.168.48.1"), 6001); string msg = txtSend.Text; client.SendTo(Encoding.UTF8.GetBytes(msg), point); } private void timer1_Tick(object sender, EventArgs e) { txtReciv.Text = recv ; } private void ClientForm2_Load(object sender, EventArgs e) { client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); client.Bind(new IPEndPoint(IPAddress.Parse(txtIP.Text), int.Parse(txtPort.Text))); t = new Thread(sendMsg); //t.Start(); t2 = new Thread(ReciveMsg); t2.Start(); timer1.Start(); } /// <summary> /// 向特定ip的主机的端口发送数据报 /// </summary> void sendMsg() { EndPoint point = new IPEndPoint(IPAddress.Parse("192.168.48.1"), 6001); while (true) { string msg = txtSend.Text; client.SendTo(Encoding.Default.GetBytes(msg), point); } } /// <summary> /// 接收发送给本机ip对应端口号的数据报 /// </summary> void ReciveMsg() { while (true) { EndPoint point = new IPEndPoint(IPAddress.Any, 0);//用来保存发送方的ip和端口号 byte[] buffer = new byte[1024]; int length = client.ReceiveFrom(buffer, ref point);//接收数据报 recv += Encoding.UTF8.GetString(buffer, 0, length); recv += "\r\n"; //txtReciv.Text += recv; } } private void ClientForm2_FormClosing(object sender, FormClosingEventArgs e) { timer1.Stop(); t.Abort(); t2.Abort(); } }}UDP通信不需要建立连接。
0 0
- C#winform UDP通信 发送和接收信息
- [C#] 接收和发送UDP数据
- C# WinForm实现UDP发送和接收数据
- UDP 发送和接收
- UDP信息接收与发送
- socket 发送信息和接收信息 多用于银行通信
- C语言socket UDP广播的发送和接收示例
- C# UDP接收和发送
- UDP发送和接收程序
- UDP发送和接收程序
- C# UDP接收和发送
- smack发送和接收信息
- 发送和接收xml信息
- ACE基本的UDP通信(一)简单的发送和接收 1.0
- UDP和TCP通信机制(实现简单的数据发送与接收)
- Java TCP/IP之UDP 套接字发送和接收信息
- ACE基本的UDP通信(二)对UDP封装,并测试:连续发送和连续接收 1.0
- 使用UDP发送和接收消息
- HDU1323 Perfection
- 老毛桃一键还原,安全高效 稳定可靠 全面兼容WINDOWS系统。本程序基于Ghost(v11.02)内核,具有良好的安全和稳定性,真正达到快速备份还原;全面支持和兼容32位和64位的Windows等主
- ”交集“选择器和“并集”选择器
- 优先队列的实现(建堆)
- 基础知识——接口和抽象类
- C#winform UDP通信 发送和接收信息
- 线程
- Linux批量添加用户
- SIA实战(三):为bean动态的增加新方法
- HDU - 3652 数位dp水题
- 2017.02.14总结
- CppQuiz系列 1
- maven POM.xml 标签详解
- matlab 中的 figure 之间的切换方式