Problem with socket upon HTTP 
Author Message
 Problem with socket upon HTTP

I've written a PERL script to test TCP sockets upon HTTP protocol. This
script builds a socket with a HTTP daemon, sends a request and display
the answer. It works with every host I've tested except one :
www.yslaire.be.
Here is my script, can anyone help me ? (nota : I run it under WIN95).

--------------------------------------------------------------------------
# browser.pl : small browser

use Socket;

if ($#ARGV != 0) {
    print "Usage : $0 HOST[:PORT]/[URL]\n" ;
    exit 1 ;

Quote:
}

# extract HOST,PORT and FILE from received args : HOST:PORT/FILE

($host,$file) = split (/\//,$ARGV[0]) ;
$file = "/$file" ;
($host,$port) = split (/:/,$host) ;
if ( $port eq "" ) { $port = 80 ; }

# create socket and connect it

$sockaddr = 'S n a4 x8';

($name, $aliases, $proto) = getprotobyname('tcp');
($name, $aliases, $type, $len, $thataddr) = gethostbyname($host);
$that = pack($sockaddr, AF_INET, $port, $thataddr);
socket(S, AF_INET, SOCK_STREAM, $proto) || die "socket: $!";
connect(S, $that) || die "connect: $!";
select(S); $| = 1; select(stdout);

# send http request

send (S,"GET $url HTTP/1.0 \n",0) ;
send (S,"Connection: Keep-Alive\n",0) ;
send (S,"User-Agent: Mozilla/3.0Gold (Win95; I)\n",0) ;
send (S,"Host: $host\n",0) ;
send (S,"Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg,
*/*\n",0) ;
send (S,"\n",0) ;

# receive answer

while (TRUE) {
        recv(S,$ligne,1024,0) ;
        print "$ligne" ;
        if ( length($ligne) == 0 ) { last ; }

Quote:
}

# it's over

close (S) ;
--------------------------------------------------------------------------

I'm fighting with this for several weeks, I've taken a look to
libwww-perl for examples, I've searched informations on all PERL
resources I found, I've read "UNIX Network Programming" by Stevens.
I'm driving CRAZY !



Tue, 06 Jun 2000 03:00:00 GMT  
 Problem with socket upon HTTP

Oops ! I've made a mistake in my script reporting.
Instead of

Quote:
> send (S,"GET $url HTTP/1.0 \n",0) ;

you should read
send (S,"GET $file HTTP/1.0 \n",0) ;

Sorry.



Tue, 06 Jun 2000 03:00:00 GMT  
 Problem with socket upon HTTP

Quote:

>I've written a PERL script to test TCP sockets upon HTTP protocol. This
>script builds a socket with a HTTP daemon, sends a request and display
>the answer. It works with every host I've tested except one :
>www.yslaire.be.

are you sure it works with every host.?  checking for warnings by
invoking perl as

   #!/usr/bin/perl -w

will tell you about some problems.  

Quote:
>Here is my script, can anyone help me ?

Perl helps those who help themselves.  good luck :)

--

Meta Meta FAQ <URL:http://computerdog.com/Meta_MetaFAQ.html>
CGI Meta FAQ <URL:http://computerdog.com/CGI_MetaFAQ.html>



Tue, 06 Jun 2000 03:00:00 GMT  
 Problem with socket upon HTTP

 [courtesy cc of this posting sent to cited author via email]

:send (S,"GET $file HTTP/1.0 \n",0) ;

send()?  What?  

    print S "GET $file HTTP/1.0\n\n";

--tom
--

    "I never met a chocolate I didn't like."
        --Deanna Troi, ST:TNG



Tue, 06 Jun 2000 03:00:00 GMT  
 Problem with socket upon HTTP

Quote:

>  [courtesy cc of this posting sent to cited author via email]


> :send (S,"GET $file HTTP/1.0 \n",0) ;

> send()?  What?

>     print S "GET $file HTTP/1.0\n\n";

> --tom
> --

>     "I never met a chocolate I didn't like."
>         --Deanna Troi, ST:TNG

OK, sorry, I should have said that I've already tryied to work with
my socket using *print S* instead of *send(S* and using *=<S>* instead
of
*recv*. With the same result. I think the problem is not here.
Thanks for your help.


Tue, 06 Jun 2000 03:00:00 GMT  
 Problem with socket upon HTTP

Quote:

> are you sure it works with every host.?  checking for warnings by
> invoking perl as

>    #!/usr/bin/perl -w

> will tell you about some problems.

OK, PERL told me about some problems, but none of them seems to explain
why it doesn't work with www.yslaire.be. I've put my script to its
original form, as shown below, and I'm sorry to say it gave me the same
result.

----------------------------------------------------------------------------------
# browserspl.pl : simple browser

use Socket;

if ($#ARGV != 0) {
    print "Usage : $0 HOST[:PORT]/[URL]\n" ;
    exit 1 ;

Quote:
}

($host,$file) = split (/\//,$ARGV[0]) ;
$file = "/$file" ;
($host,$port) = split (/:/,$host) ;
if ( $port eq "" ) { $port = 80 ; }

$sockaddr = 'S n a4 x8';

($name, $aliases, $proto) = getprotobyname('tcp');
($name, $aliases, $type, $len, $thataddr) = gethostbyname($host);
$that = pack($sockaddr, AF_INET, $port, $thataddr);
socket(S, AF_INET, SOCK_STREAM, $proto) || die "socket: $!";
connect(S, $that) || die "connect: $!";
select(S); $| = 1; select(STDOUT);

print S ("GET $file HTTP/1.0 \n") ;
print S ("Connection: Keep-Alive\n") ;
print S ("User-Agent: Mozilla/3.0Gold (Win95; I)\n") ;
print S ("Host: $host\n") ;
print S ("Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg,
*/*\n") ;
print S ("\n") ;

while ($lig = <S>) {
    print "$lig" ;

Quote:
}

close (S) ;
---------------------------------------------------------------------------------------------------

In fact, it works very well, except with this haunted host in Belgium...



Tue, 06 Jun 2000 03:00:00 GMT  
 Problem with socket upon HTTP


Quote:
> OK, PERL told me about some problems, but none of them seems to explain
> why it doesn't work with www.yslaire.be.

Are you using the proper HTTP protocol? The server seems to be requiring
that you play strictly by the rules.

    http://www.ics.uci.edu/pub/ietf/http/rfc1945.html

When I fix your script to use the proper protocol, it works for me. :-)
This is another reason to use a module that somebody else has already
debugged.

The answer is in section 2.2 of the standards doc, if you'd still prefer
to code it up yourself. Hope this helps!

--
Tom Phoenix           http://www.teleport.com/~rootbeer/

Randal Schwartz Case:  http://www.rahul.net/jeffrey/ovs/
              Ask me about Perl trainings!



Tue, 06 Jun 2000 03:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. HTTP Socket problem....

2. Problem with listboxes/scrollbars upon selection

3. Problem with libwww and HTTP 200, HTTP 302 response messages

4. MacPerl sockets in my HTTP server

5. Using File::Copy and Sockets to copy public files from http addresses

6. HTTP Server using Sockets

7. HTTP & sockets on win32

8. cgi.pm http socket example

9. IO::Socket, HTTP POST vs. GET

10. HTTP Header (using IO::Socket)

11. http+sockets

12. socket to http

 

 
Powered by phpBB® Forum Software