Hi All,

I'm a relative newbie, but looking several samples I have a basic setup which is working, but now I'd like for the server to send a return code (ok/fail) to the client. However I can't get it working, the client get's stuck in the recv.

I believe I don't need forking, my use case is synchronous, I just want to send commands to a test machine and get a response, then send more commands, etc... (part of some testing automation framework)

I have the following code:

################## SERVER
$OUTPUT_AUTOFLUSH=1; my $socket = new IO::Socket::INET (LocalHost => '0.0.0.0', LocalPort => $port, Proto => 'tcp', Listen => 1, Reuse => 1); die "Cannot create socket $!\n" unless $socket; # waiting for a new client connection $client_socket = $socket->accept(); $stop_server = 0; while(!$stop_server) { $client_data = ""; $client_socket->recv($client_data, 1024); # dispatch command my $ret = DispatchCommand($client_data); # reply to client ### ADD REPLY $client_socket->send($ret); ### ADD REPLY } $client_socket->close();
################## CLIENT
$OUTPUT_AUTOFLUSH=1; my $socket = new IO::Socket::INET (PeerHost => 'my.domain.net', PeerPort => '8888', Proto => 'tcp', Reuse => 1); die "cannot connect to the server $!\n" unless $socket; while (1) { print "Command??? "; chomp ($cmd = <>); # data to send to a server $socket->send($cmd); # wait for server acknowledgement ### GET REPLY $socket->recv($ack, 1024); ### GET REPLY print "response from server was $ack\n"; ### GET REPLY } $socket->close();

Without the ADD REPLY / GET REPLY, it works in the sense I can send my commands, but how do I get back to the client to tell it the return code?

Also, is there an easy way to get client's recv timeout after some set seconds? I've seen suggested I should use select, how would I use it this example?

Many thanks in advance.

In reply to Interactive simple client server by rogermante

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.