Hi All.
In the program below, I want client to send server a message. This message is interpreted by the server and a corresponding reply is given to client.
I'm not sure why client is not getting the reply from the server.
server code
#!/usr/bin/perl
#tcpserver.pl
use Switch;
use IO::Socket::INET;
$o=0;
$| = 1;
$i=0;
my ($socket,$client_socket);
my ($peer_address,$peer_port);
$socket = new IO::Socket::INET ( #LocalHost => '127.0.0.1',
LocalPort => '5000',
Proto => 'tcp',
Listen => 5,
Reuse => 1) or die "ERROR in Socket
+Creation : $!\n";
$timeData = scalar(localtime);
print $timeData."\n";
print "SERVER Waiting for client connection on port 5000";
$n=0;
$times_connected=0; # number of client connection
$t=0;
@connects=("");
while(1)
{
# waiting for new client connection.
$client_socket = $socket->accept();
my $gotdata="";
# get the host and port number of newly connected clients.
$peer_address = $client_socket->peerhost();
$peer_port = $client_socket->peerport();
$_=received($peer_address,$peer_port);
if($_)
{
print"\n\nCLIENT ADDRESS\t CLIENT PORT\n";
foreach $_(0..$#client)
{
print $client[$_]{addr}."\t".$client[$_]{port}."\n
+";
#Prints the list of connected clients (dumped in t
+he array so far).
}
}
else
{
sleep(20);
}
$times_connected++;
print "\nAccepted New Client Connection[$times_connected]
+From : $peer_address, $peer_port\n at".scalar(localtime)."\n";
while(defined(my $data=<$client_socket>))
{
chomp($data);
$gotdata.=$data;
}
process($times_connected,$gotdata,$peerport); # processes
+each client details and suitably directs them.
print "\n Data Received from Client : $gotdata\n";
###print $client_socket $status;
}
$socket->close();
# Process : Interprets the packet from client and instructs it do
+the next step
sub process
{
my $client_no=shift;
my $data=shift;
my $port=shift;
my @client_details=(id=>$client_no,info=>$data,port=>$port);
my @dataElements = split(//, $data);
if($dataElements[0] eq 0)
{
if($client_no eq 1) #if Its the
+ very first client pinging
{
$status="write and get your own Md5";
print "Get your own MD5";
}
else
{ $status="take this Md5 and verify with others";
print "verify this MD5 with already existing other
+s";
}
}
if($dataElements[0] eq 1)
{ $status="write and Get your own MD5 ";
print "write and Get your own MD5 ";
#verify(@verified);
}
if($dataElements[0] eq 2)
{ $status="Thanks for verifying";
print "Thanks for verifying";
}
}
# Checks for redundant client connections.
sub received
{
$addr=shift;
$port=shift;
if($o eq 0)
{
push @client,{addr=>$addr,port=>$port};
}
else
{
if(grep $client[$_]{port} eq $port,@client)
{
print "Value already exists";
$ret=0;
}
else
{
$ret=1;
}
$o++;
return $ret;
}
}
Client
#!/usr/bin/perl
#tcpclient.pl
use IO::Socket::INET;
my($status,$md_self,@md_others);
$status=0;
$md=0;
@md_others=("0");
$md_others=join(' ',@md_others);
$| = 1;
my ($socket,$client_socket);
print "TCP Connection Success.\n";
# creating object interface of IO::Socket::INET modules which inte
+rnally creates
# socket, binds and connects to the TCP server running on the spec
+ific port.
$socket = new IO::Socket::INET (PeerHost => '127.0.0.1',
PeerPort => '5000',
Proto => 'tcp',
) or die "ERROR in Socket Creation
+ : $!\n";
# write on the socket to server.
$_=join(' ',$status,$md,$md_others);
@data=split(" ",$_);
foreach $_(0..$#data)
{
print $socket $data[$_];
}
###$data=<$socket>;
###print $data;
$socket->close();
Programs works fine (client writes on server)if you remove "###" lines from both the codes.
I tried using "print scalar <$socket>" at clients side to display server's reply, not working.
How would I do this otherwise.
Thanks in advance.