Add libwx-perl
[pkg-perl] / deb-src / libwx-perl / libwx-perl-0.96 / .pc / hashbang.patch / samples / socket / wxSocketClientDatagram.pl
1 #############################################################################
2 ## Name:        samples/socket/wxSocketClientDatagram.pl
3 ## Purpose:     wxDatagramSocket minimal demo
4 ## Author:      Graciliano M. P., Mattia Barbon
5 ## Created:     07/02/2003
6 ## RCS-ID:      $Id: wxSocketClientDatagram.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 #############################################################################
11
12 use Wx;
13 use Wx::Socket ;
14
15 package MyApp;
16
17   use vars qw(@ISA);
18   @ISA=qw(Wx::App);
19
20 sub OnInit {
21   my( $this ) = @_;
22
23   my( $frame ) = MyFrame->new( "wxSocket Minimal demo",
24                                Wx::Point->new( 50, 50 ),
25                                Wx::Size->new( 450, 350 )
26                              );
27
28   $this->SetTopWindow( $frame );
29   $frame->Show( 1 );
30
31   1;
32 }
33
34 package MyFrame;
35   use vars qw(@ISA);
36   @ISA=qw(Wx::Frame);
37
38   use Wx qw(:sizer wxTE_MULTILINE );
39   use Wx::Event qw(EVT_BUTTON) ;
40
41   use Wx qw(wxDefaultPosition wxDefaultSize wxDEFAULT wxNORMAL);
42
43 sub new {
44   my( $class ) = shift;
45   my( $this ) = $class->SUPER::new( undef, -1, $_[0], $_[1], $_[2] );
46
47   my $top_s = Wx::BoxSizer->new( wxVERTICAL );
48
49   my $text = Wx::TextCtrl->new( $this , -1, '' , wxDefaultPosition, [200,-1] , wxTE_MULTILINE );
50   my $button = Wx::Button->new( $this, -1, 'Connect' );
51
52   $this->{TEXT} = $text ;
53
54   $top_s->Add( $text , 1, wxGROW|wxALL, 5 );
55   $top_s->Add( $button , 0, wxGROW|wxALL, 0);
56
57   $this->SetSizer( $top_s );
58   $this->SetAutoLayout( 1 );
59
60   EVT_BUTTON( $this, $button, \&OnConnect );
61
62   return( $this ) ;
63
64   $this;
65 }
66
67
68 #############
69 # ONCONNECT #
70 #############
71
72 sub OnConnect {
73   my $this = shift ;
74
75   use Wx qw(:socket) ;
76   use Wx::Event qw(EVT_SOCKET_INPUT EVT_SOCKET_LOST) ;
77
78   my $addr = Wx::IPV4address->new;
79   $addr->SetHostname( 'localhost' );
80   $addr->SetService( 5555 );
81   my $sock = Wx::DatagramSocket->new( $addr );
82
83   EVT_SOCKET_INPUT($this , $sock , \&onInput ) ;
84
85   my $addr2 = Wx::IPV4address->new;
86   $addr->SetHostname( 'localhost' );
87   $addr->SetService( 4444 );
88   $sock->SendTo( $addr, "123456\n", 7 );
89
90   $this->{TEXT}->AppendText("-------------------------\n") ;
91 }
92
93 sub onInput {
94   my ( $sock , $this , $evt ) = @_ ;
95   my $buf = '#BEGIN#' ;
96   my $addr = Wx::IPV4address->new;
97   while( $sock->RecvFrom( $addr, $buf , 1000 ) ) {
98     if ($buf =~ /\n$/s) { last ;}
99   }
100   $this->{TEXT}->AppendText($buf."#END#") ;
101   $sock->Destroy;
102 }
103
104 package main;
105
106   my( $app ) = MyApp->new();
107   $app->MainLoop();
108
109 exit ;
110
111 # Local variables: #
112 # mode: cperl #
113 # End: #