tcp socket server with threads

1 view
Skip to first unread message


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


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:

use threads; use IO::Socket;

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

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

sub serviceClient {
my $client = $_[0];
print $client "hello\n";

Reply all
Reply to author
0 new messages