Perl Tutorial – Building a Local Network

2 minute read

Perl is slowly growing out of fashion, despite it’s ubiquitous influence on developing modern languages. Much of Perl’s syntax and structure has been cleaned up and made more accessible via Python, thus many developers would favor Python instead of Perl. Yet, Larry Wall’s creation is still relevant to understanding modern programming. In this tutorial, we’ll build a local network in Perl to showcase it’s ease of networking via socketry.

Sockets

If you’re unfamiliar with networking, here is a brief overview of a socket. A socket is basically opening a connection on a computer via a port (computers have a certain amount of ports that they can communicate on with other computers). In order to use a socket, you first must “create” one or open it. Then, you must script something that tells the socket to accept incoming socket requests. Finally, you must tell the socket to do something otherwise it’s not very useful.

Receiver End

We’ll first build the receiver end of the Perl script. This end must be run first in order to create an open socket to receive incoming networks.

Screen Shot 2014-07-07 at 3.51.35 PM

Enter the above code into your editor. There is a slight problem though. What if the port is being used? What if you have multiple networks going on or your running a server in port 8000. In that case, we need someway to stop the script if it cannot make a connection.

Screen Shot 2014-07-07 at 3.55.46 PM

This script takes care of busy ports. Next, we  need to script the socket to do something when another machine/user tries to make a connection.

Screen Shot 2014-07-07 at 3.59.44 PM

What this code will do is whenever something is being received, it will print what is being received into Terminal/Command Line. Now, let’s code the sending end of the network.

Sending End

Now we want to be able to send stuff over the port for the other user (which in this case would be yourself) to see.

Screen Shot 2014-07-07 at 4.14.23 PM

Running the Test

For this network to work, open up two Terminal Windows and cd to the directory containing both the receiver.pl file and the sending.pl file. In one Terminal window, execute the following commands:

Screen Shot 2014-07-07 at 4.16.40 PM

Execute this in 1 Terminal

Execute this in the other Terminal

Execute this in the other Terminal

What you should see is nothing on one end and a prompt on the other Terminal asking to “Send Messages: ”

Enter a message on one Terminal and hit enter. You should see it on the other Terminal.

Experimentation

Now that you’ve successfully created a mini-network using Perl, feel free to incorporate other features like sending files (which Perl handles nicely) or sending/intercepting messages (and possibly test some encryption methods). If you have any questions, feel free to ask!

 

 

Leave a Comment