UDP Socket Programming example in Perl

by Can Ugur Ayfer

The example presents

  1. a UDP Server [ server.pl ]
  2. a UDP Listener [ listener.pl ]
  3. a Command probe [ cmd.pl ]
The UDP Listener will listen to port 7777 for UDP packets and echo whatever comes on this port.

The UDP Server will send some character string in UDP packets through port 7777 to a host specified in the source code. When started, the server.pl will start sending "1234567890 ABCDEFGHIJ abcdefghij" and listening to port 7778 for any commands from the "Command probe".

If the server receives the command DIGITS, it will start sending message strings of all digits.
If the server receives the command LETTERS, it will start sending message strings of all letters.
If the server receives the command DEFAULT, it will start sending message strings of digits and letters.
If the server receives the command STOP, it will pause sending messages.
If the server receives the command RESUME, it will resume sending messages.
If the server receives the command EXIT, it will terminate.
The Command Probe is a program used to send UDP command messages to the server.pl to control its behaviour.

Suggested Running Environment

I suggest that you use three different hosts to experiment with these programs.

Modify Perl codes to reflect the destination IP numbers and run in 3 separate windows.

Running the programs

  1. Suppose you have 3 hosts (host1, host2, host3) with IP numbers (192.168.0.1, 192.168.0.2, 192.168.0.3 respectively)
  2. On host1 run "listener.pl".
  3. On host2 (IP : 192.168.0.2) modify the "server.pl" code so that the variable $dest_ip reflects the IP number of host1. Run "server.pl" on host2.
  4. On host3 (IP : 192.168.0.3) modify the "cmd.pl" code so that the variable $dest_addr reflects the IP number of the host on which server.pl is running (192.168.0.2 in this example).
  5. Run cmd.pl on host3 with various command line parameters and observe the behaviour of the server program. Valid command line arguments for "cmd.pl" are:
        ./cmd.pl DIGITS
        ./cmd.pl LETTERS
        ./cmd.pl DEFAULT
        ./cmd.pl STOP
        ./cmd.pl RESUME
        ./cmd.pl EXIT
       

Back