Hello,
Everybody, I have code as below. What the code does is receive a
connection and get a file from it, It works ok, but the problem is every
time it receives some files, the memory it occupied augment, and
accumulates every time till it killed by system.

I am beginer of perl, I can't explain why?

I searhc a lot, but they always say "memory will be release by perl
automatically".

#!perl -w

use strict;
use IO::Socket;

my $dir = shift @ARGV;
#....
my $listen_socket = IO::Socket::INET->new (LocalPort => 9999,
Listen => 20,
Proto => 'tcp',
Reuse => 1);

#...

use File::Path;
use threads;

while (1) {
my $connection = $listen_socket->accept ();
if (!defined ($connection)) {
next;
}
# create a new thread to receive files.
threads->create (\&receive_result_file, $connection);
}

sub receive_result_file
{
my $connection = shift (@_);
my $client = $connection->peerhost ();
my $file;
my $buf_len = 2048;


sysread ($connection, $file, $buf_len) or die "无法读取数据。\n";
syswrite ($connection, "OK", 2);

my $buffer;
my $full_file = $dir;
#...

if (!open (THIS_FILE, "+>$full_file")) {
printf "无法打开文件 %s。\n", $full_file;
next;
}

binmode (THIS_FILE);

#...
my $data_len;
while ($data_len = sysread ($connection, $buffer, $buf_len)) {
syswrite ($connection, "OK", 2);
if (!defined (syswrite (THIS_FILE, $buffer, $data_len))) {
close (THIS_FILE);
next;
}
undef $buffer;
}

close (THIS_FILE);
$connection->shutdown (1);
return 0;
}


Reply via email to