Dear Monks,
This is my first program using sockets, so I might be doing something foolish - if so excuse me.
I'm trying to write a simple client which connects to a server, receives data, processes it, and prints it to the screen.
The problem I am having is that the program hangs for 20-30 seconds, prints out a spurt of data, and does this several times before the connection times out on the server.
Here's the code:
{
$sock = new IO::Socket::INET ( PeerAddr => $host,
PeerPort => $port,
Proto => 'tcp',
Reuse => 1
);
$sock->autoflush(1);
print $sock "log in information";
my $read_set = new IO::Select($sock);
my $incoming_server_data = "";
while (1) {
my @ready = $read_set->can_read(.5);
foreach my $rh (@ready) {
my $line = <$rh>;
$incoming_server_data .= $line;
parse(\$incoming_server_data);
}
}
}
The printing is done inside the parse function. I thought that might be the problem, but replacing the call to parse() with a simple
print "$line\n"; did not change the problem.
Update
Answered! Changing
my $line = <$rh>; to
sysread $rh, my $line, 1024;, per etcshadow's suggestion, did the trick. Thanks!
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.