So there I was, happily working my way through Lincoln Stein's new book, when something decided not to work as advertised. I've played with it for quite some time now, and as the book's web site is down, I thought I'd come here and ask for advice.

Stein gives code, which I followed fairly closely, for echo clients and servers, each implemented once with Socket and once with IO::Socket. The Socket implementations worked fine. Then I tried the IO::Socket client implementation, which worked fine against the Socket server. However, neither client worked against the IO::Socket implementation--it looks like a deadlock condition. Finally, just for grins, I telneted directly to the server, and lost the deadlock--the text came right back.

I'm finding this pretty puzzling--I've tried various things, including explicitly setting autoflush on (yes, I know it's on be default, but wotthehell). I'm not getting any success. Any thoughts? I'm using current versions from ActiveState Build 628 on Windows ME. Code snippets follow:

Here's the Socket server code:
my $protocol = getprotobyname('tcp'); socket(SOCK, AF_INET, SOCK_STREAM, $protocol) or die "No socket: $!"; setsockopt(SOCK, SOL_SOCKET, SO_REUSEADDR, 1) or die "No SO_REUSEADDR: + $!"; my $my_addr = sockaddr_in($port, INADDR_ANY); bind(SOCK, $my_addr) or die "No bind: $!"; listen(SOCK,SOMAXCONN) or die "No listen: $!"; warn "listening on port $port...\n"; while ( 1 ) { next unless my $remote_addr = accept(SESSION, SOCK); my ($port, $hisaddr) = sockaddr_in($remote_addr); warn "Connection from [",inet_ntoa($hisaddr),",$port]\n"; SESSION->autoflush(1); while(<SESSION>) { $bytes{'in'} += length($_); chomp; my $msg_out = scalar reverse $_ . "\n"; print SESSION $msg_out; $bytes{'out'} += length($msg_out); } warn "Connection from [",inet_ntoa($hisaddr),",$port] finished\n"; close SESSION; } close SOCK;

This works fine. Now, here's the IO::Socket server code:

my $socket = IO::Socket::INET->new( Listen => 20, LocalPort => $port, Timeout => 5, #60 * 60, Reuse => 1) or die "No socket: $!"; warn "listening on port $port...\n"; while ( !$quit ) { next unless my $session = $socket->accept; my $peer = gethostbyaddr($session->peeraddr,AF_INET) || $session->peerhost; my $port = $session->peerport; warn "Connection from [$peer,$port]\n"; while(<$session>) { #BLOCKS HERE $bytes{'in'} += length($_); chomp; my $msg_out = $_ . CRLF; $session->print($msg_out); print $msg_out; $bytes{'out'} += length($msg_out); } warn "Connection from [$peer,$port] finished\n"; close $session; }

Here's some of the client code, Socket version:

my $socket = IO::Socket::INET->new("$host:$port") or die "No connect: +$!"; my $protocol = getprotobyname('tcp'); my $ok = 1; $socket->autoflush(1); while ( defined (my $msg_out = STDIN->getline) ) { last unless $ok; print $socket $msg_out; my $msg_in = <$socket>; print $msg_in; $bytes{'out'} += length($msg_out); $bytes{'in'} += length($msg_in); }

adamsj

They laughed at Joan of Arc, but she went right ahead and built it. --Gracie Allen

Edited 2001-08-12 by Ovid.


In reply to IO::Socket vs. Socket vs. off-the-shelf telnet by adamsj

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.