Solved

Socket connection problem

Posted on 2000-02-24
5
150 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

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.

Question has a verified solution.

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

In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…

734 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