Morning Monks - I hope you can help me with a few issues I have with client/server behaviour on windows using TK Perl and sockets. The code below works fine on Linux however when I try and port over to windows I just can't get the client to connect to server. The idea is to launch 2 windows with text widgets that you can enter text to go back and forth between client/server an update the log. Once I can get this working I will move it into an application. Below I have attached the client server code : Client.pl
#!/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('<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; } $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(); } }
Server.pl
#!/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('<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; } $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($clien +t) }); $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(); } }

In reply to Client/Server sockets with TK on Win32 by merseyred

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.