Solved

Socket connection problem

Posted on 2000-02-24
5
144 Views
Last Modified: 2010-03-05
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
Comment
Question by:hpicd
5 Comments
 

Author Comment

by:hpicd
ID: 2553892
It would appear that it is the 'gethostbyname' call that is failing and I am not trapping it.
0
 
LVL 3

Expert Comment

by:monas
ID: 2554568
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
 
LVL 3

Expert Comment

by:monas
ID: 2554574
sorry, HTTP/1.0 - I missed a dot
0
 
LVL 16

Expert Comment

by:maneshr
ID: 2554586
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
 
LVL 2

Accepted Solution

by:
sanek earned 200 total points
ID: 2554822
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video discusses moving either the default database or any database to a new volume.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now