For Programmers: Free Programming Magazines  


Home > Archive > PERL Programming > March 2004 > TCP connection









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author TCP connection
Atanas Boev

2004-03-18, 6:46 pm

hello,

I am trying to make a simple TCP connection to a web server without using
LWP. Our university server simply doesn't have LWP installed, the perl there
is not allowed to qx// (in example lynx) and anyway I just need to do a
simple GET and parse the results. Still, it fails and makes me feel stupid;)

Okay, lets make it simple. I am doing the following on my local machine with
local web server installed:

#-----

#!/usr/local/bin/perl -w

use Socket;
socket (SH, PF_INET, SOCK_STREAM, getprotobyname('tcp')) || die $!;

my $dest = sockaddr_in (80, inet_aton('localhost'));
connect (SH, $dest) || die $!;
$get='GET /blabla.htm HTTP/1.0'."\n\n";

print SH $get;

#-----

So far it works. My weblog shows a connection and a "GET /blabla.htm". I
pasted the get string from my log file, I don't know if I need the "\n\n"
but this way it works when I manualy telnet to local host port 80.

but when I try to $buffer = <SH> it hangs for some time and then $buffer is
undefined. Even more stupid, if I do it by the book -> recv (SH, $buffer) it
gives me a syntax error "Not enough arguments for recv" (???).

When I try through LWP (the GET.pm) it works fine, so there shouldn't be a
problem is not in my perl or web server settings.

Can someone help me with a simple (working ;) )example of making a simple
call to a webhost and return the results? Or I should throw the whole LWP in
my cgi-bin, kindly inviting every hacker to take advantage?:)


Shortly: help!

suncho


gnari

2004-03-18, 6:46 pm

"Atanas Boev" <sun-alabala-cho@cs.tut.fi> wrote in message
news:c1kvdc$2sdq$1@news.cc.tut.fi...
> hello,
>
> I am trying to make a simple TCP connection to a web server without using
> LWP. Our university server simply doesn't have LWP installed, the perl

there
> is not allowed to qx// (in example lynx) and anyway I just need to do a
> simple GET and parse the results. Still, it fails and makes me feel

stupid;)
>
> Okay, lets make it simple. I am doing the following on my local machine

with
> local web server installed:
>

[snipped code using Sicket.pm and connect()]

try using IO::Socket::INET

#!/usr/local/bin/perl
use warnings;
use strinct;

use IO::Socket::INET;
my $sock = IO::Socket::INET->new(PeerAddr => 'localhost',
PeerPort => 'http(80)',
Proto => 'tcp');
my $get='GET /blabla2.htm HTTP/1.1'."\r\nHost: bla\r\nConnection:
close\r\n\r\n";
print "sending:[$get]\n";
print $sock $get;

while(<$sock> ) {
print;
}
close $sock;

gnari



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com