commodorejim has asked for the wisdom of the Perl Monks concerning the following question:
Client 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"), 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; }
#!/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;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Client/Server Question
by almut (Canon) on Mar 06, 2010 at 21:22 UTC | |
by commodorejim (Novice) on Mar 06, 2010 at 22:37 UTC | |
by almut (Canon) on Mar 07, 2010 at 00:25 UTC | |
by commodorejim (Novice) on Mar 07, 2010 at 12:06 UTC | |
by jethro (Monsignor) on Mar 07, 2010 at 00:51 UTC |