#!/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; }