perl передать файл через socket

Discussion in 'PHP' started by malik555, 4 Jul 2009.

  1. malik555

    malik555 New Member

    Joined:
    4 Feb 2009
    Messages:
    132
    Likes Received:
    1
    Reputations:
    0
    Всем привет !

    Подскажите как передать файл через по сокету на
    сервер ?

    Искал в нете что-то не нашел инфы !
     
  2. Kaimi

    Kaimi Well-Known Member

    Joined:
    23 Aug 2007
    Messages:
    1,732
    Likes Received:
    809
    Reputations:
    231
    Что есть сервер в данном случае, форма для аплоада файлов или скажем демон самописный принимающий файлы?
     
    _________________________
  3. ph1l1ster

    ph1l1ster Elder - Старейшина

    Joined:
    11 Mar 2008
    Messages:
    396
    Likes Received:
    153
    Reputations:
    19
    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;
    }
    И ещё, http://www.perlmonks.org/?node_id=613739
    http://b23.ru/btb
     
  4. malik555

    malik555 New Member

    Joined:
    4 Feb 2009
    Messages:
    132
    Likes Received:
    1
    Reputations:
    0

    В качестве сервера Демон самописный !

    В качестве клиента - тоже обычный скрипт
    и передать обычный файл который лежит у клиента на компе !