Well you are not showing your code, so all we can do is guess
and show code that works. I tend to go in phases with sockets...I get all into it, then it all fades out of my active memory, and it gets hazy. Anyways, the last time I dove in to Tk and sockets, I came up with this as a working example with fork. This is the set of snippets I refer to as a basic starting point for forking Tk sockets with Select. So run them, and see how to make your scripts work the same.
They are sort of a "minimal proof of concept".
#######the server##############################
#!/usr/bin/perl -w
use strict;
use IO::Select;
use IO::Socket;
$| = 1;
# create the socket
my $host = shift || 'localhost';
my $port = 12345;
my $socket = IO::Socket::INET->new(
LocalPort => $port,
LocalAddr => $host,
Listen => 5,
Proto => 'tcp',
Reuse => 1,
);
defined $socket or die "ERROR: Can't create socket: $!\n";
print STDERR "Socket open ... listening for incoming calls ..\n";
my $select = IO::Select->new($socket);
my %socks;
while (1) {
foreach my $fh ($select->can_read) {
if ($fh == $socket) {
# new connection
my $new = $socket->accept;
$select->add($new);
$socks{$new}{FH} = $new;
print STDERR "Received new connection ($new) ..\n";
} else {
my $data = <$fh>;
if (defined $data) {
$data =~ tr/\r\n//d;
foreach my $handle (keys %socks) {
print {$socks{$handle}{FH}} "$handle $data\n";
}
} else {
print "BYE $fh.\n";
$select->remove($fh);
delete $socks{$fh};
$fh->close;
}
}
}
}
########the client######################
<code>
#!/usr/bin/perl
use warnings;
use strict;
use Tk;
use IO::Select;
use IO::Socket;
require Tk::ROText;
# create the socket
my $host = shift || 'localhost';
my $port = 12345;
my $socket = IO::Socket::INET->new(
PeerAddr => $host,
PeerPort => $port,
Proto => 'tcp',
);
defined $socket or die "ERROR: Can't connect to port $port on $host: $
+!\n";
print STDERR "Connected to server ...\n";
my $mw = new MainWindow;
my $log = $mw->Scrolled(qw/ROText -scrollbars ose/)->pack;
my $txt = $mw->Entry()->pack(qw/-fill x -pady 5/);
$mw ->bind('<Any-Enter>' => sub { $txt->Tk::focus });
$txt->bind('<Return>' => [\&broadcast, $socket]);
$mw ->fileevent($socket, readable => sub {
my $line = <$socket>;
unless (defined $line) {
$mw->fileevent($socket => readable => '');
return;
}
$log->insert(end => $line);
});
MainLoop;
sub broadcast {
my ($ent, $sock) = @_;
my $text = $ent->get;
$ent->delete(qw/0 end/);
print $sock $text, "\n";
}
I'm not really a human, but I play one on earth.
flash japh
-
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.