Perl Programming
                 Course
            Network programming




Krassimir Berov

I-can.eu
Contents
1. Sockets, servers and clients
2. TCP/IP, UDP sockets
3. Unix Domain Socket (UDS) sockets
4. IO::Socket
5. HTTP and SMTP
6. LWP and WWW::Mechanize
Sockets, servers and clients

• Socket
  • an end-point of a bi-directional communication link
    in the Berkeley sockets API
  • Berkeley sockets API allows communications
    between hosts or between processes on one
    computer
  • One of the fundamental technologies underlying the
    Internet.
  • A network socket is a communication end-point
    unique to a machine communicating on an Internet
    Protocol-based network, such as the Internet.
  • Unix domain socket, an end-point
    in local inter-process communication
Sockets, servers and clients

• Server
  • An application or device that performs
    services for connected clients as part of a
    client-server architecture
  • An application program that accepts
    connections in order to service requests by
    sending back responses
  • Example:
    web servers(Apache), e-mail servers(Postfix),
    file servers
Sockets, servers and clients

• Client
  • An application or system that accesses a
    (remote) service on another computer
    system known as a server
  • Example:
    Web browsers (Internet Explorer, Opera,
    Firefox)
    Mail Clients (Thunderbird, KMail, MS
    Outlook)
TCP/IP, UDP sockets
• An Internet socket is composed of
  • Protocol (TCP, UDP, raw IP)
  • Local IP address
  • Local port
  • Remote IP address
  • Remote port
Unix Domain Socket
• A Unix domain socket (UDS)
  or IPC socket
  • inter-process communication socket
  • a virtual socket
  • similar to an internet socket
  • used in POSIX operating systems for inter-
    process communication
     • connections appear as byte streams, much like
       network connections,
     • all data remains within the local computer
IO::Socket
• IO::Socket - Object interface to socket
  communications
  • built upon the IO::Handle interface and inherits
    all its methods
  • only defines methods for operations common to
    all types of socket
  • Operations, specified to a socket in a particular
    domain have methods defined in sub classes
  • will export all functions (and constants) defined
    by Socket
IO::Socket
• IO::Socket – Example (TCP/IP)
  #io-socket-tcp-client.pl
  use IO::Socket;
  my $sock = IO::Socket::INET->new(
      PeerAddr => $host,
      PeerPort => "$port",
      Proto => 'tcp',
      Timeout => 60
  ) or die $@;

  die "Could not connect to $host$/"
      unless $sock->connected;

  #...
