- Properties of a socket The socket model. - The Socket module provides these constants and also many functions for packing and unpacking these data structures. - The Socket module exports constants for these. - These are represented as numbers and the Socket. - The socket model. - The Server. - The socket model (cont.). - Either accept returns or select will report the socket as readable.. - Server and Client close the socket to break the connection.. - To create a socket you need to know all three properties about the socket.. - import required constants from the Socket module. - Create the socket. - die "socket: $!";. - Binding the socket. - bind takes two arguments, the first is the socket and the second is a packed address.. - The Socket module provides functions for packing and unpacking addresses.. - Binding the socket (cont.). - $port = getservbyname(’ftp’,’tcp’);. - getservbyname(’ftp’, ’tcp’);. - If you do not care which port the socket is bound to, you. - If you do not want to bind the socket to a particular interface the you can use INADDR_ANY.. - If you want to bind the socket to a particular interface then you must pass a packed IP address.. - The Socket module provides inet_aton and inet_ntoa to pack and unpack IP addresses.. - If the socket is of type AF_UNIX the the socket. - socket(SOCK, AF_INET, SOCK_STREAM, $proto) or die "socket: $!";. - bind(SOCK, $sin) or die "bind: $!";. - Connecting to the server. - connect takes two arguments, the socket and a. - connect(SOCK, $paddr) or die "connect: $!";. - Connecting to the server (cont.). - or die "connect: $!";. - close the socket. - die "close: $!";. - bind(SOCK, $paddr) or die "bind: $!";. - socket(SOCK, AF_INET, SOCK_DGRAM, $proto) or die "socket: $!";. - send(SOCK paddr) or die "send: $!";. - recv(SOCK, $date, 1024, 0) or die "recv: $!\n";. - IO::Socket. - Create a socket with IO::Socket. - IO::Socket->new only knows about one, which tells it the domain of the socket. - $sock1 = IO::Socket->new(. - $sock2 = IO::Socket::INET->new(@args);. - IO::Socket::INET. - PeerPort - The port number at PeerAddr to connect LocalAddr - Bind the socket to the this address. - LocalPort - Bind the socket to this port Proto - The protocol to use. - IO::Socket::INET (cont.). - use IO::Socket;. - $s = IO::Socket::INET->new(’localhost:80. - die "IO::Socket: $@";. - $s = IO::Socket::INET->new(. - Example of tcp daytime client using IO::Socket use IO::Socket;. - my $sock = IO::Socket::INET->new("localhost:daytime") or die "IO::Socket: $@";. - IO::Socket TCP daytime client. - getsockname will return a packed socket address for the socket.. - getpeername will return a packed socket address for the socket at the other end of the connection.. - If you do not know what address the socket is using, how do you know which functions to call. - The first element in the socket address structure is the address family. - die "accept: $!";. - I print to the socket handle, but the server never sees my data.. - $client = $sock->accept or die "accept: $!";. - die "fork: $!". - This can be avoided by telling the system that you want to allow the socket to be reused.. - \$host, ’u:s’ =>. - \$user, ’p:s’ =>. - \$passwd, ’o:s’ =>. - open(OUT, ">>$out") or die "open: $!";. - \$passwd, ’d:s’ =>. - \$dir, ’f:s’ =>. - \$file, ’r’ =>. - $ftp = Net::FTP->new($host) or die . - $ftp->login($user, $passwd) or die $ftp->message;. - $ftp->cwd($dir) or die $ftp->message;. - ’src:s’ =>. - \$src, ’dest:s’ =>. - \$d_user, ’dp:s’ =>. - \$d_passwd, ’su:s’ =>. - \$s_user, ’sp:s’ =>. - $s_ftp->login($s_user, $s_passwd) or die $s_ftp->message;. - $d_ftp->login($d_user, $d_passwd) or die $d_ftp->message;. - $s_ftp->cwd($s_dir) if length $s_dir;. - $d_ftp->cwd($d_dir) if length $d_dir;. - $s_ftp->ascii or die $s_ftp->message;. - $d_ftp->ascii or die $s_ftp->message;. - $s_ftp->pasv_xfer($s_file, $d_ftp, $d_file). - $s_ftp->quit;. - $d_ftp->quit;. - ’tchrist’ =>. - ’gbarr’ =>. - \$host, ’g:s’ =>. - \$groups, ’p:s’ =>. - \$pattern, ’o:s’ =>. - \$outfile, ’s:s’ =>. - $nntp = Net::NNTP->new($host) or die . - open(OUT,">>$outfile") or die "open: $!";. - \$host, ’f:s’ =>. - \$from, ’s:s’ =>. - $smtp = Net::SMTP->new($host) or die . - $smtp->mail($from) or die $smtp->message;. - $smtp->recipient(@ARGV) or die $smtp->message;. - $smtp->data($header, <STDIN>) or die $smtp->message;
Xem thử không khả dụng, vui lòng xem tại trang nguồn hoặc xem
Tóm tắt