2 #############################################################################
3 ## Name: samples/socket/wxSocketServerDatagram.pl
4 ## Purpose: wxDatagramSocket demo
5 ## Author: Graciliano M. P., Mattia Barbon
7 ## RCS-ID: $Id: wxSocketServerDatagram.pl 2057 2007-06-18 23:03:00Z mbarbon $
8 ## Copyright: (c) 2004 Graciliano M. P., Mattia Barbon
9 ## Licence: This program is free software; you can redistribute it and/or
10 ## modify it under the same terms as Perl itself
11 #############################################################################
24 my( $frame ) = MyFrame->new( "wxSocket Minimal demo",
25 Wx::Point->new( 50, 50 ),
26 Wx::Size->new( 450, 350 )
29 $this->SetTopWindow( $frame );
39 use Wx qw(:sizer wxTE_MULTILINE );
40 use Wx::Event qw(EVT_BUTTON) ;
42 use Wx qw(wxDefaultPosition wxDefaultSize wxDEFAULT wxNORMAL);
46 my( $this ) = $class->SUPER::new( undef, -1, $_[0], $_[1], $_[2] );
48 my $top_s = Wx::BoxSizer->new( wxVERTICAL );
50 my $text = Wx::TextCtrl->new( $this , -1, '' , wxDefaultPosition, [200,-1] , wxTE_MULTILINE|wxTE_RICH2 );
51 my $button = Wx::Button->new( $this, -1, 'Start' );
53 $this->{TEXT} = $text ;
55 $top_s->Add( $text , 1, wxGROW|wxALL, 5 );
56 $top_s->Add( $button , 0, wxGROW|wxALL, 0);
58 $this->SetSizer( $top_s );
59 $this->SetAutoLayout( 1 );
61 EVT_BUTTON( $this, $button, \&OnConnect );
77 use Wx::Event qw(EVT_SOCKET_INPUT) ;
79 my $addr = Wx::IPV4address->new;
81 $addr->SetService( 4444 );
82 my $sock = Wx::DatagramSocket->new( $addr );
84 EVT_SOCKET_INPUT($this , $sock , \&onConnect ) ;
86 my $stat = $sock->Ok ;
87 $this->{TEXT}->AppendText("Ok: <$stat>\n") ;
88 if (! $stat) { return ;}
93 my ( $sock , $this , $evt ) = @_ ;
95 my $addr = Wx::IPV4address->new;
97 $sock->RecvFrom($addr, $buf, 1000) ;
98 my $addr_str = $addr->GetHostname . ' ' . $addr->GetService;
100 $this->{TEXT}->AppendText("-------------------------\n") ;
101 $this->{TEXT}->AppendText("New Client\n") ;
102 $this->{TEXT}->AppendText(" Peer: <$addr_str>\n") ;
104 $sock->SendTo($addr, "This is a data test!\n", 21) ;
105 $this->{TEXT}->AppendText("\n-------------------------(closed)\n") ;
110 my( $app ) = MyApp->new();