develooper Front page | perl.perl5.porters | Postings from July 2003

Problem between IO::Socket::INET in perl 5.6.1 and perl 5.8.0

Thread Next
From:
=?iso-8859-1?Q?Peter_Gylling_J=F8rgensen?=
Date:
July 29, 2003 08:01
Subject:
Problem between IO::Socket::INET in perl 5.6.1 and perl 5.8.0
Message ID:
1D70CC452533D24998C64F33948E02C22247A0@FRVEMAIL01.fomfrv.dk
Hello all,

I have found a problem conserning IO::Socket::INET and forking servers. 
Below I have put in links to an example which is working correctly in both version 5.6.1 and 5.8.0, but it's not a forking server, so I modified the server part a little bit, and suddently it's not working under perl 5.8.0. I have searched the internet using google and I cannot find anything related to this issue. So I hope you can tell me, that it's a very simple problem holding a strait forward solution.

# Original working examples and description on how to use it
Description: http://www.opensourcedigest.com/issue2/socket.html
SERVER: http://www.opensourcedigest.com/issue2/factor/factorS
CLIENT: http://www.opensourcedigest.com/issue2/factor/factorC

Modified version of factorS into a forking server.

#!/usr/local/bin/perl
use strict;
use IO::Socket;
use Net::hostent;

my $server = IO::Socket::INET->new( 
	Proto     => 'tcp',
	LocalPort => 7801,
	type      => SOCK_STREAM,
	Listen    => SOMAXCONN,
	Reuse     => 1,
);
$server or die "Can't create server\n";

#################################################
#
# Define a small sub function to handle 
# return of the child
#
#################################################
$SIG{CHLD} = sub {wait()};

print "Listening on ", $server->sockport, "\n";

for (;;)
{
  print "Blocking .....\n";
  print "Listening to $server\n";
  my $handler = $server->accept();
  $handler or die "accept: $!\n";
		
  my $pid = fork();
  defined $pid or die "Cannot fork(): $!\n";
		
  if ($pid == 0) {
    # This is the child		

    # Close childs listen socket, not needed
    close($server);
    undef $server;

    # Talk to the remote host    
    my $peeraddr = $handler->peeraddr;
    my $hostinfo = gethostbyaddr($peeraddr);
    printf "CHILD: accept %s\n", $hostinfo->name || $handler->peerhost;
    my $n = <$handler>;
    my @factors = Factor($n);
    print $handler "@factors\n";

    # close connection
    close($handler);
    undef $handler;
    print "CHILD: Ending handling request\n";
    exit(0);
  } else {
    # This is the master process

    # Close connection which is controlled by the child
    print "Master: Connection recieved\n";
    close($handler);
    undef $handler;
  }
}

Dump of call to the server
peg@n17> /usr/bin/perl ~peg/projects/perl/socket_test/factorC triton.fomfrv.dk 7801 42
2 3 7
peg@n18> /usr/bin/perl ~peg/projects/perl/socket_test/factorC bw03.fomfrv.dk 7801 42
2 3 7

Dump of output when it fails
peg@bw03> /usr/bin/perl -v
This is perl, v5.8.0 built for i386-linux-thread-multi
peg@bw03> /usr/bin/perl ~peg/projects/perl/socket_test/factorS
Listening on 7801
Blocking .....
Listening to IO::Socket::INET=GLOB(0x81b2d74)
Master: Connection recieved
Blocking .....
Listening to IO::Socket::INET=GLOB(0x81b2d74)
CHILD: accept n17.fomfrv.dk
CHILD: Ending handling request
accept: Interrupted system call

Dump of output when it works:
peg@triton> /usr/bin/perl -v  
This is perl, v5.6.1 built for alpha-dec_osf-ld
peg@triton> /usr/bin/perl factorS 
Listening on 7801
Blocking .....
Listening to IO::Socket::INET=GLOB(0x140232f10)
Master: Connection recieved
Blocking .....
Listening to IO::Socket::INET=GLOB(0x140232f10)
CHILD: accept n17.fomfrv.dk
CHILD: Ending handling request
Master: Connection recieved
Blocking .....
Listening to IO::Socket::INET=GLOB(0x140232f10)
CHILD: accept n18.fomfrv.dk
CHILD: Ending handling request
Master: Connection recieved
Blocking .....
Listening to IO::Socket::INET=GLOB(0x140232f10)
CHILD: accept n17.fomfrv.dk
CHILD: Ending handling request
Master: Connection recieved
Blocking .....
Listening to IO::Socket::INET=GLOB(0x140232f10)
CHILD: accept n18.fomfrv.dk
CHILD: Ending handling request




\pgj
--
Peter Gylling Jørgensen
Modelling group, section of Oceanography
Royal Danish Administration of Navigation and Hydrography
Overgaden o. Vandet 62B 
DK-1023 København K  
Phone: +45 32 68 96 85
Email: peg@fomfrv.dk
www: http://www.fomfrv.dk/ 

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About