Writing a simple UDP server

The purpose of the UDP server is to detect incoming data sent from the UDP client. Any new data will be displayed in a list box.

Figure 3.1

UDP client application.

Table 3.2 Significant members of the UdpClient class.

Method or Property

Purpose

Constructor

Initializes a new instance of the UdpClient class. For client UDP applications, this is used as new UdpClient (string,int); for servers use new UdpClient(int).

Close()

Closes the UDP connection.

DropMulticastGroup()

Leaves a multicast group.

JoinMulticastGroup()

Adds a UdpClient to a multicast group. This may be invoked thus: JoinMulticastGroup(IPAddress).

Receive()

Returns a UDP datagram that was sent by a remote host. This may be invoked thus: Receive(ref IPEndPoint). Returns Byte[].

Send()

Sends a UDP datagram to a remote host. This may be invoked thus Send(byte[], int).

Active

Gets or sets a value indicating whether a connection to a remote host has been made. Returns Bool

Client

Gets or sets the underlying network sockets. Returns Socket.

As before, create a new C# project, but with a new user interface, as shown below. The list box should be named lbConnections.

A key feature of servers is multithreading (i.e., they can handle hundreds of simultaneous requests). In this case, our server must have at least two threads: one handles incoming UDP data, and the main thread of execution may continue to maintain the user interface, so that it does not appear hung. The details of threading are not within the scope of this book.

First, we write the UDP data handling thread:

public void serverThread()

UdpClient udpClient = new UdpClient(8080);

IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any,

Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint);

string returnData = Encoding.ASCII.GetString(receiveBytes); lbConnections.Items.Add( RemoteIpEndPoint.Address.ToString() + ":" + returnData.ToString()

VB.NET

Public Sub serverThread() Dim udpClient as new UdpClient(8080) While true

Dim RemoteIpEndPoint as new IPEndPoint(IPAddress.Any, 0) Dim receiveBytes as Byte()

receiveBytes = udpClient.Receive(RemoteIpEndPoint) Dim returnData As String = _ Encoding.ASCII.GetString(receiveBytes) lbConnections.Items.Add _

RemoteIpEndPoint.Address.ToString() + ":" + _ returnData.ToString() Wend End Sub

Again, we use the UdpClient object. Its constructor indicates that it should be bound to port 8080, like in the client. The Receive method is blocking (i.e., the thread does not continue until UDP data is received). In a real-world application, suitable timeout mechanisms should be in place because UDP does not guarantee packet delivery. Once received, the data is in byte array format, which is then converted to a string and displayed onscreen in the form source address: data.

There is then the matter of actually invoking the serverThread method asynchronously, such that the blocking method, Receive, does not hang the application. This is solved using threads as follows:

private void Form1_Load(object sender, System.EventArgs e) {

Thread thdUDPServer = new Thread(new ThreadStart(serverThread)); thdUDPServer.Start();

VB.NET

Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load Dim thdUDPServer = new Thread(new ThreadStart(AddressOf serverThread)) thdUDPServer.Start() End Sub

To finish off, the following assemblies are to be added:

using System.Threading;

using System.Net;

using System.Net.Sockets;

using System.Text;

VB.NET

imports System.Threading imports System.Net imports System.Net.Sockets imports System.Text

Figure 3.2

UDP Server application.

Figure 3.2

UDP Server application.

Net Tcp Port Listener

To test this application, execute it from Visual Studio .NET. On the same computer, open the UDP client and execute it. Type localhost into the textbox and press the button on the UDP client. A message "Localhost:Hello World?" should appear, such as shown in Figure 3.2.

If you have a second PC, get its IP address and install the server on this second PC and execute it. Again open the client, but type the IP address into the textbox. When you press the button on the client, the server should display the "Hello World" message. VoilĂ ! You have used .NET to send data across a network.

+1 -1

Responses

  • NINO
    What is the main purpose of UDP in network programming?
    9 days ago

Post a comment