- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Perl socket-daemon example
Operating System - HP-UX
1824971
Members
3619
Online
109678
Solutions
Forums
Categories
Company
Local Language
юдл
back
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Forums
Discussions
юдл
back
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-22-2003 05:04 AM
тАО08-22-2003 05:04 AM
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
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 as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-01-2003 03:24 AM
тАО09-01-2003 03:24 AM
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
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"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-01-2003 05:42 AM
тАО09-01-2003 05:42 AM
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
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
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
Company
Learn About
News and Events
Support
© Copyright 2025 Hewlett Packard Enterprise Development LP