Solved

Socket connection problem

Posted on 2000-02-24
5
147 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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 shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

816 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

12 Experts available now in Live!

Get 1:1 Help Now