Network Programming with Perl

Using Perl to make network task is easy—here's how.
A Forking Client

If you want to write a client that accepts commands from STDIN and sends them to the server, the easiest solution is to write a client that forks a child. (A solution can be written using select that does not fork, but it is more complicated.) The client's parent process will read the commands from the user through STDIN and print them to the server. The client's child process will then read from the server and print the responses to STDOUT.

Listing 7.

Listing 7, clientfork.pl, is an example of a client that forks.

To fork in Perl, call the cleverly named fork function. It returns undef if the fork fails. If it succeeds, it returns 0 to the child, non-zero (the child's pid) to the parent. In clientfork.pl, an if statement checks the value of $kid, the return value from the fork. If $kid is true (non-zero, the child's pid), parent executes reading from STDIN printing to the server. If $kid is false (zero), the child executes reading from the server printing to STDOUT.

The following is the example session executing the client code in Listing 7, clientfork.pl connecting to the code in Listing 5, server2way.pl:

[james@client networking]$ clientfork.pl
server.onsight.com
NAME
server.onsight.com
DATE
Tue Oct 27 15:42:58 1998
HELP
DEFAULT
HELLO
Hi

When the parent process is finished reading from STDIN, it executes the kill function to kill the child process. It is very important the parent reap its child so that the child does not outlive the parent and become a zombie.

A Forking Server

Listing 8.

Servers usually don't handle only one client at a time. One approach to a server that can handle more than one client is a server that forks a child process to handle each client connection. Listing 8, serverfork.pl, is an example of a forking server.

