Network programming
Upcoming SlideShare
Loading in...5
×
 

Network programming

on

  • 1,006 views

This is the thirteenth set of slides from a Perl programming course that I held some years ago. ...

This is the thirteenth set of slides from a Perl programming course that I held some years ago.
I want to share it with everyone looking for intransitive Perl-knowledge.
A table of content for all presentations can be found at i-can.eu.
The source code for the examples and the presentations in ODP format are on https://github.com/kberov/PerlProgrammingCourse

Statistics

Views

Total Views
1,006
Views on SlideShare
994
Embed Views
12

Actions

Likes
0
Downloads
7
Comments
0

1 Embed 12

http://www.linkedin.com 12

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

CC Attribution-NonCommercial-ShareAlike LicenseCC Attribution-NonCommercial-ShareAlike LicenseCC Attribution-NonCommercial-ShareAlike License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Network programming Network programming Presentation Transcript

  • Perl Programming Course Network programmingKrassimir BerovI-can.eu
  • Contents1. Sockets, servers and clients2. TCP/IP, UDP sockets3. Unix Domain Socket (UDS) sockets4. IO::Socket5. HTTP and SMTP6. 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: Cant 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::UserAgents 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::UserAgents 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 programmingQuestions?