服务器端程序在这里--[q.
服务器端程序在这里
[quote]
use IO::Socket::INET;
use threads;
use strict;
my $server; #server socket
my $basedir="/var/data";
if ($basedir!~/\/$/){$basedir=$basedir."\/";}
$server=IO::Socket::INET->new(
LocalPort=>5555,
Listen=>500, #maxium connections
Reuse=>1
) or die "socket generation error.\n";
$server->autoflush();
while (accept(my $client,$server))
{
$client->autoflush();
threads->create("process_connection",\$client);
}
sub process_connection
{
my $myclient_ref=$_[0];
my $myclient=$$myclient_ref;
my $conn_id=$_[1];
#read command line.
my $myline=<$myclient>;
if ($myline=~/^quit\r$/)
{
print $myclient "system quiting...\r\n";
print $myclient "bye\r\n";
close($myclient);
}
elsif ($myline=~/^\r$/)
{
}
elsif($myline=~/^put\r$/)
{
&file_put(\$myclient,$basedir);
}
elsif($myline=~/^read\r$/)
{
print readuu(\$myclient);
}
else
{
print $myclient "fail.invalid command"."\r\n";
}
close $myclient;
#print_prompt($myclient);
}
sub file_put
{
my $myclient_ref=$_[0];
my $myclient=$$myclient_ref;
my $basedir=$_[1];
print $myclient "ok.ready to put.\r\n";
my $lineuu=<$myclient>;
#get filename from uuencode head
my $filename;
if ($lineuu=~/^begin\s+[0-7]{3}\s+(\C+?)\r{0,1}$/)
{
$filename=$2;
$filename=getfilename($filename);
print "filename ok.";
print $myclient "ok.begin block is correct.\r\n";
#threads->create("reply",\$myclient,"ok.begin block is correct.");
}else{
print $myclient "fail.begin block is incorrect.\r\n";
#threads->create("reply",\$myclient,"fail.begin block is incorrect.");
return undef;
}
$filename=$basedir.$filename;
print "putting file:".$filename."\n";
#get main content of the uuencoded message from client;
print "getting uu\n";
$lineuu=readuu($myclient_ref);
if ($lineuu eq undef)
{
print $myclient "fail.main content of the uuencoded message is incorrect.\r\n";
#threads->create("reply",\$myclient,"fail.main content of the uuencoded message is incorrect");
return undef;
}
print $lineuu;
my $linebin=unpack("u",$lineuu); #decomment this after testing.
if ($linebin eq undef)
{
print $myclient "fail.file decode fail.\r\n";
#threads->create("reply",\$myclient,"fail.file decode fail");
return undef;
}else
{
print $linebin;
open(my $out_file,">".$filename);#decomment this after testing.
print $out_file $linebin;
close $out_file;
print $myclient "ok. file transmit successfully.\r\n";
#threads->create("reply",\$myclient,"ok. file transmit successfully");
}
}
sub readuu
{
my $client_ref=$_[0];
my $client=$$client_ref;
my $line; #a single line from client socket.
my $lineuu; #the uuencoded string to be returned.
while ($line=<$client>) {
return(undef) if ($line eq undef);
if ($line=~/^M[ -`]{60}\r{0,1}$/) {
$lineuu.=$line;
}elsif($line=~/^[!-L][ -`]{1,59}\r{0,1}$/) {
$lineuu.=$line;
$line=<$client>;
if ($line=~/^end\r{0,1}$/) #uudecode don't have lowercase char except the last line "end".
{
return $lineuu;
}else{return undef;}
}elsif($line=~/^\s\r{0,1}$/)
{
$line=<$client>;
if ($line=~/^end\r{0,1}$/) #uudecode don't have lowercase char except the last line "end".
{
return $lineuu;
}else{return undef;}
}elsif($line=~/^end\r{0,1}$/)
{
return $lineuu;
}else{return undef;}
}
}
sub getfilename
{
#
my $dir=$_[0];
$dir=~s/^((\.\.\/)|(\~\/)|(\.\/))+//;
return $dir;
}
sub reply #used with threads which to reply message to client
{
my $client_ref=$_[0];
my $client=$$client_ref;
my $message=$_[1];
print $client $message;
print $client "\r\n";
}
[/quote]
这段是客户端测试程序
[quote]
use strict;
use IO::Socket::INET;
my $sock=IO::Socket::INET->new
(
PeerAddr=>"192.168.170.91",
PeerPort=>"5555",
Reuse=>1
) or die "could not connect to server.\n";
$sock->autoflush(1);
open(my $fileuu,"/root/textuu.txt");
my @linesuu=<$fileuu>;
my $lineuu=join("",@linesuu);
#print $sock "aaa\r\n";
#my $line=<$sock>;
#print $line;
#print "authed:aaa\n";
print $sock "put\r\n";
#my $line=<$sock>;
#print $line;
#print "putted:put\n";
print $sock "begin 111 aaa.txt\r\n";
#my $line=<$sock>;
#print $line;
#print "begined:begin 111 aaa.txt\r\n";
print $sock $lineuu;
#print "transed:\$lineuu\n";
print $sock "end\r\n";
#my $line=<$sock>;
#print $line;
#print "ended:end\r\n";
[/quote]
多谢了