Wx::Socket(3pm)
π Wx::Socket π
okay bestie, welcome to Wx::Socket β aka the wxSocket* classes, straight out of the User Contributed Perl Documentation π
if you've ever wanted your Perl app to actually talk to other computers (iconic behaviour, honestly), you're in the right place.
this is a Perl module, it lives in section 3pm, and it's giving networking realness. π
β¨ USAGE β¨
like, here's the whole vibe, client and server, ready to go π«Ά
use Wx qw(:socket) ;
use Wx::Event qw(EVT_SOCKET_INPUT EVT_SOCKET_LOST) ;
use Wx::Event qw(EVT_SOCKET_CONNECTION) ;
##########
# CLIENT #
##########
my $sock = Wx::SocketClient->new(wxSOCKET_WAITALL);
EVT_SOCKET_INPUT($parent , $sock , \&onInput ) ;
EVT_SOCKET_LOST($parent , $sock , \&onClose ) ;
$sock->Connect('localhost',5050) ;
if (! $sock->IsConnected ) { print "ERROR\n" ;}
sub onInput {
my ( $sock , $this , $evt ) = @_ ;
my $length = 123;
my $buffer ;
$sock->Read($buffer , 1024 , $length ) ;
}
##########
# SERVER #
##########
my $sock = Wx::SocketServer->new('localhost',5050,wxSOCKET_WAITALL);
EVT_SOCKET_CONNECTION($parent , $sock , \&onConnect ) ;
if ( !$sock->Ok ) { print "ERROR\n" ;}
sub onConnect {
my ( $sock , $this , $evt ) = @_ ;
my $client = $sock->Accept(0) ;
my ($local_host,$local_port) = $client->GetLocal ;
my ($peer_host,$peer_port) = $client->GetPeer ;
$client->Write("This is a data test!\n") ;
... or ...
$client->Write( $data , length($data) ) ;
$client->Close ;
}
no cap, that's the whole starter pack πΈ
π METHODS π
all the methods work exactly like they do in wxWidgets (go read the documentation, bestie, it's all there).
π reading
the read-y functions β Read, ReadMsg, Peek β take 3 arguments, just like Perl's own read() function. iconic consistency π
## To read the data into the variable
$sock->Read($buffer , 1024) ;
... or ...
## To append data at the given offset:
$sock->Read($buffer , 1024 , $offset ) ;
βοΈ writing
and the write girlies β Write, WriteMsg, Unread β they're flexible, they take 1 or 2 arguments:
$client->Write("This is a data test!\n") ;
$client->Write($data , $length) ;
π¦ EVENTS π¦
the events are, and I'm listing them all because every single one matters:
EVT_SOCKET
EVT_SOCKET_ALL
EVT_SOCKET_INPUT
EVT_SOCKET_OUTPUT
EVT_SOCKET_CONNECTION
EVT_SOCKET_LOST
so EVT_SOCKET does its thing the same way as in wxWidgets, and the rest of them? those are wxPerl extensions, periodt π
β οΈ heads up bestie:EVT_SOCKETevents ofwxSocketClientandwxSocketServerwork differently than other event types. not a drill.
First you gotta set the event handler:
$sock->SetEventHandler($handler, $id) ;
Then you set what types of event you want to receive:
## this select all.
$sock->SetNotify(wxSOCKET_INPUT_FLAG|wxSOCKET_OUTPUT_FLAG|
wxSOCKET_CONNECTION_FLAG|wxSOCKET_LOST_FLAG) ;
Then enable the event notification:
$sock->Notify(1) ;
And only after all of that, use:
## note that $handler must be the same that was used in
## SetEventHandler
EVT_SOCKET($handler, $id , sub{...} )
okay but here's the slay part β¨ to make events way easier to use, all of that process is automatic, so you literally just do:
EVT_SOCKET_INPUT($handler , $socket , sub{...} )
EVT_SOCKET_OUTPUT($handler , $socket , sub{...} )
EVT_SOCKET_CONNECTION($handler , $socket , sub{...} )
EVT_SOCKET_LOST($handler , $socket , sub{...} )
## This is for the events not used yet by the above:
EVT_SOCKET_ALL($parent , $socket , sub{...} )
The new way is better to handle more than one socket in the same time too. π
Take a look in the demos. π
π SEE ALSO π
Wx- The wxWxwindows documentation at <http://www.wxwindows.org/>
π AUTHOR π
Graciliano M. P. <gm@virtuasites.com.br>
π COPYRIGHT π
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
and that's the whole page, bestie π this was perl v5.42.3, dated 2026-08-20, and it's the Wx::Socket(3pm) manual page β you ate, go build something with sockets π«Άβ¨
π¦ where this page comes from
- ubuntu stonking: libwx-perl 1:0.9932-10build2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms