Why does my ActivePerl program on Windows stop accepting socket connections?
Asked Answered
G

3

5

I'm using fork() on Perl on Windows (ActivePerl) for a basic socket server, but apparently there are problems (it won't accept connections after a few times), is there any workaround?

Here is the portion of the relevant code:

while($client = $bind->accept()) {
    $client->autoflush();
    if(fork()){ $client->close(); }
    else { $bind->close(); new_client($client); exit(); }
}
Gifu answered 14/3, 2010 at 0:23 Comment(0)
D
6

I'd suggest installing Net::Server and using it instead, because there's probably no compelling reason for you to write all of the listening and forking logic yourself, and Net::Server is already well-tested to work on many platforms including ActivePerl. The Net::Server::Fork personality is the most similar to the code you're writing, although I'd suggest using Net::Server::Prefork instead, since it offers better performance at a minimal cost.

Discriminate answered 14/3, 2010 at 0:51 Comment(0)
N
3

If by a few, you mean exactly 64, then you should see this discussion. In the parent process you should call waitpid -1,&WNOHANG every once in a while to clean up the completed children.

Nest answered 14/3, 2010 at 1:6 Comment(0)
K
1

Windows doesn't have fork. Attempts to emulate it usually give less than ideal results.

The best solution is probably to rewrite your code so it doesn't use fork. Maybe threads?

Kulak answered 14/3, 2010 at 0:30 Comment(1)
Perl on Windows emulates fork() with threads behind the scenes.Granniah

© 2022 - 2024 — McMap. All rights reserved.