473,545 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::INE T->new(LocalPor t => 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 4457
In article <Jd************ ******@news-binary.blueyond er.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::INE T->new(LocalPor t => 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
1747
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 the Python process. obviously, I needed some way to receive and processed SMTP. smtpd.py works OK if you are willing to use single threaded one at...
0
2257
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 $!; $listen->autoflush(1);
7
4229
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 methods directly? E.G.: 1) System Start - daemon class is instantiated and launched
3
2955
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 first 5 fibonacci number. The problem is, I thought the parent process will always go first, and so here I should get "0, 1, 1, 2, 3" But I ran the ...
1
1975
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
2083
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 python-based app servers, all of theme seem to reinvent the wheel, eventually reusing SocketServer. I'd appreciate a framework with logging,...
2
1834
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 state that may take up to an hour to build). I would like to be able to notify the daemon that it needs to update its state. Because it forks for...
10
2164
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 children which process chunks of data (200 rows) with heavy usage of CPU and regexp
3
5190
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 child process from becoming defunct. Here is an over-simplified main function... int main(void) { // Daemonize int pid;
0
7484
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7415
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7675
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7775
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5997
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5344
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4963
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3451
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
726
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.