C#简单的UDP通信例子
1 1,UDP客户端 2 3 using System; 4 using System.Collections.Generic; 5 using System.ComponentModel; 6 using System.Data; 7 using System.Drawing; 8 using System.Linq; 9 using System.Text; 10 using System.Windows.Forms; 11 using System.Net.Sockets; 12 using System.Net; 13 14 namespace WindowsFormsApplication14 15 { 16 public partial class Form1 : Form 17 { 18 UdpClient udpClient; 19 IPEndPoint ipEndPoint; 20 21 public Form1() 22 { 23 InitializeComponent(); 24 udpClient = new UdpClient(12345); 25 ipEndPoint = new IPEndPoint(IPAddress.Parse("192.168.1.241"), 60000); 26 } 27 28 private void button1_Click(object sender, EventArgs e) 29 { 30 //byte[] mybyte = Encoding.Default.GetBytes("nihao"); 31 32 byte[] mybyte = new byte[4]; 33 mybyte[0] = 0x12; 34 mybyte[1] = 0x00; 35 mybyte[2] = 0x34; 36 mybyte[3] = 0x00; 37 38 39 udpClient.Send(mybyte, mybyte.Length,ipEndPoint); 40 } 41 } 42 } 43 2,UDP服务器 44 45 using System; 46 using System.Collections.Generic; 47 using System.ComponentModel; 48 using System.Data; 49 using System.Drawing; 50 using System.Linq; 51 using System.Text; 52 using System.Windows.Forms; 53 using System.Net.Sockets; 54 using System.Net; 55 56 namespace WindowsFormsApplication15 57 { 58 public partial class Form1 : Form 59 { 60 UdpClient udpServer; 61 IPEndPoint ipEndPoint; 62 63 public Form1() 64 { 65 InitializeComponent(); 66 udpServer = new UdpClient(23456); 67 ipEndPoint = new IPEndPoint(new IPAddress(0), 0); 68 } 69 70 private void button1_Click(object sender, EventArgs e) 71 { 72 73 byte[] data = udpServer.Receive(ref ipEndPoint); 74 MessageBox.Show ( Encoding.Default.GetString(data)); 75 } 76 } 77 } 78 3,读接收缓冲区当前数据个数 79 80 using System; 81 using System.Collections.Generic; 82 using System.ComponentModel; 83 using System.Data; 84 using System.Drawing; 85 using System.Linq; 86 using System.Text; 87 using System.Windows.Forms; 88 using System.Net.Sockets; 89 using System.Net; 90 91 namespace WindowsFormsApplication15 92 { 93 public partial class Form1 : Form 94 { 95 UdpClient udpServer; 96 IPEndPoint ipEndPoint; 97 98 public Form1() 99 { 100 InitializeComponent(); 101 udpServer = new UdpClient(23456); 102 ipEndPoint = new IPEndPoint(new IPAddress(0), 0); 103 } 104 105 private void button1_Click(object sender, EventArgs e) 106 { 107 byte[] outValue = BitConverter.GetBytes(0); 108 udpServer.Client.IOControl(IOControlCode.DataToRead, null, outValue); 109 110 111 MessageBox.Show((BitConverter.ToInt32(outValue,0)).ToString());//发送数据发现此时数据增长,但不会超过8K 112 //MessageBox.Show((BitConverter.ToString(outValue)).ToString()); 113 114 } 115 } 116 } 117 118 119 4,上例有点复杂,看下面的例子,这个例子还可以说明如何设置非阻塞Socket通讯 120 121 using System; 122 using System.Collections.Generic; 123 using System.ComponentModel; 124 using System.Data; 125 using System.Drawing; 126 using System.Linq; 127 using System.Text; 128 using System.Windows.Forms; 129 using System.Net.Sockets; 130 using System.Net; 131 132 namespace WindowsFormsApplication15 133 { 134 public partial class Form1 : Form 135 { 136 UdpClient udpServer; 137 IPEndPoint ipEndPoint; 138 139 public Form1() 140 { 141 InitializeComponent(); 142 udpServer = new UdpClient(23456); 143 ipEndPoint = new IPEndPoint(new IPAddress(0), 0); 144 udpServer.Client.Blocking = false; //设置为非阻塞模式 145 } 146 147 private void button1_Click(object sender, EventArgs e) 148 { 149 int buffSizeCurrent; 150 buffSizeCurrent = udpServer.Client.Available;//取得缓冲区当前的数据的个数 151 MessageBox.Show(buffSizeCurrent.ToString()); 152 if (buffSizeCurrent > 0) //有数据时候才读,不然会出异常哦 153 { 154 byte[] data = udpServer.Receive(ref ipEndPoint); 155 MessageBox.Show(Encoding.Default.GetString(data)); 156 } 157 } 158 } 159 } 160 5,上面的例子是同步非阻塞的例子,但默认是同步阻塞的。除此之外还有异步的通讯方式(事件通知的方式触发),稍微复杂,以后补充。