SlideShare a Scribd company logo
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?

More Related Content

PPTX
File and directory
PPT
Introduction to Web Programming - first course
PPT
MYSQL - PHP Database Connectivity
PPT
Lecture 1 data structures and algorithms
PPT
Free space managment46
PPT
Threads And Synchronization in C#
PDF
Loops in JavaScript
File and directory
Introduction to Web Programming - first course
MYSQL - PHP Database Connectivity
Lecture 1 data structures and algorithms
Free space managment46
Threads And Synchronization in C#
Loops in JavaScript

What's hot (20)

PPT
JQuery introduction
PPT
Developing an ASP.NET Web Application
PPTX
Linux security
PPT
Intrusion Detection System using Snort
PPT
Php Lecture Notes
PDF
JAVA PROGRAMMING - The Collections Framework
PPTX
Database Connectivity in PHP
PPTX
Form using html and java script validation
PDF
Client-side JavaScript
PDF
CS9222 Advanced Operating System
PPTX
Basics of shell programming
PPSX
Javascript variables and datatypes
PPTX
presentation in html,css,javascript
PPTX
SQL Functions
PPTX
PPT
Javascript arrays
PPTX
Bootstrap - Basics
JQuery introduction
Developing an ASP.NET Web Application
Linux security
Intrusion Detection System using Snort
Php Lecture Notes
JAVA PROGRAMMING - The Collections Framework
Database Connectivity in PHP
Form using html and java script validation
Client-side JavaScript
CS9222 Advanced Operating System
Basics of shell programming
Javascript variables and datatypes
presentation in html,css,javascript
SQL Functions
Javascript arrays
Bootstrap - Basics
Ad

Viewers also liked (17)

PDF
PPTX
Aulas de Java Avançado 2- Faculdade iDez 2010
PPTX
Pyhug zmq
PDF
Aplicações Web Ricas e Acessíveis
PDF
Linguagem PHP
PDF
Linguagem PHP para principiantes
PDF
Módulo-6-7-ip-com-sockets
PDF
Tecnologia java para sockets
PPT
Redes 1 - Sockets em C#
PPTX
Socket programming with php
DOCX
correção Ficha 4,5,6,e 7
PDF
Programming TCP/IP with Sockets
PPT
Basic socket programming
PPT
Socket programming
PPT
Socket Programming Tutorial
Aulas de Java Avançado 2- Faculdade iDez 2010
Pyhug zmq
Aplicações Web Ricas e Acessíveis
Linguagem PHP
Linguagem PHP para principiantes
Módulo-6-7-ip-com-sockets
Tecnologia java para sockets
Redes 1 - Sockets em C#
Socket programming with php
correção Ficha 4,5,6,e 7
Programming TCP/IP with Sockets
Basic socket programming
Socket programming
Socket Programming Tutorial
Ad

Similar to Network programming (20)

PPT
Socket Programming in Java.ppt yeh haii
PPTX
PDF
Exploring Async PHP (SF Live Berlin 2019)
PPT
java networking
PDF
2019 11-bgphp
PDF
Introduction to Node.js: What, why and how?
DOCX
Running Head IMPLEMENTING THE LIST AND SEARCH FEATURES IN THE DIS.docx
PPTX
Learn Electron for Web Developers
PDF
KEY
Rails web api 开发
PPT
Socket programming-tutorial-sk
PDF
AnyMQ, Hippie, and the real-time web
PDF
Write php deploy everywhere tek11
PDF
Python, do you even async?
PDF
Asynchronous PHP and Real-time Messaging
PPTX
Service Discovery using etcd, Consul and Kubernetes
PDF
Automating Complex Setups with Puppet
PPTX
How to automate all your SEO projects
PDF
Nodejs Explained with Examples
PDF
Nodejsexplained 101116115055-phpapp02
Socket Programming in Java.ppt yeh haii
Exploring Async PHP (SF Live Berlin 2019)
java networking
2019 11-bgphp
Introduction to Node.js: What, why and how?
Running Head IMPLEMENTING THE LIST AND SEARCH FEATURES IN THE DIS.docx
Learn Electron for Web Developers
Rails web api 开发
Socket programming-tutorial-sk
AnyMQ, Hippie, and the real-time web
Write php deploy everywhere tek11
Python, do you even async?
Asynchronous PHP and Real-time Messaging
Service Discovery using etcd, Consul and Kubernetes
Automating Complex Setups with Puppet
How to automate all your SEO projects
Nodejs Explained with Examples
Nodejsexplained 101116115055-phpapp02

More from Krasimir Berov (Красимир Беров) (15)

Recently uploaded (20)

PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Advanced IT Governance
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
AI And Its Effect On The Evolving IT Sector In Australia - Elevate
PDF
Transforming Manufacturing operations through Intelligent Integrations
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
GamePlan Trading System Review: Professional Trader's Honest Take
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Advanced Soft Computing BINUS July 2025.pdf
PPTX
Telecom Fraud Prevention Guide | Hyperlink InfoSystem
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
madgavkar20181017ppt McKinsey Presentation.pdf
PDF
cuic standard and advanced reporting.pdf
PPTX
Cloud computing and distributed systems.
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Modernizing your data center with Dell and AMD
PDF
Chapter 2 Digital Image Fundamentals.pdf
20250228 LYD VKU AI Blended-Learning.pptx
NewMind AI Weekly Chronicles - August'25 Week I
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Advanced IT Governance
NewMind AI Monthly Chronicles - July 2025
AI And Its Effect On The Evolving IT Sector In Australia - Elevate
Transforming Manufacturing operations through Intelligent Integrations
Reach Out and Touch Someone: Haptics and Empathic Computing
“AI and Expert System Decision Support & Business Intelligence Systems”
GamePlan Trading System Review: Professional Trader's Honest Take
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Advanced Soft Computing BINUS July 2025.pdf
Telecom Fraud Prevention Guide | Hyperlink InfoSystem
Dropbox Q2 2025 Financial Results & Investor Presentation
madgavkar20181017ppt McKinsey Presentation.pdf
cuic standard and advanced reporting.pdf
Cloud computing and distributed systems.
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Modernizing your data center with Dell and AMD
Chapter 2 Digital Image Fundamentals.pdf

Network programming

  • 1. Perl Programming Course Network programming Krassimir Berov I-can.eu
  • 2. 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
  • 3. 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
  • 4. 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
  • 5. 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)
  • 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 = '[email protected]'; my @to = ('[email protected]',); 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/)