home *** CD-ROM | disk | FTP | other *** search
- @rem = '-*- Perl -*-';
- @rem = '
- @echo off
- perl %0.cmd %1 %2 %3 %4 %5 %6 %7 %8 %9
- goto endofperl
- ';
- #
- # routine to connect to the echo service on a workstation
- # (main use is to test socket code)
- #
- # Note: The echo service is not normally enabled on Intergraph
- # workstations. you must edit /etc/inetd.conf and restart
- # inetd to enable it.
- #
-
- die "usage: sock <host>\n" if scalar(@ARGV) == 0;
-
- $serverhost = shift;
-
- sub sock_close {
- close S;
- die "Socket closed due to SIGINT\n";
- }
-
- $SIG{'INT'} = 'sock_close';
-
- $service = 'echo';
-
- $AF_INET = 2;
- $SOCK_STREAM = 1;
- $IPPROTO_TCP = 6;
-
- $sockaddr = 'S n a4 x8';
- ($name, $aliases, $proto) = getprotobyname('tcp');
- #print "tcp protocol number is $proto\n";
-
- ($name, $aliases, $port, $foo) = getservbyname($service, 'tcp');
- #print "$service port number is $port\n";
-
-
- ($name, $aliases, $adrtype, $length, @serveraddr) =
- gethostbyname($serverhost);
-
- die "unable to get host address for $serverhost"
- if ($length == 0);
-
- ($a, $b, $c, $d) = unpack('C4', $serveraddr[0]);
-
- #print "address for $serverhost is $a.$b.$c.$d\n";
-
- unless (socket(S, $AF_INET, $SOCK_STREAM, $proto)) {
- ($!) = ($!, close(S));
- die "socket failed: $!";
- }
-
- $serverproc = pack($sockaddr, $AF_INET, $port, $serveraddr[0]);
-
- unless (connect(S, $serverproc)) {
- ($!) = ($!, close(S)); # close S while saving $!
- die "connect failed: $!\n";
- }
-
- print "connected to echo service on $serverhost\n";
- print "type control-c to stop program\n";
-
- while(1) {
- print "Input a line: ";
- $line = &gets;
- last if $line eq "quit\n";
- if (send (S, $line, 0) == undef) {
- ($!) = ($!, close(S)); # close S while saving $!
- close S;
- die "send failed: $!\n";
- }
- recv(S, $buffer, 200, 0);
- print "Received: $buffer";
- }
-
- print "Shutting down connection to $serverhost\n";
- shutdown (S, 2);
- close S;
-
-
-
- sub gets {
- local($s) = "";
- local($c);
-
- while(read(STDIN, $c, 1) == 1) {
- last if ($c eq "\n");
- $s .= $c;
- }
- "$s\n";
- }
-
- __END__
- :endofperl
-