1 #############################################################################
2 ## Name: samples/socket/wxSocketClient.pl
3 ## Purpose: wxSocketClient minimal demo
4 ## Author: Graciliano M. P.
6 ## RCS-ID: $Id: wxSocketClient.pl 2057 2007-06-18 23:03:00Z mbarbon $
7 ## Copyright: (c) 2003-2004 Graciliano M. P.
8 ## Licence: This program is free software; you can redistribute it and/or
9 ## modify it under the same terms as Perl itself
10 #############################################################################
30 my( $frame ) = MyFrame->new( "wxSocket Minimal demo",
31 Wx::Point->new( 50, 50 ),
32 Wx::Size->new( 450, 350 )
35 $this->SetTopWindow( $frame );
45 use Wx qw(:sizer wxTE_MULTILINE );
46 use Wx::Event qw(EVT_BUTTON) ;
48 use Wx qw(wxDefaultPosition wxDefaultSize wxDEFAULT wxNORMAL);
52 my( $this ) = $class->SUPER::new( undef, -1, $_[0], $_[1], $_[2] );
54 my $top_s = Wx::BoxSizer->new( wxVERTICAL );
56 my $text = Wx::TextCtrl->new( $this , -1, '' , wxDefaultPosition, [200,-1] , wxTE_MULTILINE );
57 my $button = Wx::Button->new( $this, -1, 'Connect' );
59 $this->{TEXT} = $text ;
61 $top_s->Add( $text , 1, wxGROW|wxALL, 5 );
62 $top_s->Add( $button , 0, wxGROW|wxALL, 0);
64 $this->SetSizer( $top_s );
65 $this->SetAutoLayout( 1 );
67 EVT_BUTTON( $this, $button, \&OnConnect );
83 use Wx::Event qw(EVT_SOCKET_INPUT EVT_SOCKET_LOST) ;
85 my $sock = Wx::SocketClient->new(wxSOCKET_WAITALL);
87 EVT_SOCKET_INPUT($this , $sock , \&onInput ) ;
88 EVT_SOCKET_LOST($this , $sock , \&onClose ) ;
90 my $stat = $sock->Connect('localhost',5050) ;
92 my $stat = $sock->IsConnected ;
93 $this->{TEXT}->AppendText("IsConnected: <$stat>\n") ;
94 if (! $stat) { return ;}
96 my @lcaddr = $sock->GetLocal ;
97 my @praddr = $sock->GetPeer ;
98 $this->{TEXT}->AppendText("Local: <@lcaddr>\n") ;
99 $this->{TEXT}->AppendText("Peer: <@praddr>\n") ;
101 $this->{TEXT}->AppendText("-------------------------\n") ;
105 my ( $sock , $this , $evt ) = @_ ;
106 my $buf = '#BEGIN#' ;
107 while( $sock->Read($buf , 1 , length($buf) ) ) {
108 if ($buf =~ /\n$/s) { last ;}
110 $this->{TEXT}->AppendText($buf."#END#") ;
114 my ( $sock , $this , $evt ) = @_ ;
115 $this->{TEXT}->AppendText("\n-------------------------(closed)\n") ;
121 my( $app ) = MyApp->new();