IO::Socket
• IO::Socket – Example 2 (TCP/IP)
  #io-socket-tcp-server.pl
  use IO::Socket;
  my ($host, $port, $path ) = ( 'localhost', 8088 );
  my $server = new IO::Socket::INET (
       LocalAddr => $host,
       LocalPort => $port,
       Proto => 'tcp',
       Listen => 10,
       Type      => SOCK_STREAM,
       ReuseAddr => 1
  );
  print "Server ($0) running on port $port...n";
  while (my $connection = $server->accept) {
  #...
IO::Socket
• IO::Socket – Example (UDS)
 #io-socket-uds-server.pl
 use IO::Socket;
 my $file = "./udssock";
 unlink $file;
 my $server = IO::Socket::UNIX->new(
     Local => $file,
     Type   => SOCK_STREAM,
     Listen => 5
 ) or die $@;

 print "Server running on file $file...n";
 #...
IO::Socket
• IO::Socket – Example 2 (UDS)
 #io-socket-uds-client.pl
 use IO::Socket;
 my $server = IO::Socket::UNIX->new(
     Peer => "./udssock",
 ) or die $@;

 # communicate with the server
 print "Client connected.n";
 print "Server says: ", $server->getline;
 $server->print("Hello from the client!n");
 $server->print("And goodbye!n");
 $server->close;
 #...
IO::Socket
• IO::Socket – Example (UDP)
 #io-socket-udp-server.pl
 use IO::Socket;
 my $port = 4444;
 my $server = new IO::Socket::INET(
    LocalPort => $port,
    Proto     => 'udp',
 );
 die "Bind failed: $!n" unless $server;
 print "Server running on port $port...n";
 #...
IO::Socket
• IO::Socket – Example 2 (UDP)
 #io-socket-udp-client.pl
 use IO::Socket;
 my $host = 'localhost';
 my $port = 4444;
 my $client = new IO::Socket::INET(
      PeerAddr => $host,
      PeerPort => $port,
      Timeout => 2,
      Proto    => 'udp',
 );
 $client->send("Hello from client") or die "Send: $!n";
 my $message;
 $client->recv($message, 1024, 0);
 print "Response was: $messagen";
 #...
HTTP and SMTP
• HTTP
• SMTP
Sending Mail
• Net::SMTP
 #smtp.pl
 use Net::SMTP;
 my $smtp = Net::SMTP->new(
     Host => 'localhost',
     Timeout => 30,
     Hello =>'localhost',
 );
 my $from = 'me@example.com';
 my @to   = ('you@example.org',);
 my $text = $ARGV[0]|| 'проба';
 my $mess = "ERROR: Can't send mail using Net::SMTP. ";
 $smtp->mail( $from ) || die $mess;
 $smtp->to( @to, { SkipBad => 1 } ) || die $mess;
 $smtp->data( $text ) || die $mess;
 $smtp->dataend() || die $mess;
 $smtp->quit();
LWP and WWW::Mechanize
• LWP - The World-Wide Web library for Perl
  • provides a simple and consistent application
    programming interface (API)
    to the World-Wide Web
  • classes and functions that allow you to write
    WWW clients
  • also contains modules that help you implement
    simple HTTP servers
  • supports access to http, https, gopher, ftp, news,
    file, and mailto resources
  • transparent redirect, parser for robots.txt files
    etc...
LWP and WWW::Mechanize
• WWW::Mechanize - Handy web browsing in a
  Perl object
  • helps you automate interaction with a website

  • well suited for use in testing web applications

  • is a proper subclass of LWP::UserAgent

  • you can also use any of the LWP::UserAgent's
    methods.
LWP and WWW::Mechanize
• WWW::Mechanize - Example
  • helps you automate interaction with a website

  • well suited for use in testing web applications

  • is a proper subclass of LWP::UserAgent

  • you can also use any of the LWP::UserAgent's
    methods.
LWP and WWW::Mechanize
• Example – getting product data from a site
 #www_mech.pl
 use strict; use warnings;
 use MyMech;
 use Data::Dumper;
 #Config
 $MyMech::Config->{DEBUG}=0;
 my $Config = $MyMech::Config;
 #print Dumper($Config);
 my $mech = MyMech->new(
      agent      => $Config->{agent},
      cookie_jar => $Config->{cookie_jar},
      autocheck => $Config->{autocheck},
      onwarn     => $Config->{onwarn}
 );
 #...
Network programming
• Resources
  • Beginning Perl
    (Chapter 14 – The World of Perl/IPC and
    Networking)
  • Professional Perl Programming
    (Chapter 23 – Networking with Perl)
  • perldoc perlipc
  • perldoc IO::Socket
  • http://en.wikipedia.org/wiki/Berkeley_sockets
  • Perl & LWP (http://lwp.interglacial.com/)
Network programming




Questions?

Network programming

  • 1.
    Perl Programming Course Network programming Krassimir Berov I-can.eu
  • 2.
    Contents 1. Sockets, serversand clients 2. TCP/IP, UDP sockets 3. Unix Domain Socket (UDS) sockets 4. IO::Socket 5. HTTP and SMTP 6. LWP and WWW::Mechanize
  • 3.
    Sockets, servers andclients • Socket • an end-point of a bi-directional communication link in the Berkeley sockets API • Berkeley sockets API allows communications between hosts or between processes on one computer • One of the fundamental technologies underlying the Internet. • A network socket is a communication end-point unique to a machine communicating on an Internet Protocol-based network, such as the Internet. • Unix domain socket, an end-point in local inter-process communication
  • 4.
    Sockets, servers andclients • Server • An application or device that performs services for connected clients as part of a client-server architecture • An application program that accepts connections in order to service requests by sending back responses • Example: web servers(Apache), e-mail servers(Postfix), file servers
  • 5.
    Sockets, servers andclients • Client • An application or system that accesses a (remote) service on another computer system known as a server • Example: Web browsers (Internet Explorer, Opera, Firefox) Mail Clients (Thunderbird, KMail, MS Outlook)
  • 6.
    TCP/IP, UDP sockets •An Internet socket is composed of • Protocol (TCP, UDP, raw IP) • Local IP address • Local port • Remote IP address • Remote port
  • 7.
    Unix Domain Socket •A Unix domain socket (UDS) or IPC socket • inter-process communication socket • a virtual socket • similar to an internet socket • used in POSIX operating systems for inter- process communication • connections appear as byte streams, much like network connections, • all data remains within the local computer
  • 8.
    IO::Socket • IO::Socket -Object interface to socket communications • built upon the IO::Handle interface and inherits all its methods • only defines methods for operations common to all types of socket • Operations, specified to a socket in a particular domain have methods defined in sub classes • will export all functions (and constants) defined by Socket
  • 9.
    IO::Socket • IO::Socket –Example (TCP/IP) #io-socket-tcp-client.pl use IO::Socket; my $sock = IO::Socket::INET->new( PeerAddr => $host, PeerPort => "$port", Proto => 'tcp', Timeout => 60 ) or die $@; die "Could not connect to $host$/" unless $sock->connected; #...
  • 10.
    IO::Socket • IO::Socket –Example 2 (TCP/IP) #io-socket-tcp-server.pl use IO::Socket; my ($host, $port, $path ) = ( 'localhost', 8088 ); my $server = new IO::Socket::INET ( LocalAddr => $host, LocalPort => $port, Proto => 'tcp', Listen => 10, Type => SOCK_STREAM, ReuseAddr => 1 ); print "Server ($0) running on port $port...n"; while (my $connection = $server->accept) { #...
  • 11.
    IO::Socket • IO::Socket –Example (UDS) #io-socket-uds-server.pl use IO::Socket; my $file = "./udssock"; unlink $file; my $server = IO::Socket::UNIX->new( Local => $file, Type => SOCK_STREAM, Listen => 5 ) or die $@; print "Server running on file $file...n"; #...
  • 12.
    IO::Socket • IO::Socket –Example 2 (UDS) #io-socket-uds-client.pl use IO::Socket; my $server = IO::Socket::UNIX->new( Peer => "./udssock", ) or die $@; # communicate with the server print "Client connected.n"; print "Server says: ", $server->getline; $server->print("Hello from the client!n"); $server->print("And goodbye!n"); $server->close; #...
  • 13.
    IO::Socket • IO::Socket –Example (UDP) #io-socket-udp-server.pl use IO::Socket; my $port = 4444; my $server = new IO::Socket::INET( LocalPort => $port, Proto => 'udp', ); die "Bind failed: $!n" unless $server; print "Server running on port $port...n"; #...
  • 14.
    IO::Socket • IO::Socket –Example 2 (UDP) #io-socket-udp-client.pl use IO::Socket; my $host = 'localhost'; my $port = 4444; my $client = new IO::Socket::INET( PeerAddr => $host, PeerPort => $port, Timeout => 2, Proto => 'udp', ); $client->send("Hello from client") or die "Send: $!n"; my $message; $client->recv($message, 1024, 0); print "Response was: $messagen"; #...
  • 15.
    HTTP and SMTP •HTTP • SMTP
  • 16.
    Sending Mail • Net::SMTP #smtp.pl use Net::SMTP; my $smtp = Net::SMTP->new( Host => 'localhost', Timeout => 30, Hello =>'localhost', ); my $from = 'me@example.com'; my @to = ('you@example.org',); my $text = $ARGV[0]|| 'проба'; my $mess = "ERROR: Can't send mail using Net::SMTP. "; $smtp->mail( $from ) || die $mess; $smtp->to( @to, { SkipBad => 1 } ) || die $mess; $smtp->data( $text ) || die $mess; $smtp->dataend() || die $mess; $smtp->quit();
  • 17.
    LWP and WWW::Mechanize •LWP - The World-Wide Web library for Perl • provides a simple and consistent application programming interface (API) to the World-Wide Web • classes and functions that allow you to write WWW clients • also contains modules that help you implement simple HTTP servers • supports access to http, https, gopher, ftp, news, file, and mailto resources • transparent redirect, parser for robots.txt files etc...
  • 18.
    LWP and WWW::Mechanize •WWW::Mechanize - Handy web browsing in a Perl object • helps you automate interaction with a website • well suited for use in testing web applications • is a proper subclass of LWP::UserAgent • you can also use any of the LWP::UserAgent's methods.
  • 19.
    LWP and WWW::Mechanize •WWW::Mechanize - Example • helps you automate interaction with a website • well suited for use in testing web applications • is a proper subclass of LWP::UserAgent • you can also use any of the LWP::UserAgent's methods.
  • 20.
    LWP and WWW::Mechanize •Example – getting product data from a site #www_mech.pl use strict; use warnings; use MyMech; use Data::Dumper; #Config $MyMech::Config->{DEBUG}=0; my $Config = $MyMech::Config; #print Dumper($Config); my $mech = MyMech->new( agent => $Config->{agent}, cookie_jar => $Config->{cookie_jar}, autocheck => $Config->{autocheck}, onwarn => $Config->{onwarn} ); #...
  • 21.
    Network programming • Resources • Beginning Perl (Chapter 14 – The World of Perl/IPC and Networking) • Professional Perl Programming (Chapter 23 – Networking with Perl) • perldoc perlipc • perldoc IO::Socket • http://en.wikipedia.org/wiki/Berkeley_sockets • Perl & LWP (http://lwp.interglacial.com/)
  • 22.