Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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.

In reply to Re: How to do simultaneous reads and writes to/from a socket? by BrowserUk
in thread How to do simultaneous reads and writes to/from a socket? by sonofason

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (8)
As of 2024-04-23 10:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found