hpicd
asked on
Socket connection problem
Hi.
I wrote the following script which is giving me some problems. The script is being run behind a company firewall and I wonder if that is part of the problem.
There are two problems.
1. When it does return anything it seems as if it is coming from the server on which the script is running. e.g. it returns the local 'index.html' and not the page from the remote server.
2. The 'while' loop does not seem to exit until the server timeout. If I run the script from the unix command line it prints the document content but does not return me to the prompt.
The 'connect' seems to work, I am just not convinced that it is connecting to the correct server.
#!/usr/local/bin/perl
print "Content-type: text/html\n\n";
use Socket;
$server = "search.excite.com";
$port = 80;
$server_addr = (gethostbyname($server))[4 ];
$server_struct = pack("S n a4 x8", AF_INET, $port, $server_addr);
$proto = (getprotobyname('tcp'))[2] ;
socket(MYSOCK, PF_INET, SOCK_STREAM, $proto)|| die "Failed to initialize socket: $!\n";
connect(MYSOCK, $server_struct) || die "Failed to connect() to server: $!\n";
select(MYSOCK);
$| = 1;
select(STDOUT);
print MYSOCK "GET /search.gw\n\n";
while (<MYSOCK>) {
print;
}
close(MYSOCK);
I wrote the following script which is giving me some problems. The script is being run behind a company firewall and I wonder if that is part of the problem.
There are two problems.
1. When it does return anything it seems as if it is coming from the server on which the script is running. e.g. it returns the local 'index.html' and not the page from the remote server.
2. The 'while' loop does not seem to exit until the server timeout. If I run the script from the unix command line it prints the document content but does not return me to the prompt.
The 'connect' seems to work, I am just not convinced that it is connecting to the correct server.
#!/usr/local/bin/perl
print "Content-type: text/html\n\n";
use Socket;
$server = "search.excite.com";
$port = 80;
$server_addr = (gethostbyname($server))[4
$server_struct = pack("S n a4 x8", AF_INET, $port, $server_addr);
$proto = (getprotobyname('tcp'))[2]
socket(MYSOCK, PF_INET, SOCK_STREAM, $proto)|| die "Failed to initialize socket: $!\n";
connect(MYSOCK, $server_struct) || die "Failed to connect() to server: $!\n";
select(MYSOCK);
$| = 1;
select(STDOUT);
print MYSOCK "GET /search.gw\n\n";
while (<MYSOCK>) {
print;
}
close(MYSOCK);
First, if you are behind firewall what redirects to some proxy in any event, then changing
print MYSOCK....
to
print MYSOCK "GET http://search.excite.com/index.html HTTP/10\n\n";
may help a little...
print MYSOCK....
to
print MYSOCK "GET http://search.excite.com/index.html HTTP/10\n\n";
may help a little...
sorry, HTTP/1.0 - I missed a dot
i tried the exact program from my server and it works fine.
do you have a proxy in between??
i would suggest this.
from the same server that you are running the script try this.
from the command prompt type
telnet search.excite.com 80
when you see
Connected to rwcsearch1.excite.com.
Escape character is '^]'.
type the following and press enter once
GET /search.gw
if you see a log of HTML ouput then your script has to work too.
do you have a proxy in between??
i would suggest this.
from the same server that you are running the script try this.
from the command prompt type
telnet search.excite.com 80
when you see
Connected to rwcsearch1.excite.com.
Escape character is '^]'.
type the following and press enter once
GET /search.gw
if you see a log of HTML ouput then your script has to work too.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER