Try to wrap the TCP protocol in your own Perl language with the effect of a python-like language decorator

Source: Internet
Author: User

#!/usr/bin/perl

# Filename:BuildSocketTCP.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 buildsockettcp;

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 = = ' TCP ')

{

My $socket = new Io::socket::inet (

#LocalHost = ' 0.0.0.0 ',

LocalPort = $port | | ' 7777 ',

Proto = ' TCP ',

Listen = 5,

Reuse = 1) or die "* Error Server in Socket Creation: $!\n";

Print "TCP Server connected successful is created with port: $port \ n";

print "---------------------\ n";

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

}

Else

{

My $socket = new Io::socket::inet (

Peerhost = $ip | | ' 127.0.0.1 ',

Peerport = $port | | ' 7777 ',

Proto = ' tcp ') or die "* Error Client in Socket Creation: $!\n";

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

Print "TCP Client connected successful is created with port: $port \ n";

print "---------------------\ n";

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

$self->{' socket '} = $socket; #将新建的socket作为类似 class properties are stored in Dict

}

#print $self->{' socket '}. " Aaaa\n ";

Bless ($self); # Here should be aware that only bless the self variable itself

return $self; # Return The reference to the hash.

}

#Subroutine to accept the socket

Sub AcceptSocket

{

my $self = shift;

return $self->{' socket '} = $self->{' sock '}->accept ();

}


#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, $length);

($self->{' socket '})->flush;

Print "Send data successful via TCP 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 TCP socket: $description >>: $data _in\n";

return $data _in;

}


1;


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/1614593

Try to wrap the TCP protocol in your own Perl language with the effect of a python-like language decorator

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.