Perl
  Home arrow Perl arrow Page 4 - Socket Programming in PERL
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PERL

Socket Programming in PERL
By: Rahul Chauhan
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 99
    2003-02-18


    Table of Contents:
  • Socket Programming in PERL
  • Types of Sockets
  • Client–Server Script in PERL
  • PERL Makes Life Easy
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    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 2003


     
     
    >>> More Perl Articles          >>> More By Rahul Chauhan
     

       

    PERL ARTICLES

    - More Perl Bits
    - Perl, Bit by Bit
    - Basic Charting with Perl
    - Using Getopt::Long: More Command Line Option...
    - Command Line Options in Perl: Using Getopt::...
    - Web Access with LWP
    - More Templating Tools for Perl
    - Site Layout with Perl Templating Tools
    - Build a Perl RSS Aggregator with Templating ...
    - Looping, Security, and Templating Tools
    - Perl: Bon Voyage Lists and Hashes
    - Templating Tools
    - Perl: Number Crunching
    - Perl Debuggers in Detail
    - Debugging Perl





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT