Dart.Sockets Namespace : Udp Class |
Use the Udp component to send and receive UDP datagrams.
Use the UdpBase.Open method to bind to a socket prior to sending and receiving.
Use the UdpBase.Send and UdpBase.Receive methods to send/receive datagrams.
Use UdpBase.JoinMulticastGroup to join a multicast group, and UdpBase.LeaveMulticastGroup to leave a multicast group.
Use Start to send or receive datagrams asynchronously, on a worker thread.
private void buttonStartServer_Click(object sender, EventArgs e) { //Start worker thread to bind to the socket and start echoing data. udpServer.Start(udpServer_Open, null); } private void buttonStopServer_Click(object sender, EventArgs e) { udpServer.Close(); } private void buttonDoEcho_Click(object sender, EventArgs e) { //Start a Udp component to act as client udpClient.Start(udpClient_DoEcho, null); } private void udpServer_Open(object state) { byte[] buffer = new byte[1024]; udpServer.Open(7); Datagram datagram = udpServer.Receive(buffer); //Datagram is null when local socket has been closed while (datagram != null) { if (datagram.Count > 0) udpServer.Send(buffer, datagram.Origin); datagram = udpServer.Receive(buffer); } } private void udpClient_DoEcho(object state) { //Send message, receive response and close byte[] buffer = new byte[1024]; udpClient.Open(0); udpClient.Send("hello world!", "127.0.0.1", 7); Datagram datagram = udpClient.Receive(buffer); //marshal data to UI thread udpClient.Marshal(datagram.ToString(), null); udpClient.Close(); } private void udpClient_UserState(object sender, UserStateEventArgs e) { textBox1.AppendText(e.Message); }
Private Sub buttonStartServer_Click(ByVal sender As Object, ByVal e As EventArgs) 'Start worker thread to bind to the socket and start echoing data. udpServer.Start(AddressOf udpServer_Open, Nothing) End Sub Private Sub buttonStopServer_Click(ByVal sender As Object, ByVal e As EventArgs) udpServer.Close() End Sub Private Sub buttonDoEcho_Click(ByVal sender As Object, ByVal e As EventArgs) Handles buttonDoEcho.Click 'Start a Udp component to act as client udpClient.Start(AddressOf udpClient_DoEcho, Nothing) End Sub Private Sub udpServer_Open(ByVal state As Object) Dim buffer(1023) As Byte udpServer.Open(7) Dim datagram As Datagram = udpServer.Receive(buffer) 'Datagram is null when local socket has been closed Do While datagram IsNot Nothing If datagram.Count > 0 Then udpServer.Send(buffer, datagram.Origin) End If datagram = udpServer.Receive(buffer) Loop End Sub Private Sub udpClient_DoEcho(ByVal state As Object) 'Send message, receive response and close Dim buffer(1023) As Byte udpClient.Open(0) udpClient.Send("hello world!", "127.0.0.1", 7) Dim datagram As Datagram = udpClient.Receive(buffer) 'marshal data to UI thread udpClient.Marshal(datagram.ToString(), Nothing) udpClient.Close() End Sub Private Sub udpClient_UserState(ByVal sender As Object, ByVal e As UserStateEventArgs) textBox1.AppendText(e.Message) End Sub
private void button1_Click(object sender, EventArgs e) { udp1.Start(multicastWorker, null); } private void multicastWorker(object state) { IPEndPoint groupEndPoint = new IPEndPoint("239.0.0.1", 32000); udp1.Open(32000); udp1.JoinMulticastGroup(groupEndPoint.Address); udp1.Send("hello world!", groupEndPoint); byte[] buffer = new byte[1024]; Datagram datagram = udp1.Receive(buffer); //Marshal data to UI for display udp1.Marshal(datagram.ToString(), null); udp1.LeaveMulticastGroup(groupEndPoint.Address); udp1.Close(); } void udp1_UserState(object sender, UserStateEventArgs e) { textBox1.AppendText(e.Message); }
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click udp1.Start(AddressOf multicastWorker, Nothing) End Sub Private Sub multicastWorker(ByVal state As Object) Dim groupEndPoint As New IPEndPoint("239.0.0.1", 32000) udp1.Open(32000) udp1.JoinMulticastGroup(groupEndPoint.Address) udp1.Send("hello world!", groupEndPoint) Dim buffer(1023) As Byte Dim datagram As Datagram = udp1.Receive(buffer) 'Marshal data to UI for display udp1.Marshal(datagram.ToString(), Nothing) udp1.LeaveMulticastGroup(groupEndPoint.Address) udp1.Close() End Sub Private Sub udp1_UserState(ByVal sender As Object, ByVal e As UserStateEventArgs) textBox1.AppendText(e.Message) End Sub
System.Object
System.MarshalByRefObject
System.ComponentModel.Component
Dart.Sockets.ComponentBase
Dart.Sockets.SocketBase
Dart.Sockets.UdpBase
Dart.Sockets.Udp