UDP Server and Client

A UDP Server and UDP client are very similar since there is no explicit connection between the two endpoints. The difference is in how the endpoints are determined. The remote and local endpoints are free to send or receive messages to or from any network address or port. To set up a UDP server or client, follow this procedure:

Step Action

1.  

Create an IPEndPoint Object for the local IP address and port. Normally the IP address can be left blank. The port may be left as zero if incoming datagrams to any port should be matched, or non-zero to match only datagrams to a specific port.

2.  

Create a UdpClient Object using this local IPEndPoint Object.

3.  

Obtain the Socket Object by calling the udpclient_object.Client method.

4.  

If you are initiating a request, create another IPEndPoint Object that contains the IP address and port of the remote destination. Use this remote IPEndPoint Object with the socket_object.SendTo method to send the datagram.

5.  

If you are expecting to receive a request, create an IPEndPoint Object and pass it ByRef when calling the socket_object.ReceiveFrom method. The IP address and port of the remote endpoint is automatically stored in this IPEndPoint Object. You can then use the same IPEndPoint Object in a socket_object.SendTo method call to respond to the endpoint that made the request.

UDP Client Example - Read File using TFTP

In this example, a UDP client is created to read a file from a TFTP server. TFTP is a standard UDP-based file server found on many computers.

The IPEndPoint Objectsrv_ep for the remote UDP client is set to IP address “192.168.0.2”, and the TFTP port 69. A UdpClient Object, uc, is created and the Socket Object associated with uc is stored in us. The remainder of the I/O is performed with this Socket Object.

A TFTP “file open” message is built in string out and sent to the remote UDP endpoint contained in srv_ep using the SendTo method. Using the ReceiveFrom method, the reply is stored into the string inp, and the responding remote endpoint is saved in rem_ep. The rest of the messages are sent to rem_ep, and additional replies are checked to verify that they are also from rem_ep.

Public Sub TftpClient
' Access a TFTP server using UDP, open a file,
' and display it on the console.
Dim file As String = "testfile.txt"
Dim srv_ep As New IPEndPoint("192.168.0.2", 69)
Dim rem_ep, ep As IPEndPoint
Dim out, inp As String
Dim uc As New UdpClient()
Dim us As Socket
Dim count, op, block As Integer

us = uc.Client

' Build "open for read" command
out = Chr(0) & Chr(1) & file & Chr(0) & "octet" & Chr(0)
us.SendTo(out, 0, srv_ep)

count = us.ReceiveFrom(inp, 1500, rem_ep)
Console.Writeline("Remote ip: " & rem_ep.IPAddress & _
", port: " & CStr(rem_ep.Port))

op = Asc(inp.Substring(0,1))*256 + Asc(inp.Substring(1,1))
block = Asc(inp.Substring(2,1))*256 + Asc(inp.Substring(3,1))
Console.Writeline("Block: " & CStr(Block))
If (count>4) Then
Console.Writeline(inp.Substring(4))
End If

While True
out = Chr(0) & Chr(4) & Chr(block/256) & Chr(block)
us.SendTo(out, 0, rem_ep)

If (count<512) Then ' End if less than 512 bytes
Exit While
End If


count = us.ReceiveFrom(inp, 1500, ep)
If (ep.IPAddress<>rem_ep.IPAddress) Or _
(ep.Port<>rem_ep.Port) Then
Console.Writeline("Address mismatch")
Exit While
End If

block = Asc(inp.Substring(2,1))*256 + _
Asc(inp.Substring(3,1))
Console.Writeline("Block: " & CStr(Block))
If (count>4) Then
Console.Writeline(inp.Substring(4))
End If
End While


Console.Writeline("Transfer complete")
us.Close

End Sub

UDP Client Example - Write File using TFTP

In this example, a UDP client is executed on the controller that writes a file to a remote TFTP server. TFTP is a standard UDP-based file server found on many computers.

The IPEndPoint Object srv_ep is set to the IP address (192.168.0.2) and TFTP port (69) for the remote UDP server.  An UdpClient Object, uc, is created and the Socket Object associated with uc is stored in us. The remainder of the I/O is performed with this Socket Object.

A local file is opened for read using a StreamReader object. Then a TFTP “file write request” message is built in string out and sent to the remote UDP endpoint contained in srv_ep using the SendTo method. Using the ReceiveFrom method, the reply is stored into the string inp, and the responding remote endpoint is saved in rem_ep. The reply opcode is checked to verify that the server has accepted the write.

The rest of the messages are sent to rem_ep, and additional replies are checked to verify that they are also from rem_ep.

Data is transferred from the local file to the TFTP server in blocks of 512 bytes, using a "data" message. After each data message, the reply is read from the server and the opcode and acknowledged block number is checked. A more elaborate client program could retransmit data blocks if an error occurs.

Public Sub TftpWrite
' Access a TFTP server using UDP,
' Open a local file for read,
' and write the file to the TFTP server
Dim file As String = "testfile.txt"
Dim srv_ep As New IPEndPoint("192.168.0.2", 69)
Dim rem_ep, ep As IPEndPoint
Dim out, inp As String
Dim in_file As StreamReader
Dim uc As New UdpClient()
Dim us As Socket
Dim count, op, block, ack, err As Integer
Dim c As Integer
Dim
ii As Integer

' Open file to read from flash
in_file = New StreamReader("/flash/" & file)

us = uc.Client

' Build "open for write" command
out = Chr(0) & Chr(2) & file & Chr(0) & "octet" & Chr(0)
us.SendTo(out, 0, srv_ep)

count = us.ReceiveFrom(inp, 1500, rem_ep)
Console.Writeline("Remote ip: " & rem_ep.IPAddress & _
", port: " & CStr(rem_ep.Port))

op = Asc(inp.Substring(0,1))*256 + Asc(inp.Substring(1,1))
Console.Writeline("Open response: " & CStr(op))

' Handle error
If op <> 4 Then
If
op = 5 Then
err = Asc(inp.Substring(2,1))*256 + Asc(inp.Substring(3,1))
Console.Writeline("Error code: " & CStr(err))
End If
GoTo
_exit
End If

block = 1

Do
out = ""
For ii = 1 To 512 ' Read block of up to 512 bytes
c = in_file.Read()
If c < 0 Then Exit For
out &= Chr(c)
Next ii

' Write data block
out = Chr(0) & Chr(3) & Chr(block/256) & Chr(block) & out
us.SendTo(out, 0, rem_ep)

' Read reply
count = us.ReceiveFrom(inp, 1500, ep)
If (ep.IPAddress <> rem_ep.IPAddress) OrElse _
(ep.Port <> rem_ep.Port) Then
Console.Writeline("Address mismatch")
Exit Do
End If


op = Asc(inp.Substring(0,1))*256 + Asc(inp.Substring(1,1))
If (op <> 4) Then
Console.WriteLine("Failed to write")
End If
ack = Asc(inp.Substring(2,1))*256 + Asc(inp.Substring(3,1))
If ack <> block Then
Console.Writeline("Ack block mismatch")
End If
block += 1
Loop While c >= 0 ' Loop until end of file
Console.Writeline("Transfer complete")

_exit:
us.Close
in_file.Close()

End Sub