Socket Programming in PERL - PERL Makes Life Easy (Page 4 of 5 )
Those scripts were a lot of PERL code just to send a few bytes of information across. It is possible to write some cute and short socket programs by using OO Concepts and the IO::Socket module. We shall see now, how it is possible.
Sample Script Server Using IO 1. #!/usr/bin/perl -w
2. # serIO.pl
3. # server using IO::Socket
4. #---------------------
5. use strict;
6. use IO::Socket;
7. my $sock = new IO::Socket::INET(
LocalHost => 'localhost',
LocalPort => 7890,
Proto => 'tcp',
Listen => SOMAXCONN,
Reuse => 1);
8. $sock or die "no socket :$!";
9. my($new_sock, $c_addr, $buf);
10. while (($new_sock, $c_addr) = $sock->accept())
11. {
my ($client_port, $c_ip) =sockaddr_in($c_addr);
my $client_ipnum = inet_ntoa($c_ip);
my $client_host =gethostbyaddr($c_ip, AF_INET);
print "got a connection from: $client_host"," [$client_ipnum] ";
while (defined ($buf = <$new_sock>))
{
print $buf;
}
12. } Analysis Line 7 A new IO::Socket::INET object is created using the new method The new method returns a socket that is assigned to $sock
Line 10 A client connection is accepted using the accept method. The accept method returns the client socket when evaluated in scalar context and the client's socket and IP address when evaluated in list context.
Sample Client Script Using IO 1. #!/usr/bin/perl -w
2. # cliIO.pl
3. # a simple client using IO:Socket
4. #----------------
5. use strict;
6. use IO::Socket;
7. my $host = shift || 'localhost';
8. my $port = shift || 7890;
9. my $sock = new IO::Socket::INET( PeerAddr => $host, PeerPort => $port, Proto => 'tcp');
10. $sock or die "no socket :$!";
11. foreach my $i (1..10)
12. {
13. print $sock "$i",scalar(localtime)," ";
14. sleep(1);
15. }
16. close $sock; Analysis A new object is created which connects to the server and sends 10 timestamp strings with 1 second delay in between.
Output of the new cute scripts:
D:GalantPerl>start perl serIO.pl
D:GalantPerl>cliIO.pl On the server window:
got a connection from: RAHUL [127.0.0.1]
1Thu Feb 6 12:52:57 2003
2Thu Feb 6 12:52:58 2003
3Thu Feb 6 12:52:59 2003
4Thu Feb 6 12:53:00 2003
5Thu Feb 6 12:53:01 2003
6Thu Feb 6 12:53:02 2003
7Thu Feb 6 12:53:03 2003
8Thu Feb 6 12:53:04 2003
9Thu Feb 6 12:53:05 2003
10Thu Feb 6 12:53:06 2003 Pinging in PERL Perl makes life easy, with it various modules available. Let's demonstrate this by writing a short and quick ping program.
Sample Ping Program 1. #!/usr/bin/perl
2. # ping.pl
3. # a simple ping program
4. #---------------
5. use Net::Ping;
6. $pinghost = shift||"gsmgprs";
7. print "Pinging $pinghost ";
8. $p = Net::Ping->new("icmp", 2);
9. for (;;) # infinite loop
10. {
11. unless ($p->ping($pinghost))
12. {
13. print "Fail: ", scalar(localtime), " ";
14. } else
15. {
16. print "Success: ", scalar(localtime), " ";
17. }
18. sleep 10;
19. } Analysis The Net::Ping makes pinging remote hosts easy and quick. An object of the class created. Here we use the icmp protocol (you can also choose tcp or udp).
A sample output:
D:GalantPerl>ping.pl
Pinging gsmgprs
Success: Thu Feb 6 14:22:01 2003
Success: Thu Feb 6 14:22:11 2003
Success: Thu Feb 6 14:22:21 2003
Success: Thu Feb 6 14:22:31 2003
Success: Thu Feb 6 14:22:41 2003
Success: Thu Feb 6 14:22:51 2003
Success: Thu Feb 6 14:23:01 2003
Success: Thu Feb 6 14:23:11 2003
Success: Thu Feb 6 14:23:21 2003
Success: Thu Feb 6 14:23:31 2003Next: Conclusion >>
More Perl Articles
More By Rahul Chauhan