1. UDP client
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Net.Sockets;using System.Net;namespace WindowsFormsApplication14{ public partial class Form1 : Form { UdpClient udpClient; IPEndPoint ipEndPoint; public Form1() { InitializeComponent(); udpClient = new UdpClient(12345); ipEndPoint = new IPEndPoint(IPAddress.Parse("192.168.1.241"), 60000); } private void button1_Click(object sender, EventArgs e) { //byte[] mybyte = Encoding.Default.GetBytes("nihao"); byte[] mybyte = new byte[4]; mybyte[0] = 0x12; mybyte[1] = 0x00; mybyte[2] = 0x34; mybyte[3] = 0x00; udpClient.Send(mybyte, mybyte.Length,ipEndPoint); } }}
2. UDP Server
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Net.Sockets;using System.Net;namespace WindowsFormsApplication15{ public partial class Form1 : Form { UdpClient udpServer; IPEndPoint ipEndPoint; public Form1() { InitializeComponent(); udpServer = new UdpClient(23456); ipEndPoint = new IPEndPoint(new IPAddress(0), 0); } private void button1_Click(object sender, EventArgs e) { byte[] data = udpServer.Receive(ref ipEndPoint); MessageBox.Show ( Encoding.Default.GetString(data)); } }}
3. Number of current data in the read buffer
Using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. windows. forms; using System. net. sockets; using System. net; namespace WindowsFormsApplication15 {public partial class Form1: Form {UdpClient udpServer; IPEndPoint ipEndPoint; public Form1 () {InitializeComponent (); udpServer = new UdpClient (23456 ); ipEndPoint = new IPEndPoint (new IPAddress (0), 0);} private void button#click (object sender, EventArgs e) {byte [] outValue = BitConverter. getBytes (0); udpServer. client. IOControl (IOControlCode. dataToRead, null, outValue); MessageBox. show (BitConverter. toInt32 (outValue, 0 )). toString (); // when sending data, it is found that the data is growing, but not more than 8 K. // MessageBox. show (BitConverter. toString (outValue )). toString ());}}}
4. The above example is a little complicated. Let's look at the example below. This example also shows how to set up non-blocking Socket communication.
Using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. windows. forms; using System. net. sockets; using System. net; namespace WindowsFormsApplication15 {public partial class Form1: Form {UdpClient udpServer; IPEndPoint ipEndPoint; public Form1 () {InitializeComponent (); udpServer = new UdpClient (23456 ); ipEndPoint = new IPEndPoint (new IPAddress (0), 0); udpServer. client. blocking = false; // set to non-Blocking mode} private void button#click (object sender, EventArgs e ){