perl sample
chap 13. Internet socket
a, client_w.pl
#!/usr/bin/perl -w
use IO::Socket;
$socket = IO::Socket::INET->new
(
PeerAddr => '127.0.0.1',
PeerPort => 27777,
Proto => "tcp",
Type => SOCK_STREAM,
) or die "could not open port.\n";
print $socket "hello from the client\n";
close ($socket);
b, server_r.pl
#!/usr/bin/perl -w
use IO::Socket;
$server = IO::Socket::INET->new
(
LocalPort => 27777,
Type => SOCK_STREAM,
Reuse => 1,
Listen => 5
) or die "could not open port.\n";
while ($client = $server->accept()) {
$line = <$client>;
print $line;
}
close ($server);
=================================
Notes: PerlInNut/13.2
a, The parameters for the new socket object determine whether it is a server or a client socket. Because we're creating a server socket, LocalAddr and LocalPort provide the address and port to bind to the socket. The Listen parameter gives the queue size for the number of client requests that can wait for an accept at any one time.
When the server receives a client request, it calls the accept method on the socket object. This creates a new socket object on which the rest of the communication can take place:
$new_sock = $sock->accept();
b, Type => SOCK_STREAM | SOCK_DGRAM
Specifies the type of socket. SOCK_STREAM indicates a stream-based socket connection, and SOCK_DGRAM indicates a message-based (datagram) connection.
Q:datagram = udp?
A:With sockets, you can do both virtual circuits (as TCP streams) and datagrams (as UDP packets)
Tuesday, February 9, 2010
Labels:
Perl
Wednesday, February 3, 2010
Perl Sample for Ticket
1, T1180
#!/usr/bin/perl -w
print "$#ARGV\n";
if ($#ARGV != 2) {
print "Usage: gen50phase.pl phase1 src dst\n";
exit;
}
print "$ARGV[0]\n";
print "$ARGV[1]\n";
print "$ARGV[2]\n";
open (PHASE2, ">50phase2.txt");
print PHASE2 "config vpn ipse phase2-interface\n";
for ($i=1; $i<51;$i++) {
print PHASE2 "edit a10.1.1.$i\n";
print PHASE2 "set keepalive enable\n";
print PHASE2 "set phase1name $ARGV[0]\n";
print PHASE2 "proposal aes128-sha1\n";
print PHASE2 "set replay disable\n";
print PHASE2 "set dst-subnet $ARGV[2].$i 255.255.255.255\n";
print PHASE2 "set src-subnet $ARGV[1].$i 255.255.255.255\n";
print PHASE2 "next\n";
}
print PHASE2 "end\n";
close (PHASE2);
Labels:
Perl
Subscribe to:
Posts (Atom)