hi,
I was just wondering if anyone could help, i am trying to create a client and server socket connection in perl, i want to ask for name and then do a check on the name and ask for a secret word if it does not match, i have this much done but dont know where to go from here and cant find anything to help on net, if you could help using the code i have done i would be grateful, thanks!
here is the 2 files
=============== =============== =============== =============== =
=============== =============== =============== =============== =
I was just wondering if anyone could help, i am trying to create a client and server socket connection in perl, i want to ask for name and then do a check on the name and ask for a secret word if it does not match, i have this much done but dont know where to go from here and cant find anything to help on net, if you could help using the code i have done i would be grateful, thanks!
here is the 2 files
Code:
#!/usr/bin/perl -w
# server2way.pl - a server that reads from
# and writes to a client
use strict; #declaring all variables
use IO::Socket; #use Input/Output socket
use Sys::Hostname; #use any hostname that can be found
my $sock = new IO::Socket::INET( #assigning sock with object IO socket
LocalHost => 'localhost',
LocalPort => 7890, #local port number to be used to bind socket
Proto => 'tcp', #parameters to pass through
Listen => SOMAXCONN, #wait for request
Reuse => 1);
$sock or die "no socket :$!"; #if no socket is available abort and print following message
STDOUT->autoflush(1);
my($buf,$secretword, $guess, $name ); #declares variables
while (my $new_sock = $sock->accept()) { # got a client connection, accept socket
while (<$new_sock>) { # respond to client request using
$buf = $1;
print $new_sock $buf, next;
#hash table that contains the names and password
my %words = qw(
fred camel
barney llama
betty alpaca
wilma alpaca
);
if ($buf eq "randal") #if buf is Randal, print the following
{
print($new_sock "Hello, Randal! How good of you to be here!\n");
}
else
{
$secretword = ""; #declare variable secretword
while($secretword eq ""){ #use secretword to enter while loop
$secretword =$words{$buf};
if ($secretword eq "") #if secretword does not match
{
print($new_sock "Incrorrect Name: Please enter Name\n");
$buf = <$new_sock>;
}
else{
print($new_sock "correct");
}
}
print "What is the secret word? "; #Ask user what is the secret word?
$guess = <STDIN>; #Take in what the user writes
chomp ($guess); #Cut off the new line character
while ($guess ne $secretword) #if guess is not equal to the secretword, print the following..
{
print "Wrong, try again. What is the secret word? "; #print message
$guess = <STDIN>; #take in text from keyboard
chomp ($guess); #cut off new-line characters
}
}
}
close $new_sock; #close the socket
}
=============== =============== =============== =============== =
=============== =============== =============== =============== =
Code:
#!/usr/bin/perl -w
# client2way.pl - a client that writes to
# and reads from a server
use strict; #declaring all variables
use IO::Socket; #use Input/Output socket
my $host = shift || 'localhost'; #using the loopback address for the host
my $port = shift || 7890; # port number to bind the socket to
my $sock = new IO::Socket::INET( #assigning sock with oject IO socket
PeerAddr => $host, #parameters to pass through
PeerPort => $port, #parameters to pass through
Proto => 'tcp'); #parameters to pass through
my ($name);
$sock or die "no socket :$!"; #if no socket is available abort and print following message
do{
print "Enter your name please\n"; #print message
my $name = <STDIN>; #take in text from keyboard
chomp ($name); #cut off new-line characters
print $sock $name; # send message to server
# print server response to STDOUT
}while (scalar <$sock> ne "correct" );
close $sock;