F
Franz Prilmeier
Hi,
I want to write a simple SMTP server (As a side note with
Net::SMTP::Server) for evaluating incoming mails. One of my objectives is
having a proper server shutdown. I will try to argue on a code example:
-- Begin Perl Code --
#! /usr/bin/perl -w
use strict;
use IO::Socket;
my $pid;
my $socket = new IO::Socket::INET->new(LocalPort => 10025,
Type => SOCK_STREAM,
Reuse => 1,
Listen => 10 )
or die "Couldn't be a tcp server on port 10025 : $@\n";
$| = 1;
FORK:
{
$pid = fork;
die "can't fork" unless defined $pid;
if ( $pid )
{
print "Child\n";
&run_server ( $socket );
print "Child shutdown finished\n";
}
else
{
print "Parent\n";
sleep 1;
print "Parent shutdown\n";
&stop_server ( $socket );
print "Parent shutdown finished\n";
exit
}
}
sub run_server
{
my $server = shift;
my $client;
while ( $client = $server-> accept () )
{
print "Incomming connection";
}
close ( $server );
}
sub stop_server
{
my $server = shift;
print "Entered Stop Server";
close ( $server );
}
-- End of perl code --
A friend of mine already told me, that $server in the child might not the
same object as $server in the parent thread. So this code example is
plain wrong, but I hope it helps to get you my intention:
I want to be able to close a socket while it is listening (waiting for
incoming connection via the accept call). In a different thread. And
definitely not by killing the whole program. I want to be able to do this
inside the same program. I haven't been able to find any proper solutions
(except connecting to the socket in the stop method - which is not what I
want to do either).
To be short: Is there a clean way to close a socket while it is listening
for incoming connections?
Thanks in advance,
Franz
I want to write a simple SMTP server (As a side note with
Net::SMTP::Server) for evaluating incoming mails. One of my objectives is
having a proper server shutdown. I will try to argue on a code example:
-- Begin Perl Code --
#! /usr/bin/perl -w
use strict;
use IO::Socket;
my $pid;
my $socket = new IO::Socket::INET->new(LocalPort => 10025,
Type => SOCK_STREAM,
Reuse => 1,
Listen => 10 )
or die "Couldn't be a tcp server on port 10025 : $@\n";
$| = 1;
FORK:
{
$pid = fork;
die "can't fork" unless defined $pid;
if ( $pid )
{
print "Child\n";
&run_server ( $socket );
print "Child shutdown finished\n";
}
else
{
print "Parent\n";
sleep 1;
print "Parent shutdown\n";
&stop_server ( $socket );
print "Parent shutdown finished\n";
exit
}
}
sub run_server
{
my $server = shift;
my $client;
while ( $client = $server-> accept () )
{
print "Incomming connection";
}
close ( $server );
}
sub stop_server
{
my $server = shift;
print "Entered Stop Server";
close ( $server );
}
-- End of perl code --
A friend of mine already told me, that $server in the child might not the
same object as $server in the parent thread. So this code example is
plain wrong, but I hope it helps to get you my intention:
I want to be able to close a socket while it is listening (waiting for
incoming connection via the accept call). In a different thread. And
definitely not by killing the whole program. I want to be able to do this
inside the same program. I haven't been able to find any proper solutions
(except connecting to the socket in the stop method - which is not what I
want to do either).
To be short: Is there a clean way to close a socket while it is listening
for incoming connections?
Thanks in advance,
Franz