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;
}
}
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.