Glossary Item Box
The following examples are intended to serve as a basic primer, demonstrating the most simple cases of sending and receiving datagrams using the Udp component.
Example 1: A Quick Test: Communicate With An Echo Server
The Echo protocol is quite simple. The client connects to the server, the client sends data to the server, the server echoes the data back to the client, and finally the client closes the connection. For this reason, the echo protocol is quite useful for testing and debugging. If you can't find a UDP echo server to use, scroll down to Example 3, where a UDP Echo server is built.
[C#] // Listen on the default interface and an ephemeral port. udp1.Open(); // Send a datagram to a echo server string s = "test"; // Send the string s to the host "MyEchoServer" on port 7. udp1.Send(s, "MyEchoServer", 7); // Receive the echoed data Datagram d = udp1.Receive(s.Length); // Display the data. Debug.WriteLine(d.ToString()); /* Output * ------------------------ * test * ------------------------ */ [Visual Basic] ' Listen on the default interface and an ephemeral port. Udp1.Open() ' Send a datagram to a echo server Dim s as String = "test" ' Send the string s to the host "MyEchoServer" on port 7. Udp1.Send(s, "MyEchoServer", 7) ' Receive the echoed data Dim d as Datagram = Udp1.Receive(s.Length) ' Display the data. Debug.WriteLine(d.ToString()) '* Output '* ------------------------ '* test '* ------------------------ '*
Example 2: Multiple Sends And Receives
This example illustrates the fact that a UDP datagram is a distinct block of data. If a host connects to an echo server and sends three datagrams, to receive all data, three datagrams will have to be received.
[C#] int iterations = 3; // Listen on the default interface and an ephemeral port. udp1.Open(); // Send several datagrams string s = "test"; for(int i=0; i<iterations; i++) udp1.send(s, "MyEchoServer", 7); // receive the echoed datagrams, there should be 3...requiring 3 Udp.Receive calls. for(int i=0; i<iterations; i++) { Datagram d = udp1.Receive(s.Length); Debug.WriteLine(d.ToString()); } /* Output * ------------------------ * test * test * test * ------------------------ */ [Visual Basic] Dim iterations as Integer = 3 ' Listen on the default interface and an ephemeral port. udp1.Open() ' Send several datagrams Dim s as String = "test" Dim i as Integer For(i=0; i<iterations; i++) Udp1.send(s, "MyEchoServer", 7) Next ' receive the echoed datagrams, there should be 3...requiring 3 Udp.Receive calls. For(i=0; i<iterations; i++) Dim d as Datagram = Udp1.Receive(s.Length) Debug.WriteLine(d.ToString()) Next '* Output '* ------------------------ '* test '* test '* test '* ------------------------ '*
Example 3: Asynchronous Use
To demonstrate asynchronous UDP methods a UDP echo server (similar to the echo server the above two examples connected to) will be created. The function StartServer will start the server. Simply call this function on Form_Load or Button_Click. This function begins an asynchronous Receive operation. When complete the EndReceive event will be raised.
[C#] private void StartServer() { // Listen for datagrams on port 7. udp1.Open(7); // Begin an asynchronous Receive byte[] buffer = new byte[udp1.BufferSize]; udp1.BeginReceive(buffer); } private void udp1_EndReceive(object sender, Dart.PowerTCP.SslSockets.DatagramEventArgs e) { // Echo the data back using the Datagram object passed into the event. // Datagram.Buffer = data received from client // Datagram.RemoteEndPoint = address/port of client. Datagram d = udp1.Send(e.Datagram.ToString(), e.Datagram.RemoteEndPoint); // Start receiving next byte[] buffer = new byte[udp1.BufferSize]; udp1.BeginReceive(buffer); } [Visual Basic] Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click ' Listen for datagrams on port 7. Udp1.Open(7) ' Begin an asynchronous Receive Dim buffer(Udp1.BufferSize) As Byte Udp1.BeginReceive(buffer) End Sub Private Sub Udp1_EndReceive(ByVal sender As Object, ByVal e As Dart.PowerTCP.SslSockets.DatagramEventArgs) Handles Udp1.EndReceive ' Echo the data back using the Datagram object passed into the event. ' Datagram.Buffer = data received from client ' Datagram.RemoteEndPoint = address/port of client. Udp1.Send(e.Datagram.ToString(), e.Datagram.RemoteEndPoint) ' Start receiving next Dim buffer(Udp1.BufferSize) As Byte Udp1.BeginReceive(buffer) End Sub
Send comments on this topic.
Documentation version 1.1.2.0.
© 2008 Dart Communications. All rights reserved.