Wednesday, August 24, 2011

network programming: perl


04_1_echoClient.pl

use strict;
use Socket;
use IO::Handle;
my ($bytes_out,$bytes_in) = (0,0);

my $host = shift || 'localhost';
my $port = shift || getservbyname('echo','tcp');

my $protocol = getprotobyname('tcp');
$host=inet_aton($host);

socket(SOCK,AF_INET,SOCK_STREAM,$protocol);
my $dest_addr=sockaddr_in($port,$host);
connect(SOCK,$dest_addr);

SOCK->autoflush(1);
while (my $msg_out=<>) {
print SOCK $msg_out;
my $msg_in=;
print $msg_in;

$bytes_out += length($msg_out);
$bytes_in += length($msg_in);
}

close SOCK;
print STDERR "$bytes_out, $bytes_in\n";


===========================================

04)3_srvEcho.pl

use strict;
use Socket;
use IO::Handle;
use constant MY_ECHO_PORT => 2007;
my ($bytes_out,$bytes_in) = (0,0);
my $port = shift || MY_ECHO_PORT;
my $protocol = getprotobyname('tcp');

$SIG{'INT'} = sub {
print STDERR "$bytes_out, $bytes_in\n";
exit 0;
};

socket (SOCK, AF_INET, SOCK_STREAM, $protocol);
setsockopt (SOCK,SOL_SOCKET,SO_REUSEADDR,1);
my $my_addr = sockaddr_in($port,INADDR_ANY);
bind(SOCK, $my_addr);
listen(SOCK,SOMAXCONN);
warn "waiting for incoming connectionson port $port....\n";

while (1) {
next unless my $remote_addr=accept(SESSION,SOCK);
my ($port,$hisaddr) = sockaddr_in($remote_addr);
warn "Connection from [", inet_ntoa($hisaddr), " , $port]\n";

SESSION->autoflush(1);
while () {
$bytes_in += length($_);
chomp;

my $msg_out = (scalar reverse $_) . "\n";
print SESSION $msg_out;
$bytes_out += length ($msg_out);
}
warn "connection from finished\n";
close SESSION;
}
close SOCK;


==============================

Friday, August 19, 2011

Step to step config TestCenter 3.70

a, connect to TC: Action->chassis->Port Reserved
b, create the device-host for port1
c, create the device-router for port1
d, create the link for port1's device



e, repeat for the port2.