Abstract: This article mainly introduces how to try to create your own Perl language package and repackaging the UDP protocol to make it look like a python language decorator.
use IO::Socket::INET; # provides an object interface to creating and using Socket use strict 'vars'; # this generates a runtime error if you use symbolic references use constant false =>0; use constant true =>1;
# flush after every write $| = 1;
#Create a new instance subnew{ my $self = {}; # Connect the hash to the package Cocoa. shift; my ($ip, $port, $proto, $isserver) = @_; my $socket; my $self->{'ip'} = $ip; my $self->{'port'} = $port; if ($isserver == true && $proto == 'udp') { #print "Buldi socket for server\n"; $socket = new IO::Socket::INET( LocalPort => $port || '8765', Blocking =>'0', Proto => $proto) ordie"* Error Server in Socket Creation : $!\n"; print"UDP Server connected successful be created with port : $port\n"; print"---------------------\n"; } else { #print "Buldi socket for client\n"; $socket = new IO::Socket::INET( PeerHost => $ip || '127.0.0.1', PeerPort => $port || '8765', Blocking =>'0', Proto => $proto) ordie"* Error Client in Socket Creation : $!\n"; print"UDP Client connected successful be created with host : $ip\n"; print"UDP Client connected successful be created with port : $port\n"; print"---------------------\n"; } #print "$socket"."\n"; $self->{'socket'} = $socket; #print $self->{'socket'}."AAAA\n"; bless ($self); return $self; # Return the reference to the hash. }
#Subroutine to close the socket subcloseSocket { my $self = shift; ($self->{'socket'})->close() ordie"* Error to close the socket" }
#Subroutine to send the data subsendViasocket { my $self = shift; my ($data_out, $length , $description) = @_; ($self->{'socket'})->send($data_out); #($self->{'socket'})->flush; #print "Send data successful via udp socket>> : $description >>: $data_out\n"; }
#Subroutine to recv the data subrecvViasocket { my $self = shift; my ($length, $description) = @_; my $data_in; ($self->{'socket'})->recv($data_in, $length); #($self->{'socket'})->flush; #print "Recvied data successful via udp socket: $description >>: $data_in\n"; return $data_in; }