Home > Archive > PERL Programming > March 2005 > Perl tcp client error
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 |
Perl tcp client error
|
|
| fabris 2005-03-20, 3:57 pm |
| I've a problem with this code
that just hangs while sending the
binary data to the server.
I need to know how to prevent this lock.
Tnx for your help into solving this problem
Fabris.
---code---
use Socket;
#use strict;
my $myhostname = "localhost";
$filetouploadname = "Untitled-3.psd";
$uploaddescr = "untitolo";
$uploadaffirm = "1";
my $port = 80;
$ufilesize = -s $filetouploadname ;
($g, $g, $proto) = getprotobyname("tcp");
($g, $g, $g, $g, $rawserver) = gethostbyname($myhostname);
$serveraddr = pack("Sna4x8", 2, $port, $rawserver);
socket(SOCKET, AF_INET, SOCK_STREAM, $proto) || die "No socket: $!";
connect(SOCKET, $serveraddr) || die "cannot connect";
select(SOCKET); $| = 1; # flush immediatelly SOCKET data
select(STDOUT); $| = 1;
$cont_len = length($uploaddescr) + length($uploadaffirm) + $ufilesize;
$string_to_send = "POST it.php HTTP/1.1\n"
.. "Host: " . $myhostname . ":" . $port . "\n"
.. "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)\n"
.. "Accept:
text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=
0.8,image/png,*/*;q=0.5\n"
.. "Accept-Language: en-us,en;q=0.5\n"
.. "Accept-Encoding: gzip,deflate\n"
.. "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\n"
.. "Keep-Alive: 300\n"
.. "Connection: close\n"
.. "Content-Type: multipart/form-data;
boundary=---------------------------20106283638626\n"
.. "Content-Length: " . $cont_len . "\n"
.. "\n\n-----------------------------20106283638626\n"
.. "Content-Disposition: form-data; name=\"uplFile\"; filename=\"" .
$filetouploadname . "\"\n"
.. "Content-Type: application/octet-stream\n\n";
open(IN, "<" . $filetouploadname ) or print "cannot open file ---> $!";
binmode(IN);
while (read(IN,$b,1024)) { $string_to_send .= $b; }
close(IN);
$string_to_send .= "\nContent-Disposition: form-data; name=\"upltitle\"\n"
.. "\n-----------------------------20106283638626\n"
.. "\n" . $uploaddescr . "\n"
.. "-----------------------------20106283638626\n"
.. "Content-Disposition: form-data; name=\"uplbody\"\n"
.. "\n" . $uploadaffirm . "\n"
.. "-----------------------------20106283638626\n"
.. "Content-Disposition: form-data; name=\"uplsubmit\"\n"
.. "\nUpload file\n"
.. "-----------------------------20106283638626--\n";
print SOCKET $string_to_send;
print "OK";
close SOCKET;
exit();
---code---
| |
| Marco A P D'Andrade 2005-03-22, 8:55 pm |
| Verify use of LWP::UserAgent or LWP::Simple...
fabris wrote:
> I've a problem with this code
> that just hangs while sending the
> binary data to the server.
> I need to know how to prevent this lock.
>
> Tnx for your help into solving this problem
>
> Fabris.
>
>
> ---code---
>
> use Socket;
>
> #use strict;
>
> my $myhostname = "localhost";
>
> $filetouploadname = "Untitled-3.psd";
>
> $uploaddescr = "untitolo";
>
> $uploadaffirm = "1";
>
> my $port = 80;
>
> $ufilesize = -s $filetouploadname ;
>
> ($g, $g, $proto) = getprotobyname("tcp");
>
> ($g, $g, $g, $g, $rawserver) = gethostbyname($myhostname);
>
> $serveraddr = pack("Sna4x8", 2, $port, $rawserver);
>
> socket(SOCKET, AF_INET, SOCK_STREAM, $proto) || die "No socket: $!";
>
> connect(SOCKET, $serveraddr) || die "cannot connect";
>
> select(SOCKET); $| = 1; # flush immediatelly SOCKET data
>
> select(STDOUT); $| = 1;
>
>
>
>
>
> $cont_len = length($uploaddescr) + length($uploadaffirm) + $ufilesize;
>
> $string_to_send = "POST it.php HTTP/1.1\n"
>
> . "Host: " . $myhostname . ":" . $port . "\n"
>
> . "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)\n"
>
> . "Accept:
> text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=
> 0.8,image/png,*/*;q=0.5\n"
>
> . "Accept-Language: en-us,en;q=0.5\n"
>
> . "Accept-Encoding: gzip,deflate\n"
>
> . "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\n"
>
> . "Keep-Alive: 300\n"
>
> . "Connection: close\n"
>
> . "Content-Type: multipart/form-data;
> boundary=---------------------------20106283638626\n"
>
> . "Content-Length: " . $cont_len . "\n"
>
> . "\n\n-----------------------------20106283638626\n"
>
> . "Content-Disposition: form-data; name=\"uplFile\"; filename=\"" .
> $filetouploadname . "\"\n"
>
> . "Content-Type: application/octet-stream\n\n";
>
> open(IN, "<" . $filetouploadname ) or print "cannot open file ---> $!";
>
> binmode(IN);
>
> while (read(IN,$b,1024)) { $string_to_send .= $b; }
>
> close(IN);
>
> $string_to_send .= "\nContent-Disposition: form-data; name=\"upltitle\"\n"
>
> . "\n-----------------------------20106283638626\n"
>
> . "\n" . $uploaddescr . "\n"
>
> . "-----------------------------20106283638626\n"
>
> . "Content-Disposition: form-data; name=\"uplbody\"\n"
>
> . "\n" . $uploadaffirm . "\n"
>
> . "-----------------------------20106283638626\n"
>
> . "Content-Disposition: form-data; name=\"uplsubmit\"\n"
>
> . "\nUpload file\n"
>
> . "-----------------------------20106283638626--\n";
>
> print SOCKET $string_to_send;
>
> print "OK";
>
> close SOCKET;
>
> exit();
>
>
>
> ---code---
>
>
|
|
|
|
|