Linux and UNIX Man Pages

Linux & Unix Commands - Search Man Pages

wx::socket(3pm) [debian man page]

Wx::Socket(3pm) 					User Contributed Perl Documentation					   Wx::Socket(3pm)

NAME
Wx::Socket - wxSocket* classes USAGE
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 " ;} 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 " ;} 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! ") ; ... or ... $client->Write( $data , length($data) ) ; $client->Close ; } METHODS
All the methods work as in wxWidgets (see the documentation). The functions for reading data (Read, ReadMsg, Peek) take 3 arguments, like the Perl read() function: ## To read the data into the variable $sock->Read($buffer , 1024) ; ... or ... ## To append data at the given offset: $sock->Read($buffer , 1024 , $offset ) ; The write functions (Write, WriteMsg, Unread) can be used with 1 or 2 arguments: $client->Write("This is a data test! ") ; $client->Write($data , $length) ; EVENTS
The events are: EVT_SOCKET EVT_SOCKET_ALL EVT_SOCKET_INPUT EVT_SOCKET_OUTPUT EVT_SOCKET_CONNECTION EVT_SOCKET_LOST The EVT_SOCKET works as in wxWidgets, the others are wxPerl extensions. Note that EVT_SOCKET events of wxSocketClient and wxSocketServer work differently than other event types. First you need to 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) ; Enable the event notification: $sock->Notify(1) ; And only after this use: ## note that $handler must be the same that was used in ## SetEventHandler EVT_SOCKET($handler, $id , sub{...} ) To make the events easier to use, all the proccess is automatic, and you just use: 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. perl v5.14.2 2007-06-18 Wx::Socket(3pm)

Check Out this Related Man Page

SOCKET_BIND(3)								 1							    SOCKET_BIND(3)

socket_bind - Binds a name to a socket

SYNOPSIS
bool socket_bind (resource $socket, string $address, [int $port]) DESCRIPTION
Binds the name given in $address to the socket described by $socket. This has to be done before a connection is be established using socket_connect(3) or socket_listen(3). PARAMETERS
o $socket - A valid socket resource created with socket_create(3). o $address - If the socket is of the AF_INET family, the $address is an IP in dotted-quad notation (e.g. 127.0.0.1). If the socket is of the AF_UNIX family, the $address is the path of a Unix-domain socket (e.g. /tmp/my.sock). o $port (Optional) - The $port parameter is only used when binding an AF_INET socket, and designates the port on which to listen for connections. RETURN VALUES
Returns TRUE on success or FALSE on failure. The error code can be retrieved with socket_last_error(3). This code may be passed to socket_strerror(3) to get a textual explanation of the error. EXAMPLES
Example #1 Using socket_bind(3) to set the source address <?php // Create a new socket $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); // An example list of IP addresses owned by the computer $sourceips['kevin'] = '127.0.0.1'; $sourceips['madcoder'] = '127.0.0.2'; // Bind the source address socket_bind($sock, $sourceips['madcoder']); // Connect to destination address socket_connect($sock, '127.0.0.1', 80); // Write $request = 'GET / HTTP/1.1' . " " . 'Host: example.com' . " "; socket_write($sock, $request); // Close socket_close($sock); ?> NOTES
Note This function must be used on the socket before socket_connect(3). Note Windows 9x/ME compatibility note: socket_last_error(3) may return an invalid error code if trying to bind the socket to a wrong address that does not belong to your machine. SEE ALSO
socket_connect(3), socket_listen(3), socket_create(3), socket_last_error(3), socket_strerror(3). PHP Documentation Group SOCKET_BIND(3)
Man Page