Network Programming with Perl
Listing 2, client1.pl, shows a simple client. The first command-line argument to this program is the host name to which it should connect, which defaults to server.onsight.com. The second command-line argument is the port number which defaults to 7890.
The host name and the port number are used to generate the port address using inet_aton (ASCII to numeric) and sockaddr_in. A socket is then created using socket and the client connects the socket to the port address using connect.
The while loop then reads the data the server sends to the client until the end-of-file is reached, printing this input to STDOUT. Then the socket is closed.
Let's assume all of the clients are started on the the machine named client.avue.com, although they could be executed from any machine on the network. To execute the client, type:
[james@client networking]$ client1.pl server.onsight.com Hello from the server: Tue Oct 27 09:48:40 1998
The following is the standard output from the server:
got a connection from: client.avue.com [192.168.1.2]
Creating sockets using the functions described above is good when you want to control how the socket is created, the protocol to be used, etc. But using the functions above is too hard; I prefer the easy way—IO::Socket.
The module IO::Socket provides an easy way to create sockets which can then be used like file handles. If you don't have it installed on your machine, it can be found on CPAN. To see this module's POD, type:
perldoc IO::Socket
Listing 3, serverIO.pl, is a simple server using IO::Socket. A new IO::Socket::INET object is created using the new method. Note that the arguments to the method include the host name, port number, protocol, queue length and an option indicating we want this port to be immediately reusable. The new method returns a socket that is assigned to $sock. This socket can be used like a file handle—we can either read the client output from it, or write to it by sending data to the client.
A client connection is accepted using the accept method. Note the accept method returns the client socket when evaluated in scalar context:
$new_sock = $sock->accept()
and returns the client's socket and the client's IP address when evaluated in list context:
($new_sock, $client_addr) = $sock->accept()The client address is computed and printed the same as in Listing 1, server1.pl. Then the socket associated with that client is read until end-of-file. The data read is printed to STDOUT. This example illustrates that the server can read from a client using < > around the socket variable.
Listing 4, clientIO.pl, is a simple client using IO::Socket. This time, a new object is created that connects to a host at a port using the TCP protocol. Ten strings are then printed to that server, then the socket is closed.
If the server in Listing 3, serverIO.pl, is executed and then the client Listing 4, clientIO.pl, connects, the output would be:
[james@server networking]$ serverIO.pl
got a connection from: client.avue.com [192.168.1.2] hello, world: 1 hello, world: 2 hello, world: 3 hello, world: 4 hello, world: 5 hello, world: 6 hello, world: 7 hello, world: 8 hello, world: 9 hello, world: 10
It is possible to create servers and clients that communicate with one another in both directions. For instance, the client may send information to the server, then the server may send information back to the client. Therefore, network programs can be written so that the server and client follow some predetermined protocol.
Listing 5, server2way.pl, shows how a simple server can be created to read a command from a client, then print out an appropriate response to the client. The module Sys::Hostname provides a function named hostname that returns the host name of the server. To insure output is seen as we print, IO buffering is turned off for the STDOUT file handle using the autoflush function. Then a while loop is executed that accepts connections. When a client connects, the server reads a line from the client, chopping off the newline character. Then a switch statement is executed. (The switch is cleverly disguised as a foreach loop, which happens to be one of my favorite ways of writing a switch.) Depending on the input entered by the client, the server outputs an appropriate response. All lines from the client are read until end-of-file.
Listing 6, client2way.pl, shows the companion client. A connection to the server is made, then the client prints a few commands to the server reads the response and prints the response to STDOUT.
The following is the output of the client code in Listing 6:
[james@client networking]$ client2way.pl server.onsight.com Hi server.onsight.com Tue Oct 27 15:36:19 1998 DEFAULT
Today’s modular x86 servers are compute-centric, designed as a least common denominator to support a wide range of IT workloads. Those generic, virtualized IT workloads have much different resource optimization requirements than hyperscale and cloud applications. They have resulted in a “one size fits all” enterprise IT architecture that is not optimized for a specific set of IT workloads, and especially not emerging hyperscale workloads, such as web applications, big data, and object storage. In this report, you will learn how shifting the focus from traditional compute-centric IT architectures to an innovative disaggregated fabric-based architecture can optimize and scale your data center.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
| Non-Linux FOSS: Seashore | May 10, 2013 |
| Trying to Tame the Tablet | May 08, 2013 |
- RSS Feeds
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Using Salt Stack and Vagrant for Drupal Development
- New Products
- Validate an E-Mail Address with PHP, the Right Way
- Drupal Is a Framework: Why Everyone Needs to Understand This
- A Topic for Discussion - Open Source Feature-Richness?
- Tech Tip: Really Simple HTTP Server with Python
- Home, My Backup Data Center
- Download the Free Red Hat White Paper "Using an Open Source Framework to Catch the Bad Guy"
- Android is Linux -- why no better inter-operation
27 min 32 sec ago - Connecting Android device to desktop Linux via USB
56 min 2 sec ago - Find new cell phone and tablet pc
1 hour 54 min ago - Epistle
3 hours 22 min ago - Automatically updating Guest Additions
4 hours 31 min ago - I like your topic on android
5 hours 18 min ago - Reply to comment | Linux Journal
5 hours 39 min ago - This is the easiest tutorial
11 hours 53 min ago - Ahh, the Koolaid.
17 hours 32 min ago - git-annex assistant
23 hours 31 min ago
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Pi Cobbler Breakout Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Free Webinar: Linux Backup and Recovery
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.




Comments
INSOMNIA_THE MIDNIGHT PROGRAMMING CONTEST
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?
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
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
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
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?
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
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 -
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
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
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
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
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
You could use bytes module see http://perldoc.perl.org/bytes.html
Minor correction to server1.pl example
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
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
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
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
Absolute Good thing.. Thanks for all the info presented.
Fork vs Thread
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
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
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
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
a good guide to the world of the networks!
Re: Strictly On-Line: Network Programming with Perl
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
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
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
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
Keep up the good job of showing a wonderful way to perl network programming beginners.
Hey! This one's simply too
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