Hi folks, I am trying to write an tcp server, which listens on a port and provides telnet clients a "command line". I love readline, but the readline prompt is getting displayed on the server side not to the client, not to mention the inpossibility to edit the line. does any of you have experience with this? I'm attaching the script, it's short and after running it you can telnet to localhost:8081
use IO::Socket; use Term::ReadLine; ##################################### # Server Script: # Copyright 2003 (c) Philip Yuson # this program is distributed according to # the terms of the Perl license # Use at your own risk ##################################### $local = IO::Socket::INET->new( Proto => 'tcp', # protocol LocalAddr => 'localhost:8081', # Host and port to lis +ten to # Change the port if 8 +081 is being used Reuse => 1 ) or die "$!"; ##################################### # Server Script: # Copyright 2003 (c) Philip Yuson # this program is distributed according to # the terms of the Perl license # Use at your own risk ##################################### $local = IO::Socket::INET->new( Proto => 'tcp', # protocol LocalAddr => 'localhost:8081', # Host and port to lis +ten to # Change the port if 8 +081 is being used Reuse => 1 ) or die "$!"; $local->listen(); # listen $local->autoflush(1); # To send response immediately print "At your service. Waiting...\n"; my $addr; # Client handle while ($addr = $local->accept() ) { # receive a request print "Connected from: ", $addr->peerhost(); # Display mess +ages print " Port: ", $addr->peerport(), "\n"; my $result; # variable for Result #while (<$addr>) { # Read all messages from client ## (Assume all valid numbers) #last if m/^end/gi; # if message is 'end' ## then exit loop #print "Received: $_"; # Print received message #print $addr $_; # Send received message back ## to verify #$result += $_; # Add value to result #} $term = new Term::ReadLine('Simple Perl calc'); $prompt = "Enter your arithmetic expression: "; #$OUT = $term->OUT || STDOUT; while ( defined ($_ = $term->readline($prompt)) ) { $res = eval($_), "\n"; warn $@ if $@; print $OUT $res, "\n" unless $@; $term->addhistory($_) if /\S/; } chomp; # Remove the <CR> if (m/^end/gi) { # You need this. Otherwise if # the client terminates abruptly # The server will encounter an # error when it sends the result back # and terminate my $send = "result=$result"; # Format result messag +e print $addr "$send\n"; # send the result mess +age print "Result: $send\n"; # Display sent message } print "Closed connection\n"; # Inform that connection # to client is closed close $addr; # close client print "At your service. Waiting...\n"; # Wait again for next +request }

In reply to IO::Socket and Term::Readline by oernii

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.