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

Re: [MacPerl] Perl sockets on Mac



Vlad Ionesco <d89-vio@nada.kth.se> writes:

}Hello!
}
}I would greatly appreciate some help on the use of sockets on the
}Mac. I need to make a HTTP request and the code below works perfectly
}well on our Sun, but not on my Mac.

There's a simple reason for that

}
}Is there a problem with sockets on the Mac? Or do I only have to

No, there's no problem with sockets.

}change one of the parameters to socket or connect? I'm using MacPerl
}5.1.0r2b2.

Upgrade.  The current version is 5.13r2.  That's not the cause of your
trouble, though.

}
}Thanks in advance!
}
}.Vlad.
}
}
}#!/usr/bin/perl
}
}use Socket;
}
}$server = 'bb.gt.kth.se' || shift;
}$port   = 80 || shift;
}$url    = 'http://bb.gt.kth.se/cgi-bin/bbplupp.cgi';
            ^^^^^^^^^^^^^^^^^^  this is wrong, although it may work with some
                                servers.  It already knows http by the port,
                                and the host by the socket
$url = "/cgi-bin/bbplupp.cgi";
}
}socket  (SOCKET, PF_INET, SOCK_STREAM, (getprotobyname('tcp'))[2]);
}connect (SOCKET, pack('Sna4x8', AF_INET, $port, (gethostbyname($server))[4]))
}   || die "Can't connect to server $server on port $port.\n";
}
}select(SOCKET);
}$| = 1;
}
}print SOCKET "GET $url\n";
                       ^^^ right here is your main problem

print SOCKET "GET $url\012\012";#  \n != \012
}print STDOUT while (<SOCKET>);
}close SOCKET;
}
}exit;

Here's my final version of your script, which worked, on both a Sun and a
Mac (what is it, some kind of scheduling page?)

#!/usr/local/bin/perl

use Socket;

$server = 'bb.gt.kth.se' || shift;
$port   = 80 || shift;
$url    = '/cgi-bin/bbplupp.cgi';

socket  (SOCKET, PF_INET, SOCK_STREAM, (getprotobyname('tcp'))[2]);
connect (SOCKET, pack('Sna4x8', AF_INET, $port, (gethostbyname($server))[4]))
   || die "Can't connect to server $server on port $port.\n";

select(SOCKET);
$| = 1;

print SOCKET "GET $url\012\012";
while (<SOCKET>) {
	s/\012/\n/g;
	print STDOUT;
}
close SOCKET;

exit;


And here's how I'd do it:

#! perl

use LWP::Simple;

getprint "http://bb.gt.kth.se/cgi-bin/bbplupp.cgi";



---
Paul J. Schinder
NASA Goddard Space Flight Center
Code 693, Greenbelt, MD 20771
schinder@pjstoaster.pg.md.us