Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 154
  • Last Modified:

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);

0
hpicd
Asked:
hpicd
1 Solution
 
hpicdAuthor Commented:
It would appear that it is the 'gethostbyname' call that is failing and I am not trapping it.
0
 
monasCommented:
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...
0
 
monasCommented:
sorry, HTTP/1.0 - I missed a dot
0
 
maneshrCommented:
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.

0
 
sanekCommented:
If you're behind firewall, you should connect firstly to your gateway.
To do this:
change $server and port to your gateway:
$server = 'my.gateway.com';
$port = gateway_http_port;

and then print as monas suggested:
"GET http://search.excite.com/index.html HTTP/1.0\n\n"

If you're not sure if problem is in your program or gateway, you may try
to connecting it:
`telnet my.gateway.com port`
and printing
"GET http://search.excite.com/index.html HTTP/1.0"
followed by Enter.

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now