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

how to create a client/server socket connection (ask for user name and secretword)

P: 3
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
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;                                     #declaring all variables
  5. use IO::Socket;                                 #use Input/Output socket
  6. use Sys::Hostname;                              #use any hostname that can be found
  7. my $sock = new IO::Socket::INET(                #assigning sock with object IO socket 
  8.                    LocalHost => 'localhost',    
  9.                    LocalPort => 7890,           #local port number to be used to bind socket
  10.                    Proto     => 'tcp',          #parameters to pass through
  11.                    Listen    => SOMAXCONN,      #wait for request
  12.                    Reuse     => 1);
  13. $sock or die "no socket :$!";                   #if no socket is available abort and print following message
  14.  
  15. STDOUT->autoflush(1);                        
  16.  
  17.  
  18. my($buf,$secretword, $guess, $name ); #declares variables
  19.  
  20. while (my $new_sock = $sock->accept()) {    # got a client connection, accept socket
  21.  
  22.  
  23.  
  24.   while (<$new_sock>) {     # respond to client request using
  25.  
  26.   $buf = $1;
  27.  
  28.   print $new_sock $buf, next;
  29.  
  30.   #hash table that contains the names and password
  31.   my %words = qw(
  32.  
  33.   fred            camel       
  34.   barney    llama
  35.   betty            alpaca
  36.   wilma            alpaca
  37.  
  38. );
  39.  
  40.  
  41.   if ($buf eq "randal") #if buf is Randal, print the following
  42.  
  43.   {
  44.       print($new_sock "Hello, Randal! How good of you to be here!\n");
  45.   }
  46.  
  47.   else 
  48.  
  49.   {
  50.       $secretword = "";                #declare variable secretword
  51.       while($secretword eq ""){        #use secretword to enter while loop
  52.  
  53.         $secretword =$words{$buf}; 
  54.         if ($secretword eq "") #if secretword does not match
  55.  
  56.         {
  57.             print($new_sock "Incrorrect Name: Please enter Name\n");
  58.             $buf = <$new_sock>;
  59.         }
  60.         else{
  61.             print($new_sock "correct");
  62.         }
  63.     }
  64.  
  65.         print "What is the secret word? "; #Ask user what is the secret word?
  66.         $guess = <STDIN>;                  #Take in what the user writes
  67.         chomp ($guess);                    #Cut off the new line character
  68.  
  69.         while ($guess ne $secretword)      #if guess is not equal to the secretword, print the following..
  70.  
  71.         {
  72.             print "Wrong, try again. What is the secret word? "; #print message
  73.             $guess = <STDIN>;                                    #take in text from keyboard
  74.             chomp ($guess);                                      #cut off new-line characters
  75.         }
  76.  }
  77.     }
  78.     close $new_sock;                                                         #close the socket
  79. }
  80.  

================================================== ===========
================================================== ===========
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2. # client2way.pl - a client that writes to
  3. # and reads from a server
  4.  
  5. use strict; #declaring all variables
  6.  
  7. use IO::Socket;  #use Input/Output socket
  8.  
  9. my $host = shift || 'localhost';          #using the loopback address for the host
  10. my $port = shift || 7890;                 # port number to bind the socket to
  11. my $sock = new IO::Socket::INET(          #assigning sock with oject IO socket 
  12.                   PeerAddr => $host,      #parameters to pass through
  13.                   PeerPort => $port,      #parameters to pass through
  14.                   Proto    => 'tcp');     #parameters to pass through
  15.  
  16. my ($name);
  17.  
  18. $sock or die "no socket :$!";             #if no socket is available abort and print following message
  19. do{
  20.     print "Enter your name please\n"; #print message
  21.     my $name = <STDIN>;               #take in text from keyboard
  22.     chomp ($name);                    #cut off new-line characters
  23.  
  24.  
  25.     print $sock $name;     # send message to server
  26.                            # print server response to STDOUT
  27.  
  28.  
  29. }while (scalar <$sock> ne "correct" );
  30.  
  31. close $sock;
  32.  
Mar 11 '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.