Hi desiv,

BrowserUK's answer is a good one.

If you really want to do this on Windows, you're not far off.  Just don't do the fork, but make sure that for every line written from the server, there's a corresponding read in the client code.  Also, you need to terminate each to/from the socket with a newline "\n".

Here's how I modified your server code:

#!/usr/bin/perl -w use IO::Socket; use Net::hostent; # for OO version of gethostbyaddr $PORT = 9000; # pick something not in use $server = IO::Socket::INET->new( Proto => 'tcp', LocalPort => $PORT, Listen => SOMAXCONN, Reuse => 1); die "can't setup server" unless $server; print "[Server $0 accepting clients]\n"; while ($client = $server->accept()) { $client->autoflush(1); print $client "Welcome to $0; type help for command list.\n"; $hostinfo = gethostbyaddr($client->peeraddr); my $response = $hostinfo ? $hostinfo->name : $client->peerhost; printf "[Connect from %s]\n", $response; print $client "Command?\n"; while ( <$client>) { next unless /\S/; # blank line if (/quit|exit/i) { last; + } elsif (/date|time/i) { print $client "SELECTED: date/time\n" +; } elsif (/who/i ) { print $client "SELECTED: who\n"; + } elsif (/cookie/i ) { print $client "SELECTED: cookie\n"; + } elsif (/motd/i ) { print $client "SELECTED: motd\n"; + } else { print $client "Commands: quit date who cookie motd\n"; } } continue { print $client "Command?\n"; } close $client; }
And in conjunction with the following, modified client code, they work together well:
#!/usr/bin/perl -w use strict; use IO::Socket; my ($host, $port, $kidpid, $handle, $line); unless (@ARGV == 2) { die "usage: $0 host port" } ($host, $port) = @ARGV; # create a tcp connection to the specified host and port $handle = IO::Socket::INET->new(Proto => "tcp", PeerAddr => $host, PeerPort => $port) or die "can't connect to port $port on $host: $!"; $handle->autoflush(1); # so output gets there right away print STDERR "[Connected to $host:$port]\n"; # Get/display handshake message from server my $handshake = <$handle>; print $handshake; # Socket loop while (1) { my $servermsg = <$handle>; print $servermsg; while (1) { $line = <STDIN>; last if defined($line); } print $handle $line; }

s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/

In reply to Re: Windows TCP socket client hangs in perlipc code by liverpole
in thread Windows TCP socket client hangs in perlipc code by desiv

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.