473,405 Members | 2,349 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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

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
0 2618

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

Similar topics

2
by: lewi | last post by:
I started an unmanaged VC++ dlg app that I want to play arround with socket for Peer to Peer app on my local LAN and I got it connected up the the app is the same on both computer and creates a...
2
by: Martin | last post by:
Hi, I currently have an application that connects to an MS ACCESS database. This application uses an OLEDB connection string for MS ACCESS. Now, I'd like to upsize the application so I converted...
1
by: bobano | last post by:
Hi everyone, I am writing a POP3 Client program in Perl. You connect to a POP3 Server and have a running conversation with the mail server using commands from the RFC 1939 Post Office Protocol....
3
by: Erakis | last post by:
Hi, I have to make an ActiveX (Running on Internet Explorer) that play/record sound from soundcard. Also, I have to create a Socket to send/receive sound data to my server. I use this...
14
by: ahlongxp | last post by:
Hi, everyone, I'm implementing a simple client/server protocol. Now I've got a situation: client will send server command,header paires and optionally body. server checks headers and decides...
4
by: Vlad | last post by:
I am having problems using the file.create method within a function that is called when looping through an array of filepaths. If I call my function with a hardcoded file path --C:\Temp.txt the...
6
by: 7stud | last post by:
My question pertains to this example: #!/usr/bin/env python import socket, sys, time host = sys.argv textport = sys.argv s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
4
by: gcharbon | last post by:
Hi community, I have a problem with a Soap client written in php. I have a local server (coded in c and a client in c too, it works fine), but i want to test client in php, and i have an error...
10
by: Elaine121 | last post by:
Hi i've been batteling for hours and can't seem to find the problem. When my server runs and I press the connect button the gui freezes until the client gui is terminated.. only then the gui becomes...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.