This code is not intended as a good example of using threads, but is intended to show how to do bi-directional communications via a single socket under Win32.
It looks a little complicated because it is both client and server:
#! perl -slw
use strict;
use threads;
use IO::Socket;
$| = 1;
## The server.
async {
## Create the listening port
my $server = IO::Socket::INET->new(
LocalHost => 'localhost',
LocalPort => 9999,
Listen => 5,
Reuse => 1,
) or die $!;
## Accept connections
while( my $client = $server->accept ) {
## Set the socket non-blocking
ioctl( $client, 0x8004667e, \1 ) or die $^E;
## Start a thread to receive and display inbound packets
async {
my $in = '';
while( 1 ) {
## Use to accumulate whatever is available
sysread( $client, $in, 100, length( $in ) );
## When we've accumulated a complete line
if( my $p = 1 + index $in, "\n" ) {
## display it and remove it from the buffer
printf 'S: %s', substr $in, 0, $p+1, '';
}
## Stop the non-blocking read from running away with t
+he cpu
Win32::Sleep 100;
}
};
## Start another thread to simulate input from other clients
async {
print $client 'Some text' while sleep 1;
};
}
};
## The client
## Connect to the server
my $server = IO::Socket::INET->new(
'localhost:9999'
) or die $!;
## Set the socket non-blocking
ioctl( $server, 0x8004667e, \1 );
## Start a thread to recieve and display input from the server
async {
my $in = '';
while( 1 ) {
## Accumulate input and display when we've got a full line
sysread( $server, $in, 100, length( $in ) );
if( my $p = 1 + index $in, "\n" ) {
printf 'C: %s', substr $in, 0, $p+1, '';
}
Win32::Sleep 100;
}
};
## Give the threads a chance to start
sleep 2;
## The main thread reads from the keyboard and sends to the server.
while( <STDIN> ) {
print $server $_;
}
NOTE: No 'select' in sight.
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Outside of code tags, you may need to use entities for some characters:
| |
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.
|
|