Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.05 KB | None | 0 0
  1. #! /usr/bin/perl
  2. use 5.016;
  3. use File::Copy;
  4. use Getopt::Long;
  5. use Cwd;
  6. #use re 'debug';
  7. use DDP;
  8. use Socket ':all';
  9. use Net::Address::IP::Local;
  10. use IO::Select;
  11. use Fcntl qw(F_SETFL O_NONBLOCK);
  12. use FindBin;
  13. Getopt::Long::Configure ('bundling');
  14.  
  15. our $verbose    =       0;
  16. our $help       =       0;
  17.  
  18. GetOptions(
  19.         'v|verbose+' =>\$verbose,
  20.         'h|help'    =>\$help,
  21. );
  22.  
  23.         if ($help){
  24.         print "There are some commands:
  25.        ls      to view files in directory
  26.        cp      to copy file to another directory with oppotunity to change  the  name
  27.        rm      to delete a file
  28.        mv      to rename a file
  29.        mkdir   to create a directory
  30.        rmdir   to delete a directory
  31.        cat     to get data from file in variable and show them
  32.        touch   to create a file
  33.        exit    to exit the program
  34.  
  35.        if you use '!...' you can use these command in local directory.
  36.  
  37.        You can also use flags:
  38.        -h|--help        to show this text
  39.        -v|--verbose     Program will display messages about, what actions it performs with what parameters.\n";
  40.         exit();
  41.  
  42.                   }
  43.  
  44. print "Put a port:";
  45. my $port = <STDIN>;
  46. my $address = '127.0.0.1';
  47.  
  48. socket my $c, AF_INET, SOCK_STREAM, IPPROTO_TCP or die $!;
  49. my  $sa =  sockaddr_in($port, inet_aton($address) ) or die $!;
  50.  
  51. if ( connect($c, $sa)  )
  52. {
  53.         say "CONNECTED";
  54. }
  55. else
  56. {
  57.         die $!;
  58. }
  59. my $s = IO::Select->new();
  60.  
  61. for (\*STDIN, $c) {
  62.         fcntl($_, F_SETFL, O_NONBLOCK) or die $!;
  63.         $s->add($_);
  64. }
  65.  
  66. our $currentdir = getcwd."/";
  67.  
  68. while () {
  69.         for my $fd ($s->can_read()) {
  70.                 while (my $std = <$fd> ) {
  71.                         if ($fd == $c) {
  72.                                 say $std;
  73.                         }
  74.                         else {
  75.                                 say "CLIENT: ";
  76.                                 syswrite $c, $std or die $!;
  77.                         }
  78.                 }
  79.                 die "$!" unless $!{EAGAIN};
  80.         }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement