update: Oops, I wasn't thinking either. My eval/alarm solution will not work because the connection is successfull and therefore it contains the exact same issue as the OP. Sorry. *sigh*

In lieu of the completely refactored parent node, here is the refactored answer :)

When you set a socket to listen() via IO::Socket::INET, the port will come up as in-use and will queue up successive connection requests. Since your first client that connects is in an infinite loop, the second client to connect is simply waiting for the first client to close the connection to the server.

The easiest way to fix such a problem is to set a timeout via a signal handler. Such an example is provided here (see the last paragraph of screen 7 of `perldoc perlipc` on a 24-row terminal for an explanation of the $SIG{ALRM} and alarm()):

#!/usr/bin/perl # c.pl use strict; use warnings; use IO::Socket; use less 'java'; #;-) my $b_address = '127.0.0.1'; #IP of box A my $b_port = '8888'; #Port number guipartner set to on A my $eol = "\015\012"; #sets end of line chracters for socket transmi +ssions my $skipC = 0; my $socketC; eval { # set the signal handler. we die when the signal hits us local $SIG{ALRM} = sub { die("timeout\n") }; alarm(5); # number of seconds for timeout $socketC = new IO::Socket::INET ( PeerAddr => $b_address, PeerPort => $b_port, Proto => 'tcp', ); alarm(0); # cancel the alarm signal }; # we got the alarm signal, so we timed out if ($@ eq "timeout\n") { print "socket not available\n"; exit; } # some other unexpected error elsif ($@) { die($@); } local $/=$eol; #set default EOL to $eol so chomp works while (!$skipC) { print $socketC "c.pl sends \$skipC=$skipC", $eol; print "message sent\n"; sleep 2; } print "socket not available\n"; close $socketC;

Also, you can modify your server code (b.pl) so that you're not recreating the socket for each connection. You can instead move the socket creation outside of the infinite loop and loop on accept():

#!/usr/bin/perl # b.pl use strict; use warnings; use IO::Socket; use less 'java'; #;-) my $eol = "\015\012"; #sets end of line chracters for socket transm +issions my $sock = new IO::Socket::INET ( LocalPort => '8888', Proto => 'tcp', Listen => 1, ) or die "Could not create socket: $!\n"; while ( my $newsock = $sock->accept() ){ #continuous loop: wait + for socket connections until end of time while (my $incoming = <$newsock>){ local $/=$eol; #set default EOL to $eol so chomp wo +rks chomp($incoming); print "received: $incoming\n"; } close $newsock; close $sock; }

In reply to Re: IO::Socket *always* making connection? by saskaqueer
in thread IO::Socket *always* making connection? by wolfger

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.