加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

尝试自己的Perl语言的包 UDP协议的再包装起到类似python语言装饰

发布时间:2020-12-15 23:47:27 所属栏目:大数据 来源:网络整理
导读:#!/usr/bin/perl? # Filename: BuildSocketUDP.pm # # ? Copyright 2012 Axxeo GmbH # ? Licensed under the Apache License,Version 2.0 (the "License"); # ? you may not use this file except in compliance with the License. # ? You may obtain a cop

#!/usr/bin/perl?

# Filename: BuildSocketUDP.pm

#

# ? Copyright 2012 Axxeo GmbH

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

# ? you may not use this file except in compliance with the License.

# ? You may obtain a copy of the License at

#

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

#

# ? Unless required by applicable law or agreed to in writing,software

# ? distributed under the License is distributed on an "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 you 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 servern";

? $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 be created with port : $portn";

? ? print "---------------------n";

? }

? else

? {

? ? #print "Buldi socket for clientn";

? ? $socket = new IO::Socket::INET(

? ? PeerHost => $ip ?|| '127.0.0.1',

? ? PeerPort => $port || '8765',

? ? Proto ? ?=> $proto) or die "* Error Client in Socket Creation : $!n";

? ? print "UDP Client connected successful be created with host : $ipn";

? ? print "UDP Client connected successful be created with port : $portn";

? ? print "---------------------n";

? }

? #print "$socket"."n";

? $self->{'socket'} = $socket;

? #print $self->{'socket'}."AAAAn";

? 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_outn";


}


#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_inn";

return $data_in;

}


1; # 当然不要忘了这个不知所以然的1


这只是给大家提供一种思路,毕竟是第一次尝试写类似的东西,难免存在不足之处,希望大家谅解,这样类似与Python语言装饰器的效果,给一些基本的包里面的方法提供了更多扩展和美化的作用,也为后来使用提供了方便。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读