Re: Chat client/server print failed
- From: zentara <zentara@xxxxxxxxxxxxxx>
- Date: Wed, 16 Jan 2008 20:48:18 GMT
On Tue, 15 Jan 2008 16:32:52 -0800 (PST), deadpickle
<deadpickle@xxxxxxxxx> wrote:
This is a chat client wrote in perl Gtk2. THe problem that I am
running into is that when you type and click send I get a "print() on
closed filehandle GEN0 at chat-client.pl line 332" error. This error
is the print statement in the send_msg_all sub. I cant figure out how
the file handle is closed and am wondering if anyone can see why. I'll
leave the server running for testing purposes.
I'm sorry to say, that this complex set of scripts is a PITA to deal
with.
After fixing the many wordwrap problems, and host mismatches,
I got to see your problem.
It's too complex for me to see how to fix it, without alot of work.
Simply put, you need a bi-directional client so the client can
function properly. The way it is setup, your $conn only works
for the first connection, then is closed. So you are printing to a
closed socket. You need a bi-directional client, OR some loop
that keeps the client alive switching between send and recv mode.
Something like
while($select->can_read){
do stuff with the socket
}
Google for select loop examples.
Try starting your server, and testing it with this bi-directional
client. It dosn't follow your original connection protocol, but
it stays alive.
#!/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 = 6666;
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";
}
__END__
zentara
--
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html
.
- Follow-Ups:
- Re: Chat client/server print failed
- From: zentara
- Re: Chat client/server print failed
- References:
- Chat client/server print failed
- From: deadpickle
- Chat client/server print failed
- Prev by Date: Re: Shrink large file according to REG_EXP
- Next by Date: Re: Wait for background processes to complete
- Previous by thread: Re: Chat client/server print failed
- Next by thread: Re: Chat client/server print failed
- Index(es):
Relevant Pages
|