home *** CD-ROM | disk | FTP | other *** search
/ c't freeware shareware 1997 / CT_SW_97.ISO / pc / software / entwickl / win95 / pw32i306.exe / eg / tcp-clt < prev    next >
Text File  |  1996-06-27  |  991b  |  55 lines

  1. #tcp-client
  2. ( $them, $port ) = @ARGV;
  3.  
  4. $port = 2345 unless $port;
  5. $them = 'localhost' unless $them;
  6.  
  7. $AF_INET = 2;
  8. $SOCK_STREAM = 1;
  9.  
  10. $SIG{'INT'} = 'dokill';
  11. sub dokill {
  12.     kill 9,$child if $child;
  13. }
  14.  
  15. $sockaddr = 'S n a4 x8';
  16.  
  17. #chop($hostname = `hostname`);
  18.  
  19. ($name,$aliases,$proto) = getprotobyname('tcp');
  20. ($name,$aliases,$port) = getservbyname($port,'tcp')
  21.     unless $port =~ /^\d+$/;;
  22. ($name,$aliases,$type,$len,$thisaddr) =
  23.     gethostbyname($hostname);
  24. ($name,$aliases,$type,$len,$thataddr) = gethostbyname($them);
  25.  
  26. $this = pack($sockaddr, $AF_INET, 0, $thisaddr);
  27. $that = pack($sockaddr, $AF_INET, $port, $thataddr);
  28.  
  29. if (socket(S, $AF_INET, $SOCK_STREAM, $proto)) { 
  30.     print "socket ok\n";
  31. }
  32. else {
  33.     die $!;
  34. }
  35.  
  36. if (bind(S, $this)) {
  37.     print "bind ok\n";
  38. }
  39. else {
  40.     die $!;
  41. }
  42.  
  43. if (connect(S,$that)) {
  44.     print "connect ok\n";
  45. }
  46. else {
  47.     die $!;
  48. }
  49.  
  50. select(S); $| = 1; select(STDOUT);
  51.  
  52. while( <STDIN> ) {
  53.     print S;
  54. }
  55.