This script is simple  client example
 The problem :
 This script works fine in Linux
 But On Windows it connects and reads nothing (doesn't print to STDOUT
and doesn't display in UI) from server
 the simple server demo sends a Greeting Banner and current time

 Linux
 Perl v5.8.6 built for i386-linux-thread-multi
 POE 0.3301
 Gtk2 1.062
 Glib 1.120


 Win32
 Perl v5.8.7 built for MSWin32-x86-multi-thread
 POE  0.3401
 Gtk2, Glib  1.100

 More:
 I tried without Component::Client::TCP, just POE and IO::Socket::INET
results are the same while with GUI.
And the command line version of this script without all gui-Gtk2 stuff,
also works fine on windows


#! /usr/bin/perl -w


use strict;
use Gtk2 -init;
use Socket;
use POE qw(Component::Client::TCP);
use POE::Kernel { loop => "Glib" };

my $session = POE::Session->create ( inline_states => {
                _start => \&ui_start,
                ev_connect => \&Tconnect,
                ev_error  => \&server_error
        }
);
$poe_kernel->run();

sub ui_start
{
        my ( $kernel, $session, $heap ) = @_[ KERNEL, SESSION, HEAP ];
        Gtk2::Widget->set_default_direction("ltr");
        my $window = Gtk2::Window->new( 'toplevel' );
        $heap->{main_window} = $window;
    $kernel->signal_ui_destroy( $heap->{main_window} );
                $window->set_border_width(10);
                $window->move(100, 50);
                #$window->set_size_request(800, 600);
                $window->set_title("POE TCP Client");
                $window->signal_connect('delete_event', sub {exit;});
        my $vbox = new Gtk2::VBox(0, 0);
        $window->add( $vbox );
        
        my $textview = new Gtk2::TextView();#
                $textview->set_size_request(150, 150);
                $textview->set_editable( 1 );
                $textview->set_wrap_mode ('char');
        my $textbuff = $textview->get_buffer();
        my $connbutton = new Gtk2::Button("Connect");
        $connbutton->signal_connect( "clicked",
$session->postback('ev_connect', {textbuff=>$textbuff}));
        my $sendbutton = new Gtk2::Button("Send !");
        $sendbutton->signal_connect( "clicked",  $session->postback('ev_send'));
        $vbox->pack_start( $textview, 0, 0, 2 ); # expand, fill, padding
        $vbox->pack_start( $connbutton, 0, 0, 2 ); # expand, fill, padding
        $vbox->pack_start( $sendbutton, 0, 0, 2 ); # expand, fill, padding
        $window->show_all();
}
sub Tconnect
{
        my @data = @{$_[ARG0]};
        my $textbuff = $data[0]{'textbuff'};
        my $host = "192.168.0.7";
        my $port = "12345";
        print "Connecting...\n";
        POE::Component::Client::TCP->new ( RemoteAddress => $host,
        RemotePort    => $port,
        Domain        => AF_INET,        # Optional.
        ServerInput   => sub {
                my $input = $_[ARG0];
                print "from server: $input\n";  
                $textbuff->insert_at_cursor("from server: $input\n");
                        
        }
);
        print "Connected\n";
        print "Reading\n";
        return 1;
}

sub server_error {
        print "Server Error\n";
        exit;
}

Reply via email to