Perl tcp client error

Perl tcp client error

am 20.03.2005 17:35:35 von fabris

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---

Re: Perl tcp client error

am 22.03.2005 21:48:12 von mda

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---
>
>