home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / sockperl.zip / send_cry.nf < prev    next >
Text File  |  1995-03-19  |  2KB  |  106 lines

  1. #!/usr/bin/perl
  2. # os/2 client built to send simple perl scripts over a des
  3. # encrypted socket connection. Hacked from the 'Camel book'
  4. # server and client scripts. Modifications are mostly to
  5. # deal with 'fork()'ed processes not inheriting socket handles
  6. # (hence the use of a second socket address for the reply!)
  7. # Use of des is just for an interesting example, and to test
  8. # use of piped io.
  9.  
  10. $them,$port) = @ARGV;
  11. $port = 12345678 unless $port;
  12. $port2 = 12345679 ;
  13. $them = 'localhost' unless $them;
  14.  
  15. $AF_INET = 2;
  16. $SOCK_STREAM = 1;
  17.  
  18. $SIG{'INT'} = 'dokill';
  19. sub dokill {
  20.     kill 9,$child if $child;
  21. }
  22.  
  23. $sockaddr = 'S n a4 x8';
  24.  
  25. chop($hostname = `hostname`);
  26.  
  27. ($name,$aliases,$proto) = getprotobyname('tcp');
  28. ($name,$aliases,$port) = getservbyname($port,'tcp')
  29.     unless $port =~ /^\d+$/;;
  30. ($name,$aliases,$type,$len,$thisaddr)=gethostbyname($hostname);
  31. ($name,$aliases,$type,$len,$thataddr) = gethostbyname($them);
  32.  
  33. $this = pack($sockaddr, $AF_INET, 0, $thisaddr);
  34. $that = pack($sockaddr, $AF_INET, $port, $thataddr);
  35.  
  36. # Make the socket filehandle.
  37.  
  38. if (socket(S, $AF_INET, $SOCK_STREAM, $proto)) { 
  39.     print "socket ok\n";
  40. }
  41. else {
  42.     warn $!;
  43. }
  44.  
  45. # Give the socket an address.
  46.  
  47. if (bind(S, $this)) {
  48.     print "bind ok\n";
  49. }
  50. else {
  51.     warn $!;
  52. }
  53.  
  54. # Call up the server.
  55.  
  56. if (connect(S,$that)) {
  57.     print "connect ok\n";
  58. }
  59. else {
  60.     die $!;
  61. }
  62.  
  63. # Set socket to be command buffered.
  64.  
  65. select(S); $| = 1; select(STDOUT);
  66.  
  67. # Avoid deadlock by forking.
  68.  
  69. #if($child != fork) {
  70.     while (<STDIN>) {
  71.     print S;
  72.     }
  73.     close(S);
  74.     sleep 2;
  75.     print "done sending\n";
  76. #Accept call back to check reply
  77.  
  78. $this2 = pack($sockaddr, $AF_INET, $port2, "\0\0\0\0");
  79.  
  80. select(NS); $| = 1; select (stdout);
  81.  
  82. socket(S2, $AF_INET, $SOCK_STREAM, $proto) || die "socket: $!";
  83. bind(S2,$this2) || die "bind: $!";
  84. listen(S2,5) || die "connect: $!";
  85.  
  86. select(S2); $| = 1; select(stdout);
  87.  
  88. print "Listening for reply on $port2 ....\n";
  89. #for(;;) {
  90.     ($addr = accept(NS,S2)) || die $!;
  91.  
  92.  
  93.     @list = <NS>;
  94.     print "@list \n";
  95.     
  96. close (NS);
  97.  
  98.     sleep 3;
  99.     do dokill();
  100. #}
  101. #else {
  102. #    while(<S>) {
  103. #    print;
  104. #    }
  105. #}
  106.