When you read from TCP socket, you may receive the message in pieces(, but they are in the right order). Do multiple reads, until you detect the predeined end of msg. This is normal.
Update:
I tested with three different ways to read from a socket, read, sysread, and recv, and found read actually gave you a much better chance to recv the whole packet in one call.
use strict;
use IO::Socket;
my $server = IO::Socket::INET->new(Proto => "tcp",
PeerPort => 80,
PeerAddr => "www.yahoo.com",
Timeout => 2000)
|| die "failed to connect\n";
my $req = "GET / HTTP/1.1\r\nHost: www.yahoo.com\r\n\r\n";
print $server $req;
my $count = 0;
while (1) {
my $req;
#$server->recv($req, 700000);
read($server, $req, 700000);
#sysread($server, $req, 700000);
print "packet rcvd:\n", substr($req, 0, 20), "\n";
print "packet count = ", ++ $count, "\n";
exit if ($req eq "");
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.