473,405 Members | 2,310 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Forking HTTP Daemon - Problem

Hello,
I'm having some trouble trying to get this simple forking http daemon to
work, when I run it everything seems to run ok and then when I try to
connect to it with a browser on another machine it just spits out hundreds
of error messages every few seconds.

Heres an example:
[error]
print() on closed filehandle GEN1 at ./httpd.pl line 31 (#1)
getpeername() on closed socket GEN1 at
/usr/local/lib/perl5/5.8.2/mach/IO/Socket.pm line 199 (#2)
[/error]

well anyway, here is the program:
#!/usr/bin/perl

use strict;
use warnings;
use diagnostics;
use IO::Socket;

$SIG{CHLD} = sub { wait };

my (
$socket, $client,
$pid
);

$socket = IO::Socket::INET->new(LocalPort => 8080,
Proto => 'tcp',
Listen => 5,
Reuse => 1);

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

while ($client = $socket->accept()) {
die unless defined($pid = fork);
# parent goes to continue block
next if $pid;

# flush all output to client
select $client;
$|++;

print "HTTP/1.0 200 OK\r\n";
print "Content-type: text/html\r\n\r\n";
print "Your IP Address: ", $client->peerhost;

close($client);
exit;
} continue {
close($client);
# go back to the while loop, and wait for a connection
redo;
}

Any help would be appreciated,
Matt.

Jul 19 '05 #1
1 4448
In article <Jd******************@news-binary.blueyonder.co.uk>, Matt
Stevens <ma**@zola.org.uk> wrote:
Hello,
I'm having some trouble trying to get this simple forking http daemon to
work, when I run it everything seems to run ok and then when I try to
connect to it with a browser on another machine it just spits out hundreds
of error messages every few seconds.

Heres an example:
[error]
print() on closed filehandle GEN1 at ./httpd.pl line 31 (#1)
getpeername() on closed socket GEN1 at
/usr/local/lib/perl5/5.8.2/mach/IO/Socket.pm line 199 (#2)
[/error]

well anyway, here is the program:
#!/usr/bin/perl

use strict;
use warnings;
use diagnostics;
use IO::Socket;

$SIG{CHLD} = sub { wait };

my (
$socket, $client,
$pid
);

$socket = IO::Socket::INET->new(LocalPort => 8080,
Proto => 'tcp',
Listen => 5,
Reuse => 1);

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

while ($client = $socket->accept()) {
die unless defined($pid = fork);
# parent goes to continue block
next if $pid;

# flush all output to client
select $client;
$|++;

print "HTTP/1.0 200 OK\r\n";
print "Content-type: text/html\r\n\r\n";
print "Your IP Address: ", $client->peerhost;

close($client);
exit;
} continue {
close($client);
# go back to the while loop, and wait for a connection
redo;
}

Any help would be appreciated,
Matt.


From perldoc -f redo:

"The "redo" command restarts the loop block without evaluating
the conditional again. The "continue" block, if any, is not
executed. If the LABEL is omitted, the command refers to the
innermost enclosing loop. This command is normally used by
programs that want to lie to themselves about what was just
input:"

It is the conditional of the while loop ($client = $socket->accept())
that performs the accept. The redo causes that to be skipped and goes
right to the "die unless ...". So just get rid of the redo. You don't
need it anyway - the loop will continue without it. And it shouldn't be
in a continue block in any case.

FYI: this newsgroup is defunct. Try comp.lang.perl.misc in the future
for better response.
Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Eric S. Johansson | last post by:
I was working on a filter for postfix and instead of using the "fork a new python process on every message" route, I decided to use the SMTP interface instead and try forking after having started...
0
by: SRam | last post by:
I a writing Forking Server for a Small application.. This is code for multiplexing my $listen = IO::Socket::INET->new(Proto => 'tcp',LocalPort => 2323, Listen => 1, Reuse => 1) or die $!; ...
7
by: Michael Ransburg | last post by:
Hi! I have implemented a daemon in C++. It runs all the time, between reboots. Is there a way for other C++ classes to get a reference to the instance of this daemon class in order to call its...
3
by: felixfix | last post by:
Hi all, I am just wondering if something is wrong with my program. What it bascially does is to output a fibonacci sequence base on the command-line output. If I give a 5, it will generate the...
1
by: Stefan Neumann | last post by:
I have written a daemon which should run endlessly. The structure looks like this: - start-stop-daemon forks my python program then: if __name__=="__main__": try: main()
3
by: czajnik | last post by:
Hi! I'm quite new to Python development. Can someone advise me a framework useful for building (pre-)forking or threaded TCP servers, other than SocketServer ? I've seen source code of a few...
2
by: Reid Priedhorsky | last post by:
Hi folks, I am implementing a forking SocketServer daemon that maintains significant internal state (a graph that takes ~30s to build by fetching from a SQL database, and eventually further...
10
by: qwertycat | last post by:
I'm new to multi-process programming, should one avoid forking children from children of a parent? I'd like to spawn 10 children from the parent and each of those children spawns another 5...
3
by: Scottman | last post by:
I am writing a server daemon that forks a child for every incoming request. The child process terminates when the client closes the connection. My problem is that I am unsure how to prevent the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.