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 -
#!/usr/bin/perl -w
-
# server2way.pl - a server that reads from
-
# and writes to a client
-
use strict; #declaring all variables
-
use IO::Socket; #use Input/Output socket
-
use Sys::Hostname; #use any hostname that can be found
-
my $sock = new IO::Socket::INET( #assigning sock with object IO socket
-
LocalHost => 'localhost',
-
LocalPort => 7890, #local port number to be used to bind socket
-
Proto => 'tcp', #parameters to pass through
-
Listen => SOMAXCONN, #wait for request
-
Reuse => 1);
-
$sock or die "no socket :$!"; #if no socket is available abort and print following message
-
-
STDOUT->autoflush(1);
-
-
-
my($buf,$secretword, $guess, $name ); #declares variables
-
-
while (my $new_sock = $sock->accept()) { # got a client connection, accept socket
-
-
-
-
while (<$new_sock>) { # respond to client request using
-
-
$buf = $1;
-
-
print $new_sock $buf, next;
-
-
#hash table that contains the names and password
-
my %words = qw(
-
-
fred camel
-
barney llama
-
betty alpaca
-
wilma alpaca
-
-
);
-
-
-
if ($buf eq "randal") #if buf is Randal, print the following
-
-
{
-
print($new_sock "Hello, Randal! How good of you to be here!\n");
-
}
-
-
else
-
-
{
-
$secretword = ""; #declare variable secretword
-
while($secretword eq ""){ #use secretword to enter while loop
-
-
$secretword =$words{$buf};
-
if ($secretword eq "") #if secretword does not match
-
-
{
-
print($new_sock "Incrorrect Name: Please enter Name\n");
-
$buf = <$new_sock>;
-
}
-
else{
-
print($new_sock "correct");
-
}
-
}
-
-
print "What is the secret word? "; #Ask user what is the secret word?
-
$guess = <STDIN>; #Take in what the user writes
-
chomp ($guess); #Cut off the new line character
-
-
while ($guess ne $secretword) #if guess is not equal to the secretword, print the following..
-
-
{
-
print "Wrong, try again. What is the secret word? "; #print message
-
$guess = <STDIN>; #take in text from keyboard
-
chomp ($guess); #cut off new-line characters
-
}
-
}
-
}
-
close $new_sock; #close the socket
-
}
-
================================================== ===========
================================================== =========== -
#!/usr/bin/perl -w
-
# client2way.pl - a client that writes to
-
# and reads from a server
-
-
use strict; #declaring all variables
-
-
use IO::Socket; #use Input/Output socket
-
-
my $host = shift || 'localhost'; #using the loopback address for the host
-
my $port = shift || 7890; # port number to bind the socket to
-
my $sock = new IO::Socket::INET( #assigning sock with oject IO socket
-
PeerAddr => $host, #parameters to pass through
-
PeerPort => $port, #parameters to pass through
-
Proto => 'tcp'); #parameters to pass through
-
-
my ($name);
-
-
$sock or die "no socket :$!"; #if no socket is available abort and print following message
-
do{
-
print "Enter your name please\n"; #print message
-
my $name = <STDIN>; #take in text from keyboard
-
chomp ($name); #cut off new-line characters
-
-
-
print $sock $name; # send message to server
-
# print server response to STDOUT
-
-
-
}while (scalar <$sock> ne "correct" );
-
-
close $sock;
-
0 2618 Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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....
|
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...
|
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...
|
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...
|
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)
|
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...
|
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...
|
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
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
| |