On Tue, 24 Sep 2002 10:47:33 +0100, [EMAIL PROTECTED] (Mat
Harris) wrote:

>sorry, i have not made myself clear and the many typos won't have helped
>
>1) user runs client using config file containing username/passwd/domainname
>2) client connects to server and awaits ready code
>3) server sees client and checks sends a ready code
>4) client sends config file info to server
>5) server calls sub in a module to validate username/password/domain
>6) if all ok then send "ok" to client else send "not ok" 
>7) client sees "ok" and sends ip update request
>8) server updates database
>
>The server is running in inittab so no output is seen, but the client will
>print error codes and interpretations to STDOUT
>
>anyone willing to write me some quick examples.
>
>PS, the server needs to be forking (multithreaded)

Here is a "sockets-upload client-server" I wrote with Net::EasyTCP.
The client connects, sends a name,password,and filename.
The server takes the hash and checks it, sends OK if good.
The client then sends the file.
The server responds with OK or any error messages.
It can handle multiple clients.
This can be adapted quite easily to do what you want.

#server
##########################################################
#!/usr/bin/perl
use warnings;
use strict;
use Net::EasyTCP;

$|=1;

local $SIG{INT} = sub { close LOG;print "Exiting\n";exit 0;};
print "Hit control-c to stop server\n";

my $host = "localhost";
my $port = "2345";
my $portpassword = "ztest";
my $maxsize = 1000000; #1 meg
my $savedir = 'files';
my %okusers = (joe => "loco", 
               zentara => "majormojo",
               test =>  "test" );
            


if (! -d $savedir) {
    mkdir($savedir,0755) ;
    print "Save directory created: $savedir\n" ;
}

open(LOG,">>$savedir/port$port.log") or die "Couldn't open port $port
log: $!";

my $server = new Net::EasyTCP(
    host  =>  "$host", 
    mode  =>  "server",
    port  =>   "$port",
    password => "$portpassword",
    )
       || die "ERROR CREATING SERVER: $@\n";


$server->setcallback(
     data            =>      \&gotdata,
     connect         =>      \&connected,
     disconnect      =>      \&disconnected,
    )
     || die "ERROR SETTING CALLBACKS: $@\n";


$server->start() || die "ERROR STARTING SERVER: $@\n";

####################################################
sub gotdata() {
my $client = shift;
my $serial = $client->serial();
my $data = $client->data();

my $reply1 = undef;
my $reply;
my $user = $data->{'user'};
my $pass = $data->{'pass'};
my $filename = $data->{'filename'};
my $filesize = $data->{'filesize'};
my $filedata = $data->{'filedata'};
if((defined $filedata) and (length $filedata >= $maxsize)){ 
            print LOG "ERROR5: Large File trying to sneak in\n";
            $reply1 = "ERROR5: $filename is greater than $maxsize
bytes\n";
            LOG->flush;
            }

if(! defined $filedata){$reply = &process_user($user,$pass)}
else {$reply = $reply1 ||
&process_file($user,$filename,$filesize,$filedata)} 
       

print "Client $serial sent: $data  echoing status-> $reply\n";

$client->send($reply) || die "ERROR SENDING TO CLIENT: $@\n";
if ($data eq "QUIT") {
                    $client->close() || die "ERROR CLOSING CLIENT:
$@\n";
                   }
elsif ($data eq "DIE") {
                   $server->stop() || die "ERROR STOPPING SERVER: $@\n";
                  }
}

