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.

In reply to communication between server/client by hari9

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.