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
#!/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"), l
+ast;
/^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 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;
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.