#!/usr/bin/perl # tcp-ping.pl - Don Yang (uguu.org) # # 11/21/03 use strict; use Socket; my ($host, $port, $infile, $outfile, $data, $size); local (*SOCKET, *FILE); die "$0 \n" unless( $#ARGV == 3 ); ($host, $port, $infile, $outfile) = @ARGV; $data = undef; if( $infile eq '-' ) { $data .= $_ while(); } else { if( open FILE, "< $infile" ) { read FILE, $data, (-s $infile); close FILE; } } (open FILE, "> $outfile" or die $!) unless( $outfile eq '-' ); unless( $host =~ /^\d+\.\d+\.\d+\.\d+$/ ) { $host = gethostbyname($host); $host = inet_ntoa($host); } print "connect $host:$port..."; socket SOCKET, PF_INET, SOCK_STREAM, getprotobyname('tcp') or die $!; bind SOCKET, sockaddr_in(0, INADDR_ANY) or die $!; connect SOCKET, sockaddr_in($port, inet_aton($host)) or die $!; if( $data ) { print "\nsend data (", length($data), " bytes)..."; defined(send SOCKET, $data, 0) or die $!; } print "\nrecv data...\n"; $size = 0; while( defined(recv SOCKET, $data, 0x4000, 0) ) { last if( length($data) == 0 ); if( $outfile eq '-' ) { print $data; } else { print FILE $data; } $size += length($data); } print "$size bytes received\n"; close FILE if( $outfile ne '-' ); close SOCKET;