###########################################################
sub process_file{
my ($user,$filename,$filesize,$filedata) = @_; 
my $reply;

if(-e "$savedir/$filename")
        { return $reply = "ERROR2: $filename already exists, please
alter name\n"}
if($filesize > $maxsize)
        { return $reply = "ERROR3: $filename is greater than $maxsize
bytes\n"}
if($filedata eq -1){return $reply = "OK-FILE->SEND\n"}

open(FILEIN,">$savedir/$filename") or return $reply = "ERROR4: Could not
create $filename: $!"; 
print FILEIN $filedata;
close FILEIN;

my $size = -s "$savedir/$filename"; 
$reply = "SUCCESS-> $filename $size bytes uploaded ";
my $time = localtime();
print LOG "$reply by $user at $time\n";
LOG->flush;
return "$reply\n";
}
###########################################################
sub process_user{
my ($user,$pass) = @_; 
my $reply;
my $time = localtime();

if((! defined $okusers{$user}) or ($pass ne $okusers{$user}))
                { print LOG "BADPASSWORD  $user  $pass  $time\n";
                  LOG->flush;
          return $reply = "ERROR1: user or password id bad\n";
         }

$reply = "OK-SEND->$user";
print LOG "$reply at $time\n"; LOG->flush;
return "$reply\n";
}

#####################################################
sub connected() {
my $client = shift;
my $serial = $client->serial();
print "Client $serial just connected\n";

my $msginit = "File->upload, max size = $maxsize bytes\n";
$client->send($msginit) || die "ERROR SENDING TO CLIENT: $@\n";
}
###################################################
sub disconnected() {
my $client = shift;
my $serial = $client->serial();
print "Client $serial just disconnected\n";
LOG->flush; 
}
##################################################

#  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #
#  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #
#  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #

#client
#####################################################!/usr/bin/perl
use strict;
use warnings;
use Net::EasyTCP;

my $host = "localhost";
my $port = "2345";
my $portpassword = "ztest";

my $client = new Net::EasyTCP(
        mode => "client",
        host => "$host",
        port => "$port",
        password => "$portpassword",
#        donotcompress   => 1,
#        donotencrypt     => 1,
       )|| die "ERROR CREATING CLIENT: $@\n";

my $encrypt = $client->encryption();
my $compress = $client->compression();
print "encryption method ->$encrypt\ncompression method ->$compress\n";

my $reply = $client->receive() || die "ERROR RECEIVING: $@\n";
print "$reply\n";
my ($maxsize) = $reply =~ /.* = (\d+) bytes/;

my %hash;     #global 
my $filename; #global 
my $filesize; #global

# this section checks user and password
# it exits when an OK-SEND-> is received
while(1){
&getname();
&sendhash();
print "$reply\n";
if($reply =~ /^OK-SEND->(.*)/){last}
}

######user is OK now send files################## 

while (1){
   while (1){
      &getfile();
      $hash{'filedata'} = -1;
      &sendhash();
      print "$reply\n";
      if($reply !~ /^OK-FILE->SEND(.*)/){last}
      &readfile();
      &sendhash();
      print "uploading.....please wait\n";
      print "$reply\n";
      if($reply =~
/^SUCCESS->(.*)/){$hash{'filename'}='';$hash{'filesize'} = '';last}
           }
print "Send another file? (y)/n\n";
my $input =<STDIN>;
         if($input !~ /^[qn]/i){next}else{last}
}

exit 0;
#################################################################
sub getname{
print "What is your name?\n";
$hash{'user'} = <STDIN>;
chop $hash{'user'};

print "What is your password?\n";
$hash{'pass'} = <STDIN>;
chop $hash{'pass'};
}
#################################################################
sub getfile{
my $file;
my $ok =0;
while (1){
print "What file do you want to send?\n";
$file = <STDIN>;
chomp $file;
if (! -s $file) { warn "ERROR! Can't find or blank file $file\n";next}
if (-s $file > $maxsize){warn "ERROR! $file is bigger than
$maxsize\n";next}
if (-s $file){last}
}

$filesize = -s $file;
($filename) = ( $file =~ /([^\\\/]+)[\\\/]*$/gs );
$hash{'filename'} = $filename;
$hash{'filesize'} = $filesize;
}
#################################################################
sub sendhash{
$client->send(\%hash) || die "ERROR SENDING: $@\n";
$reply = $client->receive() || die "ERROR RECEIVING: $@\n";
}
################################################################
sub readfile{
   local $/=undef;
   open(FILEOUT,"<$hash{'filename'}") or die "Couldn't open file: $!";  
   $hash{'filedata'} = (<FILEOUT>);
   close FILEOUT; 
}
##################################################################














-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to