#!/usr/bin/perl -w # server1.pl - a simple server use strict; use Socket; use IO::Socket; # use port 7890 as default use constant TEST_TCP_PORT => 7890; my $port = TEST_TCP_PORT; my $proto = getprotobyname('tcp'); sub logmsg { print "$0 $$: @_ at ", scalar localtime, "\n" } # create a socket, make it reusable socket(SERVER, PF_INET, SOCK_STREAM, $proto) or die "Test socket failed : $!\n"; setsockopt(SERVER, SOL_SOCKET, SO_REUSEADDR, 1) or die "Test setsock failed : $! \n"; # grab a port on this machine my $paddr = sockaddr_in($port, INADDR_ANY); # bind to a port, then listen bind(SERVER, $paddr) or die "Test bind failed : $!"; listen(SERVER, SOMAXCONN) or die "Test listen failed : $!"; print "Server started on port $port \n"; # for each connection... my $client_addr; while ($client_addr = accept(CLIENT, SERVER)) { # find out who connected my ($client_port, $client_ip) = sockaddr_in($client_addr); my $client_ipnum = inet_ntoa($client_ip); my $client_host = gethostbyaddr($client_ip, AF_INET) ; # tell who connected #print "Got a connection from: $client_host"," [$client_ipnum]\n"; logmsg "Got a connection from: $client_host"," [$client_ipnum]\n"; #------------------------------------------------------------------------------ # foreach (1 .. 10){ # print CLIENT "nO \n"; # CLIENT->flush(); # # } print CLIENT "Hello from the server \n\n "; CLIENT->flush(); my ($response,$temp); while ( defined ($response = )){ #print length($response); print "Response from client :$response" ; $temp = $response; print "Temporary : $temp " ; print "Declined \n\n"; last ; } print "Temporary : $temp \n", length($temp) ; my @output = split (' ',$temp); print $output[0]; my $temp1 = $output[0]; print "Temporary : $temp1 " ; #print STDOUT "Sajan : ",length($response); print STDOUT "Sajan : ", length($temp1); if ( $temp1 eq "sajan" ){ print "yes"; print STDOUT "Authenticated : "; CLIENT->flush(); } #------------------------------------------------------------------------------ close CLIENT; } #### #! /usr/bin/perl -w # client1.pl - a simple client use strict; use Socket; use IO::Socket; use constant TEST_SERVER => "temp.random.com"; use constant TEST_TCP_PORT => 7890; # initialize host and port my $host = TEST_SERVER ; my $port = TEST_TCP_PORT ; my $proto = getprotobyname('tcp'); # get the port address my $iaddr = inet_aton($host); my $paddr = sockaddr_in($port, $iaddr); # create the socket, connect to the port socket(SOCKET, PF_INET, SOCK_STREAM, $proto) or die "socket: $!"; connect(SOCKET, $paddr) or die "connect: $!"; my $other_end = getpeername(SOCKET) or die "Couldn't identify other end: $!\n"; ($port, $iaddr) = unpack_sockaddr_in($other_end); my $actual_ip = inet_ntoa($iaddr); my $claimed_hostname = gethostbyaddr($iaddr, AF_INET); print "Connected to server : $actual_ip =>> $claimed_hostname \n"; #-------------------------------------------------------- my $line; print SOCKET "sajan "; SOCKET->autoflush(); while ($line = ) { print SOCKET "Client says hellooooo toooo !!!"; SOCKET->autoflush(); sleep(11); print STDOUT "Sajan inside "; print $line; open my $tfh, " >> /tmp/sajan.tmp"; my $date = `date`; print $tfh "$date :: $line "; close $tfh }