Here is a non-blocking client that sends and receives in separate threads

#!/usr/bin/perl use strict; use warnings; use IO::Socket; use threads; my $server = new IO::Socket::INET( PeerAddr => '127.0.0.1', PeerPort => 1300, Proto => 'tcp', Reuse => 1); die "Client Couldn't connect to server: $!\n" unless $server; threads->create(sub { while (my $msg = <$server>) { print "Recieved: $msg"; } }); threads->create(sub { while (my $msg = <STDIN>) { print "Sending: $msg"; print $server $msg; } });

And a server modified to echo its input. Can't use STDIN because all your clients would fight over it. Though you could create a single thread that listens and then broadcasts out to all clients.

#!/usr/bin/perl use strict; use warnings; use threads; use IO::Socket; print "Starting server: $$\n"; my $server = IO::Socket::INET->new(LocalPort => 1300, Proto => 'tcp', Listen => 10, Reuse => 1); die "Failed to create socket: $!\n" unless $server; sub listen_to_client { my ($client) = @_; my $tid = threads->tid(); while (my $get = <$client>) { print "client ($tid) : $get"; print $client $get; }; print "Client closed\n"; close $client; } while (my $client = $server->accept()) { my $thr = threads->create("listen_to_client", $client); print "Client connected ", $thr->tid,"\n"; $thr->detach; }

___________
Eric Hodges

In reply to Re^4: chat between client and server by eric256
in thread chat between client and server by wavenator

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.