Hi

My problem is to write a server script which processes incoming queries and responds to them in the usual way ... but also runs a periodic "report" function where it talks back at the client of its own accord. The report code is therefore a seperate thread.

The problem is that I don't find a way to allow the main and reporting threads to both talk back at the client. What seems to happen is that when I hasve the "write to socket" happening in both threads, they block each other in odd ways. (The report lines seem to appear after the first char of a client line is entered.)

This may be only a win32 problem, haven't tried on linux yet.

Here's my first attempt:

use strict; use warnings; use threads; use IO::Socket qw(:DEFAULT :crlf); use Time::HiRes qw( usleep ); my $sock = new IO::Socket::INET (LocalAddr => "localhost", LocalPort => 1000, Proto => 'tcp', Listen => 1, Reuse => 1); my $SOCK; sub sockout{ my $r = shift; print $SOCK "$r\r\n"; } sub chatter{ while (1){ usleep(1000 * 1000); sockout "talkin to myself ..."; } } $SOCK = $sock->accept(); my $thread = threads->create(\&chatter); sockout("CONNECTED!"); while (my $line = <$SOCK>){ sockout("You said ".$line); }

One suggestion that has been made is to use a queue. I tried, same problem. Here it is:

use strict; use warnings; use threads; use Thread::Queue; use IO::Socket qw(:DEFAULT :crlf); use Time::HiRes qw( usleep ); my $sock = new IO::Socket::INET (LocalAddr => "localhost", LocalPort => 1000, Proto => 'tcp', Listen => 1, Reuse => 1); my $SOCK; my $rq; sub sockout{ my $r = shift; print $SOCK "$r\r\n"; } sub chatter{ while (1){ usleep(1000 * 1000); sockout "talkin to myself ..."; } } sub reply_thread{ while (1){ while ($rq->pending){ print "rt pr\n"; print $SOCK $rq->dequeue; }; usleep(1000 * 100); } } $SOCK = $sock->accept(); $rq = new Thread::Queue; my $reply_thread = threads->create(\&reply_thread); my $chatter_thread = threads->create(\&chatter); sockout("CONNECTED!"); while (my $line = <$SOCK>){ sockout("You said ".$line); }

Any ideas?


In reply to sharing a socket between two threads (win32) by danmcb

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.