Use format and common methods (general methods for parent class IO: handle and IO: file are not described below ):
========================================================== ========================================================== ======================================
-----------------------------------------------------------------------------
Import IO: Socket package:
Use IO: socket;
Explanation:
IO: socket has two sub-classes: IO: Socket: iNet and IO: Socket: Unix. We
Now I am using IO: Socket: iNet.
-----------------------------------------------------------------------------
New () method:
Socket object variable = IO: Socket: iNet-> New (socket variable value );
Instance:
$ Sock = IO: Socket: iNet-> New ('192. 168.1.2: 23 ');
Explanation:
All Perl object programming uses the object 'visualized 'as a variable. The socket handle here
The returned value of this method is the socket object variable. The parameters used here are
In simple parameter mode, the socket address structure in double quotation marks or quotation marks is 'host IP address or domain name: Port
Or 'host IP address or domain name: Service name (Port Number )'.
In addition to the simplest single-parameter call, the new method also has many parameters that can be called selectively.
To summarize these parameters:
**************************************** *******************************
Parameter description Value Type
+ +
Peeraddr remote host address [: port or Service]
Peerhost is the same as peeraddr
Peerport remote port or service
Localaddr local address host address [: port or Service]
Localhost is the same as localaddr.
Localport local port or service
Protocol protocol name or Protocol number used by proto
Type socket type sock_stream/sock_dgram...
Integer Number of the queue length of the listen listener
Reuse is a Boolean value used to avoid bind time gap during restart.
Timeout value integer
Multihomed is used to connect to a Boolean value of multiple IP addresses.
**************************************** *******************************
The peeraddr parameter is basically the same as the peerhost (Remote Host Name) parameter.
The value format is also the same. In addition to the standard format, you can add the ':' sign and the port or service.
In this example, the parameter peerport (remote host port or service) is invalid.
The parameter peerport (remote host port or service) can be in the format of port or service
Service name, which can be a combination, for example, "Telnet (23)"; When peeraddr (remote host address) or
The value format of peerhost (Remote Host Name) specifies the port.
Invalid.
Parameters: localaddr (local host address), localhost (local host name), localport (local host name)
The relationship between the local host port or service) and the calling method and the preceding three parameters: peeraddr (far
The remote host address), peerhost (remote host name), and peerport (remote host port or service) are equivalent.
Another case is that if only localport (local host port or service) is defined
If you define localaddr (local host address) or localhost (local host name), then IO: socket will
By default, the address value of the local machine is the inaddr_any wildcard, that is, the location of the local host is not defined.
The address value is defined to allow all interfaces.
The value of proto can be expressed in two ways. One is a direct string representation.
Type, such:
PROTO => "TCP"
Indicates that the protocol type is TCP. The second way is to directly use the Protocol Number, EGP---8,
HMP---20, ICMP---1, raw--255, RDP---27, RVD---66, TCP---6,
UDP---17, XNS-IDP---22, other --- 22, all---0; you can also use getprotobyname
The value obtained from the parameter call of the Function Plus protocol, for example:
PROTO => getprotobyname ('tcp ')
This form also indicates that the protocol type is TCP. We recommend that you use the first method for convenience.
The value of type (socket type) is usually sock_stream (stream socket), sock_dgram (Data
Reporting socket), sock_raw (original socket), and so on. Needless to say, we all know that TCP uses stream socket.
UDP uses a datagram socket and an original socket to construct an IP packet.
If neither the preceding parameter proto (protocol type) nor type (socket type) is defined,
IO: Socket: iNet, the upper and lower 'wen' sections in the program are used to guess and estimate their values.
The default value is 'tcp '.
The value of the listen parameter (length of the listening Queue) is an integer. It indicates an acceptable connection master.
Number of hosts. If you want to construct a server, the listen step is indispensable.
To call reuse (set so_reuseaddr before binding), you can avoid termination or restart of the server.
The time of the period.
Timeout (timeout value) is calculated in seconds. It is used for the connect and accept steps in the connection,
The purpose of a call is to limit the connection suspension time when the remote host cannot be reached.
The value of multihomed (used to connect multiple IP addresses) is a Boolean value. When the value is true, if
If the host to be connected has multiple IP addresses, the new method of the Local Machine calls gethostbyname () to raise its
All IP addresses until they are successfully called.
From the list above, we can see that I/O: socket and the socket API of traditional C library are called
What's the difference:
1) The control scope is different. The interfaces provided by library C can only control domains and sockets when generating socket handles.
Types and protocols. IO: The statement for creating the socket interface (calling the new method) is almost
All parameters of the socket can be determined.
2) the 'Protocol 'Used for the call is defined in different parts. IO: The SOCKET interface calls the parameters in the new method.
The value of 'proto' can be directly defined as 'tcp ', which is easier than the socket definition of traditional C library.
3) IO: when defining socket, you can directly define the local host address, local port and remote host address,
The remote port is in a socket. In this case, the server does not need to call accept,
In the I/O read/write part, you can directly perform read/write operations on this socket without defining remote customers.
End Socket.
-----------------------------------------------------------------------------
Accept () method:
Remote connection socket object variable = server socket object variable-> Accept ();
Instance:
$ Remote_sock = $ sock-> Accept ();
Explanation:
The Calling environment of this method is the same as that of the socket library in traditional C. It is used for server waiting monitoring.
Listening process. No parameter. The returned value is the socket object variable for remote connection. This method is also called
The process of generating a socket is nothing more than a remote connection socket. It uses an object variable
Method, which has the same attributes and methods as local socket variables.
The accept () method also provides another method to call double return values in the IO: Socket package:
(Remote connection socket object variable, remote host compression address variable) = server object variable-> Accept ();
Instance:
($ Remote_sock, $ remote_addr) = $ sock-> Accept ();
Explanation:
The call method for a returned value is basically the same as that for an upstairs one, but only one variable is added to the returned value,
There are multiple variables in the returned value ------ the remote host compresses the address variable.
-----------------------------------------------------------------------------
BIND () method:
Return Value variable = server socket object variable-> BIND (local port number, local host network address );
Instance:
$ Result = $ sock-> BIND (80, '2017. 0.0.1 ');
Explanation:
The bind method is used to bind the host address and port to the server. It uses both of the following parameters:
Uncompressed value. The first is the port, and the second is the network adapter interface address of the host (you can use the default
The reserved word inaddr_any, which includes all network adapter interface addresses of the host, called
When it is used, it will enumerate all network adapter interface addresses in a poor way until they are found); Back
The return value is a Boolean value to check whether the call is successful.
-----------------------------------------------------------------------------
Connect () method:
Return Value variable = socket object variable-> connect (compressed address variable );
Instance:
$ Result = $ sock-> connect ($ pack_addr );
Explanation:
Generally used for TCP connections (also used for UDP, but not frequently used). A call will send a connection to a remote host.
Request. The parameter 'Compression address variable 'is a value in the sockaddr_in format and the return value is a Boolean value. If
The parameter 'peeraddr 'or 'peerhost ',
'Peerport'. Otherwise, a program logic error occurs.
The CONNECT () method also has a two-parameter call method, which is easier to use:
Return Value variable = socket object variable-> connect (remote port number, remote host address );
Instance:
$ Result = $ sock-> connect ($ remote_port, $ remote_host );
Explanation:
The purpose of calling is the same as that of calling a single parameter upstairs. The first parameter is
Host port (equal to the parameter 'peerport' of the new method). The second parameter is the host to be connected.
Address (equal to the parameter 'peeraddr 'or 'peerhost' of the new method). The return value is a Boolean value.
-----------------------------------------------------------------------------
Listen () method:
Return Value variable = socket object variable-> listen (maximum length value of the Request queue );
Instance:
$ Result = $ sock-> listen (20 );
Explanation:
An indispensable method for the TCP server. Single parameter. The parameter is used by the server to accept the remote Request queue.
Returns a Boolean value. Calling this method is equivalent to creating IO: Socket: iNet
The value of the 'listen' parameter is defined when the object is called. Therefore, if the 'listen' parameter is defined in the new method
If this method is used, a logic error such as 'program definition conflicted 'may occur.
-----------------------------------------------------------------------------
Shutdown () method:
Return Value variable = socket object variable-> Shutdown (control parameter );
Instance:
$ Result = $ sock-> Shutdown (2 );
Explanation:
This method is another method except close to close the socket object. Single parameter, parameter value
Defines the additional parameters. The following lists the additional parameters for this method:
**************************************** *******************************
Parameter value description
+ +
0. Disable read operations on the object socket.
1. Disable write operations on the object socket
2. Disable all operations on the object socket
**************************************** *******************************
The return value is a Boolean value.
-----------------------------------------------------------------------------
Send () method:
Successfully sent data value variable = socket object variable-> send (send data, flag value, target address value );
Instance:
$ Succ_bytes = $ sock-> send ('hihi/N', 0, $ pack_host );
Explanation:
The send method is a special method used to send data to the socket. The call format and parameter format are also
This is the same as the send function in the socket API of library C. The first parameter is the data to be sent; the second parameter
The parameter is a flag value. If this parameter is not added, the default value is 0. The third parameter is usually used only for UDP connections.
Address value in sockaddr_in format (NOTE: When the third parameter needs to be written, the second parameter
The returned value is the size of successfully sent data (in bytes ).
-----------------------------------------------------------------------------
Recv () method:
Compression remote address = socket object variable-> Recv (receiving data variable, receiving data value length, flag value );
Instance:
$ Remote_pack_address = $ sock-> Recv ($ MEM, 100,0 );
Explanation:
The Recv method is a special method used to receive data for the socket. The call format and parameter format are also
The socket API of library C is basically the same. The first parameter is the variable value that stores the received data; the second parameter
The parameter is the length value of the received data. The third parameter is the flag value. The default value is 0.
The default value is 0 ).
-----------------------------------------------------------------------------
========================================================== ========================================================== ======
I/O: the common methods of the socket interface have been introduced. However, you need to pay attention to the following issues:
As a simple client, you only need to call the new method before performing basic I/O operations (using print and Getline ).
And then call the close method to end the session. The entire socket session is complete.