Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: IO::Socket *always* making connection?

by saskaqueer (Friar)
on Feb 08, 2005 at 21:55 UTC ( [id://429239]=note: print w/replies, xml ) Need Help??


in reply to IO::Socket *always* making connection?

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; }

Replies are listed 'Best First'.
Re^2: IO::Socket *always* making connection?
by wolfger (Deacon) on Feb 10, 2005 at 15:23 UTC

    Did you test this code? Because it is not working on my system. In fact, it's a step backwards... The original code for A and C would die gracefully ("couldn't send message to connected socket") if I killed B, and with your revised code for A/C, they both keep merrily sending messages. I also tried your fix for B, but it behaves the same way as my original B. :-(

    edit: I'm guessing this code is non-functional for the same reason mine is... C *thinks* it is making this connection, even though B proceeds to ignore everything it says. So you set alarm(5), you make the fake connection, and you set alarm(0). The crux of the problem is (and always has been), "why does C think it has the connection?"


    --
    Linux, sci-fi, and Nat Torkington, all at Penguicon 3.0
    perl -e 'print(map(chr,(0x4a,0x41,0x50,0x48,0xa)))'

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://429239]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-04-18 04:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found