Re: Chat client/server print failed



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
.



Relevant Pages

  • [PATCH 0/5] [RFC] AF_RXRPC socket family implementation [try #3]
    ... These patches together supply secure client-side RxRPC connectivity as a Linux ... kernel socket family. ... presentation side is left to the client. ... Each connection goes to a particular "service". ...
    (Linux-Kernel)
  • [PATCH 0/5] [RFC] AF_RXRPC socket family implementation
    ... These patches together supply secure client-side RxRPC connectivity as a Linux ... Make it possible for the client socket to be used to go to more than one ... Each connection goes to a particular "service". ...
    (Linux-Kernel)
  • [PATCH 0/5] [RFC] AF_RXRPC socket family implementation [try #2]
    ... These patches together supply secure client-side RxRPC connectivity as a Linux ... Make it possible for the client socket to be used to go to more than one ... Each connection goes to a particular "service". ...
    (Linux-Kernel)
  • [PATCH 00/16] AF_RXRPC socket family and AFS rewrite [try #4]
    ... The first of these patches together provide secure client-side RxRPC ... connectivity as a Linux kernel socket family. ... Make certain parameters (such as connection timeouts) userspace ... the connectaddress of a client socket by making use of msg_name in the ...
    (Linux-Kernel)
  • [PATCH 00/16] AF_RXRPC socket family and AFS rewrite [try #3]
    ... The first of these patches together provide secure client-side RxRPC ... connectivity as a Linux kernel socket family. ... Make certain parameters (such as connection timeouts) userspace ... the connectaddress of a client socket by making use of msg_name in the ...
    (Linux-Kernel)