Hello Monks,

Tk and fileevent is a recurring problem. MS Windows makes it more complex. I started using Tkx but gave up. I have code that seems to work fine on Linux, but fails on MS Windows (Activestate Perl 5.8.9). I attach the simplest server and client. Can someone show me how to make a working MS Windows version? The version shown here freezes during processing of the first client data (I think at the end of sub new_connection).

This code works on Linux (at least on my system)

Thank you
taken from http://www.perlmonks.org/?node_id=546243 Updated after the first 3 replies, using: http://code.activestate.com/lists/perl-tk/%3c3A9D513E.CF3B9466@lehigh. +edu%3e/ (this example is about a client, not a server) #!/usr/bin/perl use strict; use warnings; use IO::Socket; use Tk; $| = 1; $SIG{PIPE} = 'IGNORE'; my $listen = IO::Socket::INET->new( Proto => 'tcp', LocalPort => 7777, Listen => 1, Reuse => 1, ) or die "Can't create listen socket : $!\n"; my $mw = MainWindow->new(); my $text = $mw->Scrolled( 'Text', )->pack(); my ($sel); if ( $^O eq 'MSWin32' ) { $mw->repeat( 50 => [ \&new_connection, $listen ] ); } else { $mw->fileevent( $listen, 'readable' => [ \&new_connection, $listen + ] ); } Tk::MainLoop; sub new_connection { my ($listen) = shift; my ($sel); my $client = $listen->accept() or warn "Can't accept connection"; $client->autoflush(1); if ( $^O eq 'MSWin32' ) { use IO::Select; $sel = IO::Select->new; $sel->add($listen); #<<===== $sel problem?? $mw->repeat( 50 => [ \&handle_connection, $client, $sel ] ); } else { $mw->fileevent( $client, 'readable' => [ \&handle_connection, $client, $sel ] ); } $text->insert( 'end', "Connected\t" ); $text->see('end'); } sub handle_connection { my ( $client, $sel ) = shift; if ( $^O eq 'MSWin32' ) { my (@ready) = $sel->can_read(0); return if $#ready == -1; $client = $ready[0]; } my $message = <$client>; if ( defined $message and $message !~ /^quit/ ) { $message =~ s/[\r\n]+$//; $text->insert( 'end', "Got message [$message]\t" ); $text->see('end'); } else { $text->insert( 'end', "Connection Closed\n" ); $text->see('end'); $client->close(); } }
A simple client
#!/usr/bin/perl use IO::Socket; my $machine_addr = 'localhost'; $sock = new IO::Socket::INET(PeerAddr=>$machine_addr, PeerPort=>7777, Proto=>'tcp', ); die "Could not connect: $!" unless $sock; foreach my $count(1..100){ print $sock "$count\n"; print "$count"; } close ($sock);

In reply to tk, fileevent, and MS Windows by momo33

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.