Try your own Perl language package for the UDP protocol to be re-packaged to resemble the Python language adorner effect

Source: Internet
Author: User

#!/usr/bin/perl

# Filename:BuildSocketUDP.pm

#

# Copyright Axxeo GmbH

# Licensed under the Apache License, Version 2.0 (the "License");

# You are not a use of this file except in compliance with the License.

# Obtain a copy of the License at

#

# http://www.apache.org/licenses/LICENSE-2.0

#

# unless required by applicable or agreed to writing, software

# Distributed under the License is distributed on a "as is" BASIS,

# without warranties or CONDITIONS of any KIND, either express or implied.

# See the License for the specific language governing permissions and

# Limitations under the License.

#




Package buildsocketudp;

Require exporter;

@ISA = QW (exporter);

@EXPORT = QW (ReadFile checkfile);


Use io::socket::inet; # provides an object interface to creating and using Socket

Use strict ' vars '; # This generates a runtime error if your use symbolic references

Use constant false = 0;

Use constant true = 1;

# flush after every write

$| = 1;


#Create a new instance

Sub New {

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) or die "* Error Server in Socket Creation: $!\n";

Print "UDP Server connected successful is 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) or die "* Error Client in Socket Creation: $!\n";

Print "UDP Client connected successful is created with host: $ip \ n";

Print "UDP Client connected successful is 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

Sub Closesocket

{

my $self = shift;

($self->{' socket '})->close () or Die "* Error to close the socket"

}


#Subroutine to send the data

Sub Sendviasocket

{

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

Sub Recvviasocket

{

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;

}


1; # Of course don't forget the 1 I don't know


This is just to provide you with a way of thinking, after all, is the first attempt to write something similar, inevitably there are shortcomings, I hope you understand, so similar to the effect of Python language decorator, to some basic package inside the method provides more expansion and beautification of the role, but also for later use provided convenient.

This article is from the "EIT Tramp" blog, please be sure to keep this source http://zicowarn.blog.51cto.com/3815716/1614594

Try your own Perl language package for the UDP protocol to be re-packaged to resemble the Python language adorner effect

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.