Hi,
I am trying to write some test code for bigger project where my perl script will send file over to server. This is the current SERVER and CLIENT code I have so far. When I type "hi" from client I get hello back from server and like wise I send send command I get respond back the problem comes when I type "put <filename>" on client, the file transfer finishes but I get ERROR in client "Can't use an undefined value as a symbol reference at ./client.pl line 43."
Any help in debugging this issue would be highly appreciated.
SERVER CODE:
#!/usr/bin/perl
use strict;
use IO::Socket;
my $filename;
my @command;
my $server = IO::Socket::INET->new(
Listen => 5,
LocalAddr => '127.0.0.1',
LocalPort => 5050,
Proto => 'tcp'
) or die "Can't create server socket: $!";
$server->autoflush(1);
while(my $client = $server->accept)
{
while (<$client>)
{
print $_ ;
@command = split(/ /, $_);
if(lc(trim($command[0])) eq 'time') { print $client &getTimeStamp()."\r\n"; }
elsif(lc(trim($command[0])) eq 'hi') { print $client "Hello!\r\n"; }
## if honeypot has some data to send
elsif(lc(trim($command[0])) eq 'send')
{
print $client "You said to send some data\r\n";
}
## if we see put command then save the file
elsif(lc(trim($command[0])) eq 'put')
{
if(defined($command[1]) && -e trim($command[1]))
{
$filename = trim($command[1]);
print $client "GOAHEAD\r\n";
open FILE, ">./filez_t/$filename";
binmode FILE;
while (<$client>)
{
if (trim($_) eq 'DONE')
{
close FILE;
last;
}
print FILE $_;
}
close FILE;
print $client "Thanks for sending the file\n"
}
}
else { print $client "Unknown Command\r\n"; }
}
}
sub getTimeStamp
{
# Get the all the values for current time
(my $Second, my $Minute, my $Hour, my $Day, my $Month, my $Year, my $WeekDay, my $DayOfYear, my $IsDST) = localtime(time);
# adding 1900 will create a four digit year value
$Year = $Year - 100;
# Adding 1 to month because months don't start at 0
$Month++;
#format the current time into the correct format and return it
return sprintf("%02d%02d%02d%02d%02d%02d",$Year,$Month,$Day,$Hour,$Minute,$Second);
}
sub trim($)
{
my $string = shift;
$string =~ s/^\s+//;
$string =~ s/\s+$//;
$string =~ s/[\r]+//;
$string =~ s/[\n]+//;
return $string;
}
CLIENT CODE:
#!/usr/bin/perl
use strict;
use IO::Socket;
my $server = IO::Socket::INET->new(
PeerAddr => '127.0.0.1',
PeerPort => 5050,
Proto => 'tcp'
) or die "Can't create client socket: $!";
$server->autoflush(1);
my $i = 0;
my $msg;
my @command;
my $filename="who_connected.pl";
while(1)
{
print "ASAP> ";
$msg = <STDIN>;
print $server $msg."\r";
while(<$server>)
{
@command = split(/ /, $msg);
if(lc(trim($command[0])) eq 'put')
{
$filename = trim($command[1]);
open FILE, "$filename" or die "Can't open: $!";
binmode FILE;
while (<FILE>)
{
print $server $_;
print "*";
}
close FILE;
print $server "\nDONE\n";
}
print $_ ."\n";
last if eof($_);
}
}
sub trim($)
{
my $string = shift;
$string =~ s/^\s+//;
$string =~ s/\s+$//;
$string =~ s/[\r]+//;
$string =~ s/[\n]+//;
return $string;
}