Client Program.
#!/usr/bin/perl -w
# serIO.pl
# Client Program
#———————
use strict;
use IO::Socket;
use Sys::Hostname;
print “>> Client Program <<\n\n”;
# Create a new socket
my $sock = new IO::Socket::INET(
PeerAddr => ‘localhost’,
PeerPort => 4587,
Proto => ‘tcp’);
#Check for connection
$sock or die “no socket :$!”;
#print username
print “Enter the username : “;
#get the value for username
chop (my $username = <STDIN>);
#send username to the server
print $sock “USERNAME,”,$username,”\n”;
#get the reply from the server
my $usernameFlag = scalar <$sock>;
if ( $usernameFlag == “1″ )
{
print “Enter the password : “;
chop (my $password = <STDIN>);
print $sock “PASSWORD,”,$password,”\n”;
my $passwordFlag = scalar <$sock>;
if ( $passwordFlag == “1″ ) {
print “START POSTING DATA\n”;
my $def_msg = “Enter data to be sent : “;
print $def_msg;
my $data = “\n”;
while($data)
{
chop (my $data = <STDIN>);
if ( ($data eq “exit”) ) {
print $sock “exit”,”\n”;
print scalar <$sock>,”\nYou have exited from sending messages\n”;
exit 1;
}
if ( $data ne “” ) {
print $sock “DATA,”,$data,”\n”;
my $dataFlag = scalar <$sock>;
print $dataFlag;
}
print $def_msg;
}
}
else
{
print “Sorry, wrong password.\n”;
}
}
else
{
print “Sorry, wrong username.\n”;
exit 1;
}
#print $username;
exit;
#!/usr/bin/perl -w
# serIO.pl
# Client Program
#———————
use strict;
use IO::Socket;
use Sys::Hostname;
print “>> Client Program <<\n\n”;
# Create a new socket
my $sock = new IO::Socket::INET(
PeerAddr => ‘localhost’,
PeerPort => 4587,
Proto => ‘tcp’);
#Check for connection
$sock or die “no socket :$!”;
#print username
print “Enter the username : “;
#get the value for username
chop (my $username = <STDIN>);
#send username to the server
print $sock “USERNAME,”,$username,”\n”;
#get the reply from the server
my $usernameFlag = scalar <$sock>;
if ( $usernameFlag == “1″ )
{
print “Enter the password : “;
chop (my $password = <STDIN>);
print $sock “PASSWORD,”,$password,”\n”;
my $passwordFlag = scalar <$sock>;
if ( $passwordFlag == “1″ ) {
print “START POSTING DATA\n”;
my $def_msg = “Enter data to be sent : “;
print $def_msg;
my $data = “\n”;
while($data)
{
chop (my $data = <STDIN>);
if ( ($data eq “exit”) ) {
print $sock “exit”,”\n”;
print scalar <$sock>,”\nYou have exited from sending messages\n”;
exit 1;
}
if ( $data ne “” ) {
print $sock “DATA,”,$data,”\n”;
my $dataFlag = scalar <$sock>;
print $dataFlag;
}
print $def_msg;
}
}
else
{
print “Sorry, wrong password.\n”;
}
}
else
{
print “Sorry, wrong username.\n”;
exit 1;
}
#print $username;
exit;
Server Program
#!/usr/bin/perl -w
# server2way.pl – a server that reads from
# and writes to a client
use strict;
use IO::Socket;
use Sys::Hostname;
use LWP::UserAgent;
my $sock = new IO::Socket::INET(
LocalHost => ‘localhost’,
LocalPort => 4587,
Proto => ‘tcp’,
Listen => SOMAXCONN,
Reuse => 1);
$sock or die “no socket :$!”;
STDOUT->autoflush(1);
my($new_sock, $buf);
while (($new_sock, $c_addr) = $sock->accept()) {
my $uflag = “0″;
my $pflag = “0″;
my $counter = “0″;
# got a client connection, so read
# line by line until end-of-file
while (defined($buf = <$new_sock>)) {
# respond to client request using
# a cleverly disguised switch
# statement
foreach ($buf)
{
#print $uflag;
#print $pflag;
use Switch;
switch ($buf)
{
case “USERNAME,socket\n”
{
$uflag = “1″;
print ($new_sock “1\n”);
last;
}
case “PASSWORD,win\n”
{
$pflag = “1″;
print ($new_sock “1\n”);
last;
}
case “exit\n”
{
print ($new_sock “Total records recieved “,$counter,”\n”);
}
else
{
if ( ((substr $buf , 0, 4) eq “DATA”) && ( $uflag ne “1″ && $pflag ne “1″ ) ) {
print ($new_sock “0\n”);
}
#my $fragment = substr $buf , 0, 4 ;
if ( ((substr $buf , 0, 4) eq “DATA”) && ( $uflag eq “1″ && $pflag eq “1″ ) ) {
my ($client_port, $c_ip) = sockaddr_in($c_addr);
my $client_ipnum = inet_ntoa($c_ip);
my $client_host = gethostbyaddr($c_ip, AF_INET);
$url = “http://testsite.com/SocApp/update_data_from_socket.php”;
$queryString = “data=$buf&client_ip=$client_ipnum”;
my $curl = LWP::UserAgent->new( timeout => 120 );
my $response = $curl->post($url, Content => $queryString);
#print $buf;
if ($response->is_success) {
$counter = $counter + 1;
print($new_sock $response->content, “\n”);
}
else {
print($new_sock $response->status_line, “\n”);
}
#print($new_sock $client_ipnum,$client_host, “\n”);
}
else
{
print ($new_sock “0\n”);
last;
}
}
}
}
}
close $new_sock;
}
Program
This is a client – server apps with interaction.
First the sever will request for username ( socket ).
Second the server will request for password ( win )
Then the server is set to accept the data from the client machine.
Post the data.
In this the data will be posted by a CURL module to a remote php file where we can store the data in database.
For any clarification please feel free to ask me in this program.
(albert.thinc@gmail.com)
Alexandr Ciornii said,
July 8, 2009 at 8:35 pm
to use “switch”/”case” you need to use Switch module, or just use perl 5.10 ang “given”/”when”.
albertphp said,
July 9, 2009 at 6:02 am
Yes Switch module is required.