By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,834 Members | 2,234 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,834 IT Pros & Developers. It's quick & easy.

Client/Server Password question

P: 1
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

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2. # server2way.pl - a server that reads from
  3. # and writes to a client
  4. use strict;
  5. use IO::Socket;
  6. use Sys::Hostname;
  7. my $sock = new IO::Socket::INET(
  8.                    LocalHost => 'localhost',
  9.                    LocalPort => 7890,
  10.                    Proto     => 'tcp',
  11.                    Listen    => SOMAXCONN,
  12.                    Reuse     => 1);
  13. $sock or die "no socket :$!";
  14. STDOUT->autoflush(1);
  15. my($new_sock, $buf);
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22. while ($new_sock = $sock->accept()) 
  23.  
  24. {
  25.  
  26.  
  27.  
  28.         while (defined($buf = <$new_sock>)) 
  29.  
  30.         {
  31.  
  32.            foreach ($buf) 
  33.  
  34.            {
  35.                     /^HELLO$/ and print($new_sock "Enter Name: \n"), last;
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.                      /^NAME$/  and print($new_sock "Enter Password: \n"), last;
  44.  
  45.  
  46.                    /^DATE$/  and print($new_sock scalar(localtime), "\n"),last;
  47.  
  48.                    print $new_sock "DEFAULT\n";
  49.            }
  50.  
  51.         }
  52.  
  53.  
  54.        close $new_sock;
  55. }
  56.  
  57.  
  58.  


Client Program

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. #!/usr/bin/perl -w
  4. # client2way.pl - a client that writes to
  5. # and reads from a server
  6. use strict;
  7. use IO::Socket;
  8. my $host = shift || 'localhost';
  9. my $port = shift || 7890;
  10. my $sock = new IO::Socket::INET(
  11.                   PeerAddr => $host,
  12.                   PeerPort => $port,
  13.                   Proto    => 'tcp');
  14. $sock or die "no socket :$!";
  15. # send message to server
  16.  
  17.  
  18. print $sock "HELLO\n";
  19. print scalar <$sock>;
  20. my $name =<STDIN>;
  21.  
  22.  
  23.  
  24. print $sock "NAME\n";
  25. print scalar <$sock>;
  26. my $guess=<STDIN>;
  27.  
  28.  
  29. print $sock "DATE\n";
  30. print scalar <$sock>;
  31.  
  32.  
  33. print $sock "NONE\n";
  34. print scalar <$sock>;
  35.  
  36.  
  37.  
  38.  
  39. close $sock;
  40.  
  41.  
  42.  
Mar 6 '10 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.