1Wx::Socket(3) User Contributed Perl Documentation Wx::Socket(3)
2
3
4
6 Wx::Socket - wxSocket* classes
7
9 use Wx qw(:socket) ;
10 use Wx::Event qw(EVT_SOCKET_INPUT EVT_SOCKET_LOST) ;
11 use Wx::Event qw(EVT_SOCKET_CONNECTION) ;
12
13 ##########
14 # CLIENT #
15 ##########
16
17 my $sock = Wx::SocketClient->new(wxSOCKET_WAITALL);
18
19 EVT_SOCKET_INPUT($parent , $sock , \&onInput ) ;
20 EVT_SOCKET_LOST($parent , $sock , \&onClose ) ;
21
22 $sock->Connect('localhost',5050) ;
23
24 if (! $sock->IsConnected ) { print "ERROR\n" ;}
25
26 sub onInput {
27 my ( $sock , $this , $evt ) = @_ ;
28 my $length = 123;
29 my $buffer ;
30 $sock->Read($buffer , 1024 , $length ) ;
31 }
32
33 ##########
34 # SERVER #
35 ##########
36
37 my $sock = Wx::SocketServer->new('localhost',5050,wxSOCKET_WAITALL);
38
39 EVT_SOCKET_CONNECTION($parent , $sock , \&onConnect ) ;
40
41 if ( !$sock->Ok ) { print "ERROR\n" ;}
42
43 sub onConnect {
44 my ( $sock , $this , $evt ) = @_ ;
45 my $client = $sock->Accept(0) ;
46
47 my ($local_host,$local_port) = $client->GetLocal ;
48 my ($peer_host,$peer_port) = $client->GetPeer ;
49
50 $client->Write("This is a data test!\n") ;
51
52 ... or ...
53
54 $client->Write( $data , length($data) ) ;
55
56 $client->Close ;
57 }
58
60 All the methods work as in wxWidgets (see the documentation).
61
62 The functions for reading data (Read, ReadMsg, Peek) take 3 arguments,
63 like the Perl read() function:
64
65 ## To read the data into the variable
66 $sock->Read($buffer , 1024) ;
67
68 ... or ...
69
70 ## To append data at the given offset:
71 $sock->Read($buffer , 1024 , $offset ) ;
72
73 The write functions (Write, WriteMsg, Unread) can be used with 1 or 2
74 arguments:
75
76 $client->Write("This is a data test!\n") ;
77
78 $client->Write($data , $length) ;
79
81 The events are:
82
83 EVT_SOCKET
84 EVT_SOCKET_ALL
85 EVT_SOCKET_INPUT
86 EVT_SOCKET_OUTPUT
87 EVT_SOCKET_CONNECTION
88 EVT_SOCKET_LOST
89
90 The EVT_SOCKET works as in wxWidgets, the others are wxPerl extensions.
91
92 Note that EVT_SOCKET events of wxSocketClient and wxSocketServer work
93 differently than other event types.
94
95 First you need to set the event handler:
96
97 $sock->SetEventHandler($handler, $id) ;
98
99 Then you set what types of event you want to receive:
100
101 ## this select all.
102 $sock->SetNotify(wxSOCKET_INPUT_FLAG|wxSOCKET_OUTPUT_FLAG|
103 wxSOCKET_CONNECTION_FLAG|wxSOCKET_LOST_FLAG) ;
104
105 Enable the event notification:
106
107 $sock->Notify(1) ;
108
109 And only after this use:
110
111 ## note that $handler must be the same that was used in
112 ## SetEventHandler
113 EVT_SOCKET($handler, $id , sub{...} )
114
115 To make the events easier to use, all the proccess is automatic, and
116 you just use:
117
118 EVT_SOCKET_INPUT($handler , $socket , sub{...} )
119 EVT_SOCKET_OUTPUT($handler , $socket , sub{...} )
120 EVT_SOCKET_CONNECTION($handler , $socket , sub{...} )
121 EVT_SOCKET_LOST($handler , $socket , sub{...} )
122
123 ## This is for the events not used yet by the above:
124 EVT_SOCKET_ALL($parent , $socket , sub{...} )
125
126 ** The new way is better to handle more than one socket in the same
127 time too.
128 Take a look in the demos.
129
131 Wx, The wxWxwindows documentation at <http://www.wxwindows.org/>
132
134 Graciliano M. P. <gm@virtuasites.com.br>
135
137 This program is free software; you can redistribute it and/or modify it
138 under the same terms as Perl itself.
139
140
141
142perl v5.28.0 2014-03-08 Wx::Socket(3)