Table of Contents

24.1.3.1 UDPSocket Projects

In this chapter you will be introduced to three projects that use the UDPSocket class.

For the second project, the source code is given in full in the chapter. All three project archives can be found in the download area.

24.1.3.1.1 Project 1 - Quote Service

At https://wiki.pythonde.pysv.org/UDP-Broadcasts you will find in the text

“UDP broadcasts can be used to allow client and server to find each other on a subnet without first knowing which hosts they are running on. The server does this by opening a socket that listens on a specific port. A client can then send a UDP broadcast on that port to all hosts on the subnet, to which the server can respond.”

A good hint on how to implement a broadcast on the subnet:

[1] Public Sub btnBroadcast_Click()
[2]
[3]     Dim sMessage As String
[4]
[5]     txaShowQuote.Clear()
[6]
[7] '-- Es wird ein UDP-Socket erzeugt
[8]     UDPClientSocket = New UDPSocket As "UDPClientSocket"
[9]
[10]     If UDPClientSocket.Status <= Net.Inactive Then
[11]        UDPClientSocket.Broadcast = True
[12]        UDPClientSocket.Port = 0 ' Standard-Port für einen UDP-Client
[13]        UDPClientSocket.Bind()
[14]    '-- Standard-Subnetz-Maske im Klasse-C-Netz => 255.255.255.0
[15]        txbTargetHost.Text = sNetzAdresse & ".255" ' IPv4-Broadcast-IP-Adresse
[16]    '-- Ziel-Adressierung: Host und Port
[17]        UDPClientSocket.TargetHost = txbTargetHost.Text
[18]        UDPClientSocket.TargetPort = txbPort.Text
[19]
[20]        Inc Application.Busy
[21]    '-- Ein Datagramm wird an das adressierte Ziel (Server) gesendet
[22]        Write #UDPClientSocket, "broadcast", Len("broadcast")
[23]        Dec Application.Busy
[24]        UDPClientSocket.Broadcast = False
[25]     Endif
[26]
[27]     bBroadcasted = True
[28]
[29]     Wait 0.5
[30]     If Not txaShowQuote.Text Then
[31]       sMessage = "\n\nDer Rundruf wurde von keinem UDP-Socket auf Port "
[32]       sMessage &= txbPort.Text & "\nim lokalen Netzwerk mit der Netzadresse "
[33]       sMessage &= sNetzAdresse & " quittiert!" ""
[34]        LogMessage(sMessage)
[35]        btnClose.SetFocus()
[36]     Endif
[37]
[38] End

Comment:

  1. UDPClientSocket.Port = 0
  2. UDPClientSocket.Bind()


Figures 24.1.3.1.1: Communication UDPServer ↔ UDPClient

After the acknowledged broadcast, the client knows of an active server on 192.168.2.103 with port 32340. Furthermore, it can be seen that the client is communicating with the server on the same IP address but on port 56644 assigned by the system.

In order to use the server's citation service and not just receive a message that the requested service is currently unavailable, you should install the (German) collection of citations for the server:

sudo apt install fortune-mod	        ' Programm
sudo apt-get install fortunes-de	' Quote collection


Figure 24.1.3.1.2: Server client programme

The following is a test description for one client and two servers:

24.1.3.1.2 Project 2 - Time Service

In the second project, a client uses the time service of a time server on the reserved port 123. For this purpose, the client sends an empty datagram to the server. The server acknowledges the receipt of the empty datagram by sending a datagram whose content contains the current time:


Figure 24.1.3.1.3: Client on port 123

The source code for the client is given in full:

[1] ' Gambas class file
[2]
[3] Public UDPClient As UdpSocket
[4]
[5] Public Sub Form_Open()
[6]     UDPClient = New UdpSocket As "UDPClient"
[7] '-- Initialisierung des UDP-Sockets
[8]     UDPClient.Port = 0
[9]     UDPClient.Bind()
[10] '-- Vollständige Adressierung:
[11] '-- TimeServerURL: ptbtime1.ptb.de -> IP: 192.53.103.108
[12]     UDPClient.TargetHost = "192.53.103.108"
[13] '-- Für NTP ist der UDP-Port 123 reserviert.
[14]     UDPClient.Targetport = 123
[15]     FMain.Caption = "Datagramm vom NTP-Server `ptbtime1.ptb.de` (Port 123)"
[16] End
[17]
[18] Public Sub UDPClient_Read()
[19]
[20]     Dim sData As String
[21]     Dim i, iVSeconds As Integer
[22]     Dim VSeconds As Long
[23]     Dim CurrentDate As Date
[24]
[25] '-- Auslesen des Zeit-Datagramms vom NTP-Server - gespeichert in `sData`
[26]     Read #UDPClient, sData, Lof(UDPClient)
[27] '-- Auswerten des Zeit-Datagramms -> https://de.wikipedia.org/wiki/Network_Time_Protocol
[28]     For i = 17 To 20
[29]       VSeconds += Asc(Mid$(sData, 37 - i, 1)) * 256 ^ (i - 17)
[30]     Next
[31]     iVSeconds = VSeconds - 2208988800 ' - System.TimeZone
[32]     CurrentDate = DateAdd("01/01/1970", gb.second, iVSeconds)
[33] '-- Formatieren des aktuellen Zeitstempels
[34]     lblDateTime.Text = Format(CurrentDate, "dd.mm.yyyy hh:nn:ss")
[35] End
[36]
[37] Public Sub UDPClient_Error()
[38]
[39]   Select Case UDPClient.Status
[40]     Case Net.CannotBindSocket
[41]       Message.Error(("Unable to Bind to that port"))
[42]     Case Net.CannotCreateSocket
[43]       Message.Error(("System does not allow to create a socket"))
[44]     Case Net.CannotRead
[45]       Message.Error(("Error Sending Data"))
[46]     Case Net.CannotWrite
[47]       Message.Error(("Error Receiving Data"))
[48]   End Select
[49] End
[50]
[51] Public Sub btnGetTime_Click()
[52] '-- Der NTP-Server reagiert auf den Empfang eines leeren Datagramms mit
[53] '-- dem Senden eines Datagramms, dessen Inhalt den aktuellen Zeitstempel enthält.
[54]
[55] '-- Abschicken eines leeren Datagramms an den NTP-Server -> ' # + 47 mal Nul (=> Chr(0))
[56]     Write #UDPClient, Chr$(35) & String$(47, Chr$(0)), 48
[57] End
[58]
[59] Public Sub Form_Close()
[60]   If UDPClient Then
[61]      If UDPClient.Status > 0 Then UDPClient.Close()
[62]   Endif
[63] End

24.1.3.1.3 Project 2 - Reverse Service

The third project is based on a project from the Gambas source code that demonstrates the use of the UDPSocket class. The service consists of a string sent by a client being sent back by the server as a reverse string to the client that requested the service:


Figure 24.1.3.1.4: Project 3

Note the different port numbers of client A and of client B in the middle picture, which are used to realise the communication with the server!

Download