Hi everyone,
I've recently started experimenting with PERL in the area of sockets and networks.
I've gotten the two programs listed below working on my own machine (through localhost).
However, I want to prompt the server to ask the client for a password, printing a message if the correct password is entered or not.
The problem I'm having is how do I send the values I read in in the client program to the server program?
I know how to check for these values but I'm stumped as to how to send the string that's entered client-side to the server-side. Or am I completely on the wrong track?
Server Program
Client Program
I've recently started experimenting with PERL in the area of sockets and networks.
I've gotten the two programs listed below working on my own machine (through localhost).
However, I want to prompt the server to ask the client for a password, printing a message if the correct password is entered or not.
The problem I'm having is how do I send the values I read in in the client program to the server program?
I know how to check for these values but I'm stumped as to how to send the string that's entered client-side to the server-side. Or am I completely on the wrong track?
Server Program
Code:
#!/usr/bin/perl -w
# server2way.pl - a server that reads from
# and writes to a client
use strict;
use IO::Socket;
use Sys::Hostname;
my $sock = new IO::Socket::INET(
LocalHost => 'localhost',
LocalPort => 7890,
Proto => 'tcp',
Listen => SOMAXCONN,
Reuse => 1);
$sock or die "no socket :$!";
STDOUT->autoflush(1);
my($new_sock, $buf);
while ($new_sock = $sock->accept())
{
while (defined($buf = <$new_sock>))
{
foreach ($buf)
{
/^HELLO$/ and print($new_sock "Enter Name: \n"), last;
/^NAME$/ and print($new_sock "Enter Password: \n"), last;
/^DATE$/ and print($new_sock scalar(localtime), "\n"),last;
print $new_sock "DEFAULT\n";
}
}
close $new_sock;
}
Client Program
Code:
#!/usr/bin/perl -w
# client2way.pl - a client that writes to
# and reads from a server
use strict;
use IO::Socket;
my $host = shift || 'localhost';
my $port = shift || 7890;
my $sock = new IO::Socket::INET(
PeerAddr => $host,
PeerPort => $port,
Proto => 'tcp');
$sock or die "no socket :$!";
# send message to server
print $sock "HELLO\n";
print scalar <$sock>;
my $name =<STDIN>;
print $sock "NAME\n";
print scalar <$sock>;
my $guess=<STDIN>;
print $sock "DATE\n";
print scalar <$sock>;
print $sock "NONE\n";
print scalar <$sock>;
close $sock;