One way for the parent process to reap its children is to define a subroutine and assign a reference to that subroutine to $SIG{CHLD}. (The hash %SIG is Perl's way of handling signals.) In this example, a subroutine named REAP is defined and a reference to this subroutine is assigned to $SIG{CHLD}. When the parent receives the CHLD (child terminated) signal, the REAP subroutine will be invoked.

Within the while loop that accepts all the client connections, the server forks. If the fork returns true, the parent is running and it executes the next statement which immediately transfers control to the continue block, performs the housecleaning step of closing the child socket and waits for the next client to connect. If the fork returns undef, then the fork failed, so the server dies. If the fork returns neither true nor undef, then the child is running, so the parent socket is closed and the child reads from the client and processes the client. When the child is finished processing the client, the child exits and is reaped by the parent.

Thread Programming in Perl5.005

Perl version 5.005 supports thread programming. This means a threaded networking program can be created to be either a server or a client.

Listings 9, 10, and 11 are three different versions of a client that logs into several web servers and determines the type of server being used (Apache, Netscape, etc).

Listing 9.

Listing 9, getservertype1.pl, shows a non-forking, non-threaded client. First, an array of hosts is created and initialized to a few web sites. The subroutine doit is defined to receive the web server name as an argument, open a client connection to that server at port 80 (the HTTP port), send the HTTP request and read each line of response. When the line starting Server: is read, it will extract the server name and store it in $1. Then the host name and web server name are printed. This subroutine is called for each host in the array of hosts.

Here is the output of getservertype1.pl:

processing www.ssc.com...
www.ssc.com: Stronghold/2.2 Apache/1.2.5 PHP/FI-2.0b12
processing www.linuxjournal.com...
www.linuxjournal.com: Stronghold/2.2 Apache/1.2.5 PHP/FI-2.0b12
processing www.perl.com...
www.perl.com: Apache/1.2.6 mod_perl/1.11
processing www.perl.org...
www.perl.org: Apache/1.2.5
processing www.nytimes.com...
www.nytimes.com: Netscape-Enterprise/2.01
processing www.onsight.com...
www.onsight.com: Netscape-Communications/1.12
processing www.avue.com...
www.avue.com: Netscape-Communications/1.12

Note that the hosts are processed in the same order as stored in @hosts.

Listing 10.

Listing 10, getservertype2.pl, is a forking version of getservertype1.pl. The forking occurs within the foreach loop. The fork is executed and if it returns true, the parent then executes the next statement to the next host name. If the fork returns undef, then the program dies. Otherwise, the child calls the doit function passing in the host, then exits. After the parent completes its work in the while loop, it waits for all child processes to finish, then exits.

Here is the output of getservertype2.pl:

processing www.ssc.com...
processing www.linuxjournal.com...
processing www.perl.com...
processing www.perl.org...
processing www.nytimes.com...
processing www.onsight.com...
processing www.avue.com...
www.onsight.com: Netscape-Communications/1.12
www.nytimes.com: Netscape-Enterprise/2.01
www.avue.com: Netscape-Communications/1.12
www.linuxjournal.com: Stronghold/2.2 Apache/1.2.5 PHP/FI-2.0b12
www.perl.com: Apache/1.2.6 mod_perl/1.11
www.ssc.com: Stronghold/2.2 Apache/1.2.5 PHP/FI-2.0b12
www.perl.org: Apache/1.2.5
Parent exiting...

Note that the hosts are not printed in the order stored in @hosts. They are printed in the order processed, the slower hosts taking longer than the faster ones.

Listing 11.

Listing 11, getservertype3.pl, is a threaded version. In the loop through the host names, a new Thread object is created. When creating the Thread, the new method is passed a reference to a subroutine that the thread will execute, as well as the arguments passed into that subroutine. The thread then executes its subroutine and when the subroutine returns, the thread is destroyed. Here is the output of getservertype3.pl:

processing www.ssc.com...
processing www.linuxjournal.com...
processing www.perl.com...
processing www.perl.org...
processing www.nytimes.com...
processing www.onsight.com...
processing www.avue.com...
www.nytimes.com: Netscape-Enterprise/2.01
www.onsight.com: Netscape-Communications/1.12
www.avue.com: Netscape-Communications/1.12
www.linuxjournal.com: Stronghold/2.2 Apache/1.2.5 PHP/FI-2.0b12
www.perl.com: Apache/1.2.6 mod_perl/1.11
www.ssc.com: Stronghold/2.2 Apache/1.2.5 PHP/FI-2.0b12
www.perl.org: Apache/1.2.5
______________________

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

INSOMNIA_THE MIDNIGHT PROGRAMMING CONTEST

Anonymous's picture

THE MIDNIGHT PROGRAMMING CONTEST...
www.insomnia.cognizance.org.in
Starts on : 27th March, 9:00 PM
Cash prizes worth Rs.30,000 on stake for this round.

(PS: Problems of previous rounds are available for practice.)

EOF character at Server?

PaulCEIRE's picture

I'm trying to build a server that will read on a string of characters, parse them and return an appropriate result to the client. The problem I have is that the application that will act as the client re-uses the same connection for all requests and there is no 'carriage return' or line feed at the end of each request, so my server does not know that the full request has been received and will just wait for an EOF character and hang.
I have re-created this scenario by building 2 client perl programs, 1 with the carriage return and 1 without.
As I have no control over how my application submits these strings I need to find a solution on the server side. The first 4 digits of each request string give the lentgh of the proceeding data, is there any way I can append and EOF character to the request data after I've received it on my server?

Client1:
$msg="00110100303C120";
$sock->send($msg);

Client2:
$msg="00110100303C120\n";
$sock->send($msg);

Server:
# While there is data to read from the connection
while (defined ($buf = <$new_sock>)) {
print $buf;
}

Thanks

Just read the first 4 bytes

jlhm's picture

Just read the first 4 bytes using:

read($new_sock, $length. 4);

then use the $length to read the actual data:
You might need to convert the $length data to actual decimal value if needed using unpack().

read($new_sock, $data, $length);

network programing with perl

Anonymous's picture

hi,
i am a newbie in perl programing. i found this client-server program on the net and wanted to try it out. The client will simply connect to the server and send a line "hello there" to the server which will then print it out there.

############################
client
#############################

#!/usr/bin/perl
# client.plx
use warnings;
use strict;
use IO::Socket;

my $client = IO::Socket::INET->new( 
					PeerAddr => 'localhost',
					PeerPort => '7890',
					Proto => 'tcp'
                                  )or die "could not create socket: $!\n";

print "hi\n";
print $client "Hello there!\n";
close $client;
###########################

but the problem is that whenever i try to run the client, it says "could not create socket: Unkown error". the server runs perfectly. can someone pls point out the mistake here. btw i am currently referring "beginner's perl : simon cozens" and the client program given there are the same as above.

thank you.

I get the same error

Anonymous's picture

I get the same error sometime, to solve it I need to enter in the actual IP address of my host instead of 'localhost', ie.:
PeerAddr => '127.0.0.1',
PeerPort => '7890',
Proto => 'tcp'

how to keep client session open in Perl?

Thuy's picture

I have a perl script while telnet to a remote machine from linux local machine, run commands, and return the results.
After the perl script is run completely, the telnet session is closed.
But, I want to keep the tenet session open. Can anyone tell me how to do it in Perl programs?

server2way.pl and client2way.pl

tcltkdev's picture

server2way.pl (Listing 5) and client2way.pl (Listing 6) works fine if both are ran on my pc but when I ran server2way.pl on another pc on the network (both Windows XP) I get this error on the client side:

no socket :Unknown error at client2way.pl line 14.

and this is line 14:

$sock or die "no socket :$!"; # Connect to server

I want to use this in our on going project.

Please help.

Network programming -

Anonymous's picture

Few questions -

How do I know what ports (possible no of ports also) to use for the server and the client over TCP/IP, is there some range? We are using c++ for network/socket programming.

Thanks for your help.

Odd accept() behavior

Owen LaGarde's picture

I noticed this when I converted Listings 6 (client) and 8 (server) to use EOLN delimited IO
(to better examine the behavior of concurrent stream sessions). This was initially done
because Listing 8 did not wait for new sessions after the first was closed. After the 1st
client exited accept() would return undef until another request was queued but didn't block
while waiting for the queue event. Eventually I just wrapped the outer while() in another
"while( ; sleep 2)" to give myself time to check the socket queue and match
incoming requests to accept() results. This revealed yet more oddities. Note that the
'Listen' parameter is set to SOMAXCONN (128 for my RHEL box and perl 5.8.5) for both forms
of new() socket operators. Any ideas on the following 3 oddities?

- Using IO::Socket::INET sockets -- the third concurrent session request hangs unprocessed
until one of the previous [still open] sessions exit, ie., when more than 2 open sessions
exist accept() blocks until there is only 1 open session, then processes the next waiting
request as expected.

- Using IO::Socket::UNIX sockets -- many concurrent sessions can be opened but once any of
these are closed accept() hangs until all other sessions are closed, ie., if open sessions
exist and one is closed accept() blocks until all are closed, then processes request(s) --
but not as expected, see bullet 3 below.

- Using either form of sockets -- prior to opening any sessions accept() blocks, waits for
requests to queue, and processes the first queued request as expected, but subsequent calls
to accept() return undef unless another request is queued, ie., accept() behaves as if the
O_NONBLOCK bit is being automatically set on the original socket at or after the first call.

###################################################
## The Client
###################################################

#!/usr/bin/perl -w
use strict;
use IO::Socket;
#use Carp;
#use Data::Dumper;

# Domain sockets
my $sock = new IO::Socket::UNIX( Peer => '/tmp/mysock',
Type => SOCK_STREAM ) or die $!;

## IP sockets
#my $sock = new IO::Socket::INET( PeerAddr => 'localhost', PeerPort => '7890',
# Proto => 'tcp'); $sock or die "no socket :$!";

my $buf;
while( $buf = <> ) {
chomp $buf;
print $sock "$buf\n";
print "$0 ($$): ", scalar localtime(), " sent: [$buf]\n";
$buf = scalar <$sock>;
chomp $buf;
print "$0 ($$): ", scalar localtime(), " received: [$buf]\n";
}
close $sock;

###################################################
## The server
###################################################

#!/usr/bin/perl -w
use strict;
use IO::Socket;
use POSIX qw(:sys_wait_h);
#use Carp;
#use Data::Dumper;

sub REAP {
1 until( -1 == waitpid( -1, WNOHANG ) );
$SIG{CHLD} = \&REAP;
}
$SIG{CHLD} = \&REAP;

# Domain sockets
unlink '/tmp/mysock';
my $sock = new IO::Socket::UNIX( Local => '/tmp/mysock', Type => SOCK_STREAM,
Listen => SOMAXCONN, Reuse => 1 ) or die $!;

## IP sockets
#my $sock = new IO::Socket::INET( LocalHost => 'localhost', LocalPort => 7890,
# Proto => 'tcp', Listen => SOMAXCONN, Reuse => 1) or die $!;

STDOUT->autoflush(1);

my( $new_sock, $child, $buf );
while( 1 ) {
while( $new_sock = $sock->accept() ) {
if( $child = fork ) {
close $new_sock
} elsif( defined( $child ) ) {
close $sock;
while( defined( $buf = <$new_sock> ) ) {
chomp $buf;
print "$0 ($$): ", scalar localtime(), " received: [$buf]\n";
sleep 1;
print "$0 ($$): ", scalar localtime(), " sending: [$buf]\n";
print $new_sock "$buf\n";
}
exit
} else {
die "$0 ($$): fork: $!"
}
}
sleep 2
}

###################################################
## END
###################################################

Re: odd accept() behavior

Owen LaGarde's picture

BTY: no, it isn't the "while( defined( $buf = <$new_sock> ) )..." at line 32 of the server, where the child proc reads the copied socket. Yes, changing this to an if() will remove the behavior -- by enforcing line-only processing of the socket contents -- but this doesn't explain why the behavior only occurs when sessions=3 for IP sockets or when the first child exit occurs for Domain sockets.

clientfork.pl is not working on XP

lakshmi's picture

hi there,

I'm trying to execute client fork.pl on my xp machine with both client and server running on the same machine but it doesn't seem to be working. when i enter input at client, it simply accepts the input and then nothing happens.

Can anybody help me with this.

Thanks in advance,
lakshmi.

Sending binay data to the server

Anonymous's picture

Can the print statement be used to send binary data to the server? For example: to send the message to the server, you would have a statement PRINT $client $message.

If $message = 0xFF ;

what would be sent to the server? Is it the ASCII representation of the number 255? How can I send the 8-bit binary representation of the numner 255 and not one byte for 2, one byte for 5, and another byte for 5 ? Any help would be appreciated.
Thanks.

perl send binary through socket

Anonymous's picture

You could use bytes module see http://perldoc.perl.org/bytes.html

Minor correction to server1.pl example

Allan's picture

There is a minor error at the end of the server1.pl example:
# send them a message, close connection
print CLIENT "Hello from the server: ",
close CLIENT;

As stated, there is a write to closed socket error and no message passed to the client. One fix is to replace the "," with ";" on the print line.

Another fix

RyDer's picture

Another fix is to put this on the end of the line:

print CLIENT "Hello from the server: ", scalar localtime, "\n";

because on the response of the server to the client shows a time stamp.

Be care.

Another fix

RyDer's picture

Another fix is to put this on the end of the line:

print CLIENT "Hello from the server: ", scalar localtime, "\n";

because on the response of the server to the client shows a time stamp.

Be care.

A very good example set for

Shilpa's picture

A very good example set for beginners. I am interested in knowing more about how I could use perl and start a program as server and which takes an input text from client and processes it and returns the result to client. Something using VEC and SELECT per commanda. I am woring on this code I have which uses all this. If you have any examples for that please let me know.

Thanks,

Absolute Good thing.. Thanks

Anonymous's picture

Absolute Good thing.. Thanks for all the info presented.

Fork vs Thread

lintastic's picture

This tutorial was excelent. It was clear, concise, and informative. Although, I did have one question. What are the bennefits of using fork over thread? In most cases I know that creating a thread has most of the same bennefits of using fork but without the extra overhead. Why would one want to use fork in a web server as opposed to thread? Thanks!

Fantastic

jayanthi's picture

I really got interested in learning network programming after looking in to this article. It is really great on you. Can you suggest us the sites used for learning in depth of network programming with the real time examples

great help

jatin patni's picture

Hi, i have been looking around lately for some networking related tutorials and i admit that this place is actually exactly what i was looking for.
Thanks.

PS: Beginner in perl.

Re: Strictly On-Line: Network Programming with Perl

Anonymous's picture

Hi James,

I'm very new to PERL. I want to know about socket programming in PERL. I have to make chat module in my site. where is any user want to chat with somebody then he/se can be able to send alert message to that perticular user. if he/she accept the request then new chat window should open both side without any page refreshment.
I hope your guidance will be important for me.

regards,

Bharat

Re: Strictly On-Line: Network Programming with Perl

Anonymous's picture

a good guide to the world of the networks!

Re: Strictly On-Line: Network Programming with Perl

Anonymous's picture

I was really in big trouble with perl' sockets programming, this tutorial was proved to be a big help for me to get stated writing programs that use sockets in perl.....thnx for such a nice tutorial...

Re: Strictly On-Line: Network Programming with Perl

Anonymous's picture

Great article, most of it worked on my FBSD server to.

Thanks alot, keep up the good work.

- Stian

Re: Strictly On-Line: Network Programming with Perl

Anonymous's picture

Hi James,

A very informative site. Also very elegantly written. Easy to understand and implement.

Was a big help to me.

Thanks a bunch.

Cheers,

Arun Krishnan

HI

Anonymous's picture

Yes really Arun
I'm very new to this Socket programming in perl
Please let me know , if you are familiar with this
Thanks and regards
Jey

good job

kararu's picture

Keep up the good job of showing a wonderful way to perl network programming beginners.

Hey! This one's simply too

Shraddha's picture

Hey!

This one's simply too good...was yearning for something on networking concepts...couldn ask better than this one.

Lets keep the good work coming!

Best Regards,
Shraddha

Webinar
One Click, Universal Protection: Implementing Centralized Security Policies on Linux Systems

As Linux continues to play an ever increasing role in corporate data centers and institutions, ensuring the integrity and protection of these systems must be a priority. With 60% of the world's websites and an increasing share of organization's mission-critical workloads running on Linux, failing to stop malware and other advanced threats on Linux can increasingly impact an organization's reputation and bottom line.

Learn More

Sponsored by Bit9

Webinar
Linux Backup and Recovery Webinar

Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.

In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.

Learn More

Sponsored by Storix