saeen has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have a small code to accept connections on port 8080. This is just part of a tool i am creating.
#!/usr/bin/perl -w use strict; use warnings; use IO::Socket::INET; my $sock = ''; startListening(); sub startListening(){ $sock = new IO::Socket::INET( LocalPort => '8080',Porto => 'tcp',Liste +n => '1', Reuse => '1') or die "Could not create socket \n" unless $sock; my $new_sock = $sock->accept() or die "Lost connection\n"; if ($new_sock){ print "Received incoming connection on port \n"; #close ($new_sock); close($sock); exit; } }
But when i try to telnet to port 8080 it does not work. I have SSH running on the same machine and telnet to port 22 works fine. If i try to telnet port 8080 from local host it works. Any suggestions ?

Replies are listed 'Best First'.
Re: Socket not accepting connection
by MidLifeXis (Monsignor) on Mar 18, 2012 at 10:32 UTC

    $sock = new IO::Socket::INET( LocalPort => '8080',Porto => 'tcp',Liste +n => '1', Reuse => '1')
    I think you mean ($key =~ s/Porto/Proto/)1:
    $sock = new IO::Socket::INET( LocalPort => '8080',Proto => 'tcp',Liste +n => '1', Reuse => '1')
    Have you tried adding LocalAddr? Beyond that, it has been a long time since I have made a trek into rolling my own networking code.

    Footnotes:

    • 1 - Although, from my reading of IO::Socket::INET, it appears that they are functionally equivalent, since Proto => 'tcp' is the default.

    --MidLifeXis

Re: Socket not accepting connection
by Corion (Patriarch) on Mar 18, 2012 at 10:39 UTC

    You also need to bind to the correct IP address for your server:

    LocalAddr => '192.168....', # or whatever the public IP address of +your machine is

    I don't know if IO::Socket::INET supports binding to '*', but maybe the documentation tells more about this.

      To follow up on Corion's binding to '*' proposal, the constant you would be looking for is 'IN_ADDR_ANY', located in Socket.

      --MidLifeXis

Re: Socket not accepting connection
by NetWallah (Canon) on Mar 18, 2012 at 16:39 UTC
    This one works without specifying IP address binding.

    Changes:

    • changed 'Porto' to 'Proto'
    • Port changed to 8081 (My machine already used 8080)
    • Global $sock localized
    • Added "print $new_sock" to send something back (2-way communication)
    • Added peer port and IP info
    • Improved error messaging (added $!)
    • Closed $new_sock
    • Removed old-style sub prototyping :sub(){}
    • I could not figure out why you had "..or die...unless $sock", so I took out the "unless $sock"
    #!/usr/bin/perl -w use strict; use warnings; use IO::Socket::INET; my $sock = startListening(); close ($sock); sub startListening{ my $sock = new IO::Socket::INET( LocalPort => '8081',Proto => 'tcp +',Listen => '1', Reuse => '1') or die "ERROR: Could not create socket.\n$!"; my $new_sock = $sock->accept() or die "ERROR:Lost connection:"; print "Received incoming connection from " .$new_sock->peerhos +t () . ", remote port " . $new_sock->peerport() . "\n"; print $new_sock "Ave, Mundus!\n"; close $new_sock or warn "Could not close new sock:$!"; return $sock; }

                 All great truths begin as blasphemies.
                       ― George Bernard Shaw, writer, Nobel laureate (1856-1950)

Re: Socket not accepting connection
by tobyink (Canon) on Mar 18, 2012 at 20:35 UTC

    As well as all the other suggestions, don't forget to check if you've got a firewall that could be blocking connections on port 8080.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
      Thank you all for your suggestions but guess what CentOS minimal does come with IPTABLES enabled by default. Fixed now :) Cheers