Dear Perl Monks,
I'm trying to make a threaded TCP-server with Perl.
It must run on Linux and MS-Windows, so I use threads.
After some starting problems I have a server that runs
without errors, but I grows with each new connect. I do
no understand what is wrong. Can anyone tell my what to
do to get it right?
Thanks, momo
#!/usr/bin/perl
use strict;
use threads;
use threads::shared;
use IO::Socket;
use IO::Select;
my ($thr,$tid);
my $thrcnt : shared = 0;
my $port = 7070;
my $socket = IO::Socket::INET->new( LocalPort => $port,
Type => SOCK_STREAM,
Listen => SOMAXCONN,
ReuseAddr => 1
) or die "Can't create listen socket
+: $!";
while ((!($tid = threads->tid()))&&(my $client = $socket->accept)) {
$thr = threads->create(\&process_request,$client);
$tid = threads->tid();
print "main: $tid\n";
$thr->detach;
}
print "done: exit\m";
##
## process the request
##
sub process_request {
my $socket = $_[0];
local %ENV = ();
$thrcnt++;
my $line = <$socket>;
#
#
#do my thing
my $ltid = threads->tid();
print "loop: $thrcnt, $ltid\n";
#
#
close $socket;
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.