tcp socket server with threads

1 view
Skip to first unread message

netkiller

unread,
Sep 29, 2008, 6:43:08 AM9/29/08
to netkiller
tcp socket server with threads

Hello,

I'm trying to write a socket server that handles tcp clients using
threads. I've discovered that for every incoming connection 100K of
memory is used up. I understand that this is because each thread has a
copy of all data from the parent, but why is the memory not being
released when the thread ends? I am using perl 5.8.4 on a SMP Debian
system with Linux kernel 2.4.31.

Here is the code, which I suspect may be deficient in several ways
since I'm new to sockets, threads and object usage with perl:

#!/usr/bin/perl
use threads; use IO::Socket;

$sock = new IO::Socket::INET (LocalAddr => '127.0.0.1', LocalPort =>
'8888', Proto => 'tcp', Listen => 5, Reuse => 0) or die ("problem
binding to socket.");

while ($client = $sock->accept()) {
$client->autoflush(1);
$sockthread = threads->new(\&serviceClient,$client);
$sockthread->detach;
}

sub serviceClient {
my $client = $_[0];
print $client "hello\n";
$client->shutdown(2);
return;
}


Reply all
Reply to author
Forward
0 new messages