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_SOCKET events of wxSocketClient and wxSocketServer work 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 πŸ‘€


🌟 AUTHOR 🌟

Graciliano M. P. <gm@virtuasites.com.br>


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