http://www.perlmonks.org?node_id=1008970


in reply to Telnet Client/Server: What am I doing wrong?

You have explained your problem, but without running examples, it is hard to say what is wrong. Try this Tk socket client, and see how it works.
#!/usr/bin/perl use warnings; use strict; use Tk; use IO::Socket; require Tk::ROText; # 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 $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.
Old Perl Programmer Haiku ................... flash japh

Replies are listed 'Best First'.
Re^2: Telnet Client/Server: What am I doing wrong?
by PM_Visitor (Initiate) on Dec 15, 2012 at 17:22 UTC

    Thank you, very much.

    Not knowing any better, I started with the Tkx version of Tk. So, without rewriting your example, it's not going to run.
    I guess I can install the Tk package (new to that also, don't know if installing Tk will break Tkx (and what I've scruffed together so far--2 weeks of learning :) )).

    I would upload the working program, but it's now at about 1500 lines... so should I zip the program up and post it? It's probably not what an experienced Perl programmer would create.

    I have started reading 'Network Programming with Perl, Lincoln Stein'. Perhaps that will enable me to resolve my obtuseness.

    I'm not giving up! The first version was in HTML5, CSS3, and Javascript. The problem there was that WebSockets (and Flash) have a handshaking overhead before they will open a plain socket. Security reasons it looks like. (so poof, there went 4 days of work... should have nailed down the comm part first).

    So anyway, thanks for the response.... will installing Tk break Tkx?

      will installing Tk break Tkx?

      No, they are completely different modules, although Tkx does closely mimic Tk. You can install them side by side, no problem.


      I'm not really a human, but I play one on earth.
      Old Perl Programmer Haiku ................... flash japh

        I guess I have resolved some of the problems I was having. Thanks for the help. I've spent some long hours staring at
        (sometimes called 'reading') the Lincoln Stein book... and trying examples. What it boiled down to was:
        1). Noob at the contols.
        2). The equipment at work functions differently than the sever I was able to cook up for dev. (compounded the confusion).
        3). When I use the methods in IO::Socket::INET, I can talk adequately talk to the equipment at work.
        4). My local scruffed-together-server is still broken, but I'm over it.
        5). The IO::Socket::INET in its basic mode is text and line oriented...which works for this program.

      I would upload the working program, but it's now at about 1500 lines... so should I zip the program up and post it?
      No. It is inconsiderate to expect unpaid volunteers to read and understand 1500 lines of your code.

      Instead, you should prepare and post here a short self-contained correct (compilable) example that demonstrates the problem you are trying to solve.