C#用UDP同步发送数据和接受数据
2012-08-31 12:14
507 查看
C#用UDP同步发送数据和接受数据
界面设计:
using System; using System.Text; using System.Windows.Forms; using System.Net; using System.Net.Sockets; using System.Threading; namespace UdpChatExample { public partial class FormChat : Form { /// <summary>接收用</summary> private UdpClient receiveUdpClient; /// <summary>发送用</summary> private UdpClient sendUdpClient; /// <summary>和本机绑定的端口号</summary> private const int port = 18001; /// <summary>本机IP</summary> IPAddress ip; /// <summary>远程主机IP</summary> IPAddress remoteIp; public FormChat() { InitializeComponent(); //获取本机可用IP地址 IPAddress[] ips = Dns.GetHostAddresses(Dns.GetHostName()); ip = ips[ips.Length - 1]; //为了在同一台机器调试,此IP也作为默认远程IP remoteIp = ip; textBoxRemoteIP.Text = remoteIp.ToString(); textBoxSend.Text = "你好!"; } private void FormChat_Load(object sender, EventArgs e) { //创建一个线程接收远程主机发来的信息 Thread myThread = new Thread(ReceiveData); //将线程设为后台运行 myThread.IsBackground = true; myThread.Start(); textBoxSend.Focus(); } private void ReceiveData()//接收数据 { IPEndPoint local = new IPEndPoint(ip, port); receiveUdpClient = new UdpClient(local); IPEndPoint remote = new IPEndPoint(IPAddress.Any, 0); while (true) { try { //关闭udpClient时此句会产生异常 byte[] receiveBytes = receiveUdpClient.Receive(ref remote); string receiveMessage = Encoding.Unicode.GetString( receiveBytes, 0, receiveBytes.Length); AddItem(listBoxReceive, string.Format("来自{0}:{1}", remote, receiveMessage)); } catch { break; } } } private void buttonSend_Click(object sender, EventArgs e) { Thread t = new Thread(SendMessage);//新建发送数据线程 t.IsBackground = true; t.Start(textBoxSend.Text); } /// <summary>发送数据到远程主机</summary> private void SendMessage(object obj) { string message = (string)obj; sendUdpClient = new UdpClient(0); byte[] bytes = System.Text.Encoding.Unicode.GetBytes(message); IPEndPoint iep = new IPEndPoint(remoteIp, port); try { sendUdpClient.Send(bytes, bytes.Length, iep); AddItem(listBoxStatus, string.Format("向{0}发送:{1}", iep, message)); ClearTextBox(); } catch (Exception ex) { AddItem(listBoxStatus, "发送出错:" + ex.Message); } } delegate void AddListBoxItemDelegate(ListBox listbox, string text); private void AddItem(ListBox listbox, string text) { if (listbox.InvokeRequired) { AddListBoxItemDelegate d = AddItem; listbox.Invoke(d, new object[] { listbox, text }); } else { listbox.Items.Add(text); listbox.SelectedIndex = listbox.Items.Count - 1; listbox.ClearSelected(); } } delegate void ClearTextBoxDelegate(); private void ClearTextBox() { if (textBoxSend.InvokeRequired) { ClearTextBoxDelegate d = ClearTextBox; textBoxSend.Invoke(d); } else { textBoxSend.Clear(); textBoxSend.Focus(); } } } }
界面设计:
相关文章推荐
- C#用UDP同步发送数据和接受数据
- JAVA中UDP 接受与发送数据的初步步骤
- [C#] 接收和发送UDP数据
- JAVA中UDP 接受与发送数据
- 【Python】udp网络程序-发送、接受数据
- [C#] 接收和发送UDP数据
- Java中通过UDP协议发送和接受数据
- C#编写的UDP收发数据 单独的接收数据程序可以用 但是写的接收和发送的不好使
- C# 串口编程二 应用--接受发送数据
- C# udpclient 发送数据断网后自动连接的方法
- 简单实现udp网络程序发送,接受数据
- Java中通过UDP协议发送和接受数据
- [C#] 接收和发送UDP数据
- C#使用UdpClient发送和接收UDP数据示例 16进制与字符串互转
- Java中通过UDP协议发送和接受数据
- UDP java接受数据与发送数据
- C#中UDP数据的发送、接收
- C#使用socket发送和接受数据
- UDPC#发送接受聊天
- JAVA中UDP接受与发送数据初步步骤