[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[MacPerl] socket communications on a Mac



Is it possible to make an internet socket connection work on a Mac in the 
same manner as with Unix?

(entire script below)


--
Scott

#!/usr/bin/perl

print "Content-type: text/html\n\n";
# print "test for http output...\n<br>"; # bugger

# test address
$form{'url'} = "http://www.yahoo.com/headlines/news/summary.html";

if ($form{'url'} =~ /http:\/\//i) {$valid_request_type = 'yes';}
else {$valid_request_type = 'no'; &error_message('invalid_request_type');}

if ($valid_request_type eq 'yes')
{
$form{'url'} =~ s/http:\/\///i;
@whole_path = split (/\//, "$form{'url'}");
$target_host = shift (@whole_path);
$file_path = ("/" . join ("/", @whole_path));
}

$AF_INET = 2;
$SOCK_STREAM = 2;
$socket_address = "S n a4 x8";
$port = 80;
$request_type = "GET";
$http_version = "HTTP/1.0\n\n";
$request = "$request_type $file_path $http_version";

print qq!
\$target_host - $target_host<br>
\$file_path - $file_path<br>
\$request - $request<br>
!; # bugger

&connect;
exit;

sub connect
{
$AF_INET = 2;
$SOCK_STREAM = 1;
$sockaddr = 'S n a4 x8';
chop($hostname = `hostname`);
($name,$aliases,$proto) = getprotobyname('tcp');
($name,$aliases,$port) = getservbyname($port,'tcp') unless $port =~ 
/^\d+$/;
($name,$aliases,$type,$len,$thisaddr) = gethostbyname($hostname);
	    if (!(($name,$aliases,$type,$len,$thataddr) = 
gethostbyname($target_host)))
     {
     return -1;
     }
$this = pack($sockaddr, $AF_INET, 0, $thisaddr);
$that = pack($sockaddr, $AF_INET, $port, $thataddr);
     if (!(socket(S, $AF_INET, $SOCK_STREAM, $proto)))
     {
     $SOCK_STREAM = 2;
          if (!(socket(S, $AF_INET, $SOCK_STREAM, $proto))) {return -2;}
     }
     if (!(bind(S, $this))) {return -3;}
     if (!(connect(S,$that))) {return -4;}
     select(S); $| = 1; select(STDOUT);
     print S "$request";
          while (<S>) {print "$_";}
     close(S);
}

sub error_message
{
my $error_type = (join("", @_));
%errors = ('invalid_request_type','urls must begin with "http://"');
print "$errors{$error_type}\n<br>";
}



***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch