1 #############################################################################
2 ## Name: samples/socket/wxSocketServerDatagram.pl
3 ## Purpose: wxDatagramSocket demo
4 ## Author: Graciliano M. P., Mattia Barbon
6 ## RCS-ID: $Id: wxSocketServerDatagram.pl 2057 2007-06-18 23:03:00Z mbarbon $
7 ## Copyright: (c) 2004 Graciliano M. P., Mattia Barbon
8 ## Licence: This program is free software; you can redistribute it and/or
9 ## modify it under the same terms as Perl itself
10 #############################################################################
23 my( $frame ) = MyFrame->new( "wxSocket Minimal demo",
24 Wx::Point->new( 50, 50 ),
25 Wx::Size->new( 450, 350 )
28 $this->SetTopWindow( $frame );
38 use Wx qw(:sizer wxTE_MULTILINE );
39 use Wx::Event qw(EVT_BUTTON) ;
41 use Wx qw(wxDefaultPosition wxDefaultSize wxDEFAULT wxNORMAL);
45 my( $this ) = $class->SUPER::new( undef, -1, $_[0], $_[1], $_[2] );
47 my $top_s = Wx::BoxSizer->new( wxVERTICAL );
49 my $text = Wx::TextCtrl->new( $this , -1, '' , wxDefaultPosition, [200,-1] , wxTE_MULTILINE|wxTE_RICH2 );
50 my $button = Wx::Button->new( $this, -1, 'Start' );
52 $this->{TEXT} = $text ;
54 $top_s->Add( $text , 1, wxGROW|wxALL, 5 );
55 $top_s->Add( $button , 0, wxGROW|wxALL, 0);
57 $this->SetSizer( $top_s );
58 $this->SetAutoLayout( 1 );
60 EVT_BUTTON( $this, $button, \&OnConnect );
76 use Wx::Event qw(EVT_SOCKET_INPUT) ;
78 my $addr = Wx::IPV4address->new;
80 $addr->SetService( 4444 );
81 my $sock = Wx::DatagramSocket->new( $addr );
83 EVT_SOCKET_INPUT($this , $sock , \&onConnect ) ;
85 my $stat = $sock->Ok ;
86 $this->{TEXT}->AppendText("Ok: <$stat>\n") ;
87 if (! $stat) { return ;}
92 my ( $sock , $this , $evt ) = @_ ;
94 my $addr = Wx::IPV4address->new;
96 $sock->RecvFrom($addr, $buf, 1000) ;
97 my $addr_str = $addr->GetHostname . ' ' . $addr->GetService;
99 $this->{TEXT}->AppendText("-------------------------\n") ;
100 $this->{TEXT}->AppendText("New Client\n") ;
101 $this->{TEXT}->AppendText(" Peer: <$addr_str>\n") ;
103 $sock->SendTo($addr, "This is a data test!\n", 21) ;
104 $this->{TEXT}->AppendText("\n-------------------------(closed)\n") ;
109 my( $app ) = MyApp->new();