Operating System - HP-UX
1824971 Members
3619 Online
109678 Solutions
New Discussion юеВ

Perl socket-daemon example

 
Stanimir
Trusted Contributor

Perl socket-daemon example

Hi!
I'm trying to make a small client-server /using INET-socket/ example on Perl.
Here it is:

#=============================================
# Server #=============================================
use IO::Socket;
my $sock = new IO::Socket::INET (

LocalAddr => 'servername.com',
LocalPort => '7777',
Proto => 'tcp',
Listen => 1,
Reuse => 1,
);

die "Could not create socket: $!\n" unless $sock;

my $new_sock = $sock->accept();

while(defined($Line = <$new_sock>)) {
print $Line;
}
close($sock);

#=============================================# Client -- using object interface
#=============================================
use IO::Socket;
my $sock = new IO::Socket::INET (
PeerAddr => 'servername.com',
PeerPort => '7777',
Proto => 'tcp',
);
die "Could not create socket: $!\n" unless $sock;
print $sock "Hi there!\n";
$sock->send("Hi again!\n");
close($sock);

It is working! Now I want to do the following:

1. To make this server to work like a daemon.
I've tried with:

use Proc::Daemon;
Proc::Daemon::Init();

This was working like daemon , but was damaging socket-part.

2. To make this server to "fork" child process,which will execute row
"print $Line" itself and died after that.
Main proccess must remain live after this
action.

Is anywhere a suitable modules and simple code
for doing this?

Thank's in advance! A points-gift will be done!
Stan
2 REPLIES 2
Mark Grant
Honored Contributor

Re: Perl socket-daemon example

Not sure what you mean by make the server work like a daemon. A daemon is simply a program that runs in the background without you worrying about it.

If you want to achieve this, your script should fork and then exit. The child should then close it's standard input.

e.g.

# Lets pop into the background

$fpid=fork;

if($fpid != 0){
exit;
}

close STDIN;

# All my exciting code comes now!


For part 2, the procedure is to again, fork, do the work and exit.

The question suggests you want to fork off a child for each line of input from your socket. This could be dangerous if you suddenly receive hundreds of lines of input throught the socket so you'll have to watch for that. The solution is slightly different if you want the server to wait for the fork to finish before continuuing the while loop or not.

If you don't want to wait then this will do the trick.

$pid=fork();
if($pid == 0 ) { # I.e. I am the child
print $line;
exit;
}

If you do want to wait, the server will have to "wait" after the if statement block and preferebly trap the SIGCHLD that gets sent when the child process dies.

Hope this is useful
Never preceed any demonstration with anything more predictive than "watch this"
Steven E. Protter
Exalted Contributor

Re: Perl socket-daemon example

Two ideas:
http://forums.itrc.hp.com/cm/QuestionAnswer/1,,0x29e735067c18d6118ff40090279cd0f9,00.html

http://forums.itrc.hp.com/cm/QuestionAnswer/1,,0x661a543254bfd611abdb0090277a778c,00.html

Both A. Clay specials.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com