in reply to sharing a socket between two threads (win32)

FileHandles and threads may be useful to you. On linux, at least, you can share filehandles across threads, thru the fileno. That may help you find a way to communicate the fileno of the socket thru shared variables. You might be able to setup a system of shared-variables that signal the threads for that socket when they can send and/or recv. Another possibility would be to pass the socket to the threads in the ->create() statement, then use shared vars to signal them into action of send/recv. But I'm just brainstorming and wondering why you even need 2 threads, when 1 socket can do bi-directional communication? Can't you setup a protocol of some sort, that says, "hey this is a report", and just send it over the single socket? Unless the data is all being sent 1 way, and you don't want to interrupt it.....in that case how about setting up a second socket connection on a separate port for the reports?

This is the basic client I use, which splits itself for birdirectional communication. So all you need to do is in your server, send reports with a header like <report> $report </report>, and have your client regex for it and handle it.

#!/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 || ('localhost',8989); # 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"; # split the program into two processes, identical twins die "can't fork: $!" unless defined( $kidpid = fork() ); # the if{} block runs only in the parent process if ($kidpid) { # copy the socket to standard output while ( defined( $line = <$handle> ) ) { print STDOUT $line; } kill( "TERM", $kidpid ); # send SIGTERM to child } # the else{} block runs only in the child process else { # copy standard input to the socket while ( defined( $line = <STDIN> ) ) { print $handle $line; } }

I'm not really a human, but I play one on earth. Cogito ergo sum a bum

Replies are listed 'Best First'.
Re^2: sharing a socket between two threads (win32)
by danmcb (Monk) on Oct 27, 2006 at 18:31 UTC

    the reason for having threads is simple : one thread has to wait on a line arriving from the user (client), respond to it, and repeat for ever. The other has to just report back autonomously every second. The responses seen at the client should look like this:

    OK OK ? report: 27 OK ? report: 36 OK

    With OK and ? being the responses to client commands. As you say, they are received by the same piece of code which parses them and acts appropriately. That's fine, and nothing to do with needing threads, which is a result of needing to both wait for user input AND act autonomously.

    The simple solution, which I'll likely take, is juts to poll periodically from the client. I just liked the idea of being able to say "start reporting" ... "stop reporting" instead of that.