Folks I have a server and client perl programs written using IO::Socket. The server looks just fine (I think). But the client opens a connection to the server. It sends the message for the first time and receives it successfully. The second time, the client shows an empty message and the third time it fails with "cannot determine peer address". If I create a new socket everytime, that is, if I do my $remote = IO::Socket::INET->new() inside the loop in the client code, the code works just fine but I dont want to open a new connection everytime. Can you please tell me what should I do to open a connection to the server once and use it for n times. Thanks for your help CLIENT CODE:
use strict; use IO::Socket; my $remote = IO::Socket::INET->new( Proto => 'tcp', # protocol PeerAddr=> '127.0.0.1', # Address of server PeerPort=> "1426", # port of server Timeout => 90 ) or die "$!"; $remote->autoflush(1); for (my $i = 0; $i < 100 ; $i++) { $remote->send("Test \n"); # Send to Server my $line ; $remote->recv($line,256); # Receive echo from server print $line. "\n"; } close $remote; # Close socket
SERVER CODE:
use IO::Socket qw(:DEFAULT :crlf); use IO::File; use constant PIDFILE => "/tmp/prefork.pid"; use constant PREFORK_CHILDREN => 3; my $port = shift || 1426; my $socket = IO::Socket::INET->new( Proto => 'tcp', LocalPort => $port, Listen => 5, Reuse => SO_REUSEADDR, Type => SOCK_STREAM ) or die "Can't create listen socket: $!"; # create PID file, initialize logging, and go into background #init_server(PIDFILE); make_new_child() for (1..PREFORK_CHILDREN); exit 0; sub make_new_child { #my $child = launch_child(); my $child = fork; return if $child; do_child($socket); # child handles incoming connections exit 0; } sub do_child { my $socket = shift; while (1) { local $/ = "\n"; next unless my $c = $socket->accept; handle_connection($c); close $c; } close $socket; } sub handle_connection { my $x = shift; my $sz = <$x>; print $x "Testing\n"; }

In reply to Cannot determine peer address by Anonymous Monk

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.