Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^2: Client/Server sockets with TK on Win32

by zentara (Archbishop)
on Apr 16, 2012 at 18:32 UTC ( [id://965373]=note: print w/replies, xml ) Need Help??


in reply to Re: Client/Server sockets with TK on Win32
in thread Client/Server sockets with TK on Win32

The issue seems to be that the server doesn't seem to be calling new_connection from the fileevent call.

Then comment out your first fileevent and see what happens. Your text handling should be done in the handle_connection subroutine.

Below is how the server code should look, I don't know how you got it so confused. Notice how clients get created in new_connection, and handled by handle_connection.

#!/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 => 7070, Listen => 1, Reuse => 1, ) or die "Can't create listen socket : $!\n"; my $mw = MainWindow->new(); my $text = $mw->Scrolled('Text', -background =>'black', -foreground => 'yellow', )->pack(); my $subframe = $mw->Frame()->pack(); $subframe->Button(-text => 'Clear', -command => sub { $text->delete('1.0','end'); })->pack(-side=>'left'); $subframe->Button(-text => 'Save Log', -command => sub { })->pack(-side=>'left'); $subframe->Button(-text => 'Exit', -command => sub { exit })->pack(-side=>'right'); $mw->fileevent($listen, 'readable', sub { new_connection($listen) }); Tk::MainLoop; sub new_connection { my ($listen) = @_; my $client = $listen->accept() or warn "Can't accept connection"; $client->autoflush(1); $mw->fileevent($client, 'readable', sub { handle_connection($clien +t) }); $client->print("Connected\n"); $text->insert('end', "Connected\t"); $text->see('end'); } sub handle_connection { my ($client) = @_; my $message = <$client>; if (defined $message and $message !~ /^quit/) { $message =~ s/[\r\n]+$//; $client->print("Got message [$message]\n"); #echo back if wanted $text->insert('end', "Got message [$message]\t"); $text->see('end'); } else { $text->insert('end', "Connection Closed\n"); $text->see('end'); $client->close(); } }

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

Replies are listed 'Best First'.
Re^3: Client/Server sockets with TK on Win32
by BrowserUk (Patriarch) on Apr 16, 2012 at 21:47 UTC

    Even with your (very sensible) modification, under windows, the listener's readable event handler is never called. Even though the connection from client to server is established. And even if the listener socket is set nonblocking.

    My conclusion is that Tk fileevents simply do not work on windows.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

        I don't know why you think that would make any difference.

        1. The description of "how to use ioctl properly" is moot.

          You'll notice that he doesn't say that using the oft-described method of setting non-blocking doesn't work, only that it works "for the wrong reasons".

          That's okay. Just so long as it works!

          His "proper way" is no such thing. It is at best a different way of working around the fact that the implementation of ioctl is broken.

          There is no way that Perl programmer's should be messing around with pointers. pack 'p' (or is it pack 'P') is even more badly documented than ioctl and far, far too easy to get wrong and cause crashes.

          The proper way would be to fix ioctl.

        2. Those examples do not use Tk fileevents. Tk fileevents don't work on Windows. Ergo: The OPs approach will not work on Windows.

          The limitation is in the Windows Tk implementation. Not Perl, nor Windows.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        The start of some sanity?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://965373]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-04-23 06:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found