#!/usr/bin/perl use warnings; use strict; use Tk; use IO::Socket; require Tk::ROText; use Data::Dumper; # create the socket my $host = shift || 'localhost'; my $port = 7070; 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 $text = $mw->Scrolled('Text', -background =>'black', -foreground => 'yellow', )->pack(); my $txt = $mw->Entry()->pack(qw/-fill x -pady 5/); $mw ->bind('' => sub { $txt->Tk::focus }); $txt->bind('' => [\&broadcast, $socket]); $mw ->fileevent($socket, readable => sub { my $line = <$socket>; unless (defined $line) { $mw->fileevent($socket => readable => ''); return; } $text->insert(end => $line); }); $mw->fileevent($socket, 'readable', sub { handle_connection($socket) }); MainLoop; sub broadcast { my ($ent, $sock) = @_; my $text = $ent->get; $ent->delete(qw/0 end/); $sock->print("$text\n"); } sub handle_connection { my ($client) = @_; my $message = <$client>; if (defined $message and $message !~ /^quit/) { $message =~ s/[\r\n]+$//; $text->insert('end', "Got message [$message]\n"); $text->see('end'); } else { $text->insert('end', "Connection Closed\n"); $text->see('end'); $client->close(); } } #### #!/usr/bin/perl use warnings; use strict; use Tk; use IO::Socket; require Tk::ROText; use Data::Dumper; # create the socket my $host = shift || 'localhost'; my $port = 7070; my $socket = IO::Socket::INET->new( LocalPort => $port, Listen => 1, Reuse => 1, Proto => 'tcp', ); defined $socket or die "ERROR: Can't connect to port $port on $host: $!\n"; my $mw = new MainWindow; my $text = $mw->Scrolled('Text', -background =>'black', -foreground => 'yellow', )->pack(); my $txt = $mw->Entry()->pack(qw/-fill x -pady 5/); $mw ->bind('' => sub { $txt->Tk::focus }); $txt->bind('' => [\&broadcast, $socket]); $mw ->fileevent($socket, readable => sub { my $line = <$socket>; unless (defined $line) { $mw->fileevent($socket => readable => ''); return; } $text->insert(end => $line); }); my $client; $mw->fileevent($socket, 'readable', sub { new_connection($socket) }); MainLoop; sub broadcast { my ($ent, $sock) = @_; my $txt = $ent->get; $ent->delete(qw/0 end/); $text->insert('end', "Sending [$txt]\n"); $text->see('end'); $client->print("$txt\n"); } sub new_connection { my ($listen) = @_; $client = $listen->accept() or warn "Can't accept connection"; $client->autoflush(1); $mw->fileevent($client, 'readable', sub { handle_connection($client) }); $client->print("Connected\n"); $text->insert('end', "Connected\n"); $text->see('end'); } sub handle_connection { my ($client) = @_; my $message = <$client>; if (defined $message and $message !~ /^quit/) { $message =~ s/[\r\n]+$//; $client->print("successfully sent $message\n"); $text->insert('end', "Got message [$message]\n"); $text->see('end'); } else { $text->insert('end', "Connection Closed\n"); $text->see('end'); $client->close(); } }