Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: File Transfer via Sockets

by gmpassos (Priest)
on Sep 15, 2002 at 00:04 UTC ( [id://197955]=note: print w/replies, xml ) Need Help??


in reply to File Transfer via Sockets

Take a look in this scripts that I made to transfer files through different OS. This work fine on Perl-5.6.1. If you use Perl-5.8.0 you need to compile it withou PerlIO or set your enverioment variable PERLIO to :raw (maybe will crash with this) or the PerlIO layer will cause some erros with binary files.

I use 2 scrips in the port 6123 (you can change), one client and one server. To use just run the server on the machine that will receive the files, and in the machine that will send, type this:

perl client.pl file_to_send.ext host_name port

CLIENT

#!/usr/bin/perl #################### # SEND FILE CLIENT # #################### use IO::Socket ; $bandwidth = 1024*5 ; # 5Kb/s &send_file( $ARGV[0] , $ARGV[1]||'localhost' , $ARGV[2]||6123 ) ; exit; ############# # SEND_FILE # ############# sub send_file { my ( $file , $host , $port ) = @_ ; if (! -s $file) { die "ERROR! Can't find or blank file $file" ;} my $file_size = -s $file ; my ($file_name) = ( $file =~ /([^\\\/]+)[\\\/]*$/gs ); my $sock = new IO::Socket::INET( PeerAddr => $host, PeerPort => $port, Proto => 'tcp', Timeout => 30) ; if (! $sock) { die "ERROR! Can't connect\n" ;} $sock->autoflush(1); print "Sending $file_name\n$file_size bytes." ; print $sock "$file_name#:#" ; # send the file name. print $sock "$file_size\_" ; # send the size of the file to server. open (FILE,$file) ; binmode(FILE) ; my $buffer ; while( sysread(FILE, $buffer , $bandwidth) ) { print $sock $buffer ; print "." ; sleep(1) ; } print "OK\n\n" ; close (FILE) ; close($sock) ; } ####### # END # #######

SERVER

#!/usr/bin/perl #################### # SEND FILE SERVER # #################### use IO::Socket ; my $port = $ARGV[0] || 6123 ; my $save_dir = './files' ; ############# # START SRV # ############# if (! -d $save_dir) { mkdir($save_dir,0755) ; print "Save directory created: $save_dir\n" ; } my $server = IO::Socket::INET->new( Listen => 5, LocalAddr => 'localhost', LocalPort => $port , Proto => 'tcp' ) or die "Can't create server socket: $!"; print "Server opened: localhost:$port\nWaiting clients...\n\n" ; while( my $client = $server->accept ) { print "\nNew client!\n" ; my ($buffer,%data,$data_content) ; my $buffer_size = 1 ; while( sysread($client, $buffer , $buffer_size) ) { if ($data{filename} !~ /#:#$/) { $data{filename} .= $buffer ; +} elsif ($data{filesize} !~ /_$/) { $data{filesize} .= $buffer ;} elsif ( length($data_content) < $data{filesize}) { if ($data{filesave} eq '') { $data{filesave} = "$save_dir/$data{filename}" ; $data{filesave} =~ s/#:#$// ; $buffer_size = 1024*10 ; if (-e $data{filesave}) { unlink ($data{filesave}) ;} print "Saving: $data{filesave} ($data{filesize}bytes)\n" ; } open (FILENEW,">>$data{filesave}") ; binmode(FILENEW) ; print FILENEW $buffer ; close (FILENEW) ; print "." ; } else { last ;} } print "OK\n\n" ; } ####### # END # #######

Graciliano M. P.
"The creativity is the expression of the liberty".

Replies are listed 'Best First'.
Re^2: File Transfer via Sockets
by Anonymous Monk on May 25, 2017 at 22:00 UTC

    Hi just wanted to check how are you running server side script, is it running as a cron on the destination server? or is it supposed to be manually run on destination server once we are done with running client script of sender server?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://197955]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-04-25 20:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found