Basic IO of Perl Socket

xiaoxiao2021-03-06  86

Writer: Demonalex

Email: Demonalex_at_dark2s.org

Use the Perl Socket API to first need to load the Socket module.

Use socket;

============================================================================================================================================================================================================= ====================

Socket (file handle, AF_INET, data type, protocol type);

# 建立 套字

The file handle can be found casually.

AF_INET is the domain type or writable as a PF_INET.

Data types, usually there are two types: SOCK_STREAM, SOCK_DGRAM.

Type of protocols, can be replaced by protocol numbers, EGP --- 8, HMP --- 20, ICMP --- 1,

RAW --- 255, RDP --- 27, RVD --- 66, TCP --- 6, UDP --- 17, XNS-IdP --- 22,

Others --- 22, all --- 0; you can also use the getProtobyname () function as this parameter.

Example: Socket (SOCK, AF_INET, SOCK_STREAM, GetProtobyname ('TCP'));

The shed is SOCK, and the socket is transmitted in TCP mode.

Socket (SS, AF_INET, SOCK_DGRAM, 17);

The shed holder is SS, and the socket is transmitted in UDP mode.

============================================================================================================================================================================================================= =====================

Connect (file handle, sockaddr_in structure);

# Connect the host

-----------------------------------------------

SockAddr_in structure: |

| | |

| $ address = inet_aton (host address);

| $ port = port number; |

| | |

| $ Result = SockAddr_in ($ Port, $ Address); |

| | |

# Above $ Result is SockAddr_in structure, instance: |

| | |

| | |

| | |

| $ address = inet_aton (127.0.0.1); | |

| $ port = 80; |

| | |

| | |

| $ Result = SockAddr_in ($ Port, $ Address); |

| | |

| | |

-----------------------------------------------

Example: Connect (SOCK, $ Result); ======================================== ================================

Bind (socket, sockaddr_in structure);

# 服务 server host address and port (for server)

Example: Bind (SOCK, $ Result);

============================================================================================================================================================================================================= =====================

Listen (socket, wait for the maximum number of queues);

# Set the port status as listening (for server)

Example: Listen (SOCK, 10);

============================================================================================================================================================================================================= =====================

Accept (remote socket, server listener)

# Receive remote data requests, establish a connection (for server)

Example: Accept (session, sock);

============================================================================================================================================================================================================= =====================

Close (file handle);

or

CLOSE file handle;

# Close the socket

Example: Close (SOCK); Close SS;

●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●

Talk about TCP network activity sequence:

============================================================================================================================================================================================================= =====================

CLIENT:

Create a socket () -> Connect to the target host Connect () -> Open Autoflush mode Autoflush () ->

I / O operation -> Close socket close ()

Server:

Establish a socket () -> Binding server address and port bind () -> Settings listen Listen () -> Accept remote socket accept () ->

Open Autoflush mode Autoflush () -> I / O Operation -> Close Set Close ()

●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●

◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎

Perl Socket API Programming Example:

Attachment: I / O operation is temporarily only using print () and <> symbols.

============================================================================================================================================================================================================= =====================

#! usr / bin / perl

# 客户户

Use IO :: Handle; # i :: handle

Use socket; # call socket

$ port = 80; # Connect the 80 port of the remote host

$ host = 'localhost'; # Using a loopback address

$ PACKHOST = inet_aton ($ host); #Compress IP address

$ address = sockaddr_in ($ port, $ packhost); # 压成 sockaddr_in format

Socket (Client, AF_INET, SOCK_STREAM, 6); # Sockets to Client, use TCP protocol

Connect (Client, $ Address); # connection

Client-> Autoflush (1); # 开启 flush mode

$ msg_in = ; #input

Print "IN: $ msg_in / n"; #output

Close Client; # Close the socket

EXIT 1; # 退 程序

============================================================================================================================================================================================================= =====================

#! usr / bin / perl

#Server

Use IO :: Handle; # i :: handle

Use socket; # call socket

$ port = 80; # Binding server host port is 80

$ address = sockaddr_in ($ port, inaddr_any); # 压 成 saaddr_in format, use the INADDR_ANY wildcard

Socket (Server, AF_INET, SOCK_STREAM, GETPROTOBYNAME ('TCP')); # 套接 字 s, use TCP protocol

Bind (Server, $ Address); # 绑

Listen (Server, 10); # Set the listening status

While (1) {# Enter I / O exchange cycle

Next UNSS (Accept (Client, Server);

Client-> Autoflush (1);

Print Client "What do you want? / n";

Close Client;

Close Server; # Close the socket

EXIT 1; # 退 程序

============================================================================================================================================================================================================= =====================

Example Note:

1) TCP's client and Server code has a line 'sock-> autoflush (1);', the following I / O code will enter the cache before normal conditions.

Output, but add the above code to skip this step directly. This code needs to be preloaded in advance IO :: Handle.

2) The value of the INADDR_Any wildcard is already defined in the socket module, which is all the network interfaces adapted to the local network (including the loopback address,

Broadcast address, multicast address, etc.).

◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎ ◎

Head is a bit pain, write here, refer to send () and recv () ........: p

转载请注明原文地址:https://www.9cbs.com/read-98307.html

New Post(0)