Perl Socket Programming
Socket, also called a socket endpoint, is how applications usually send requests to or respond to requests from the network, allowing communication between hosts or between processes on a single computer.
In this chapter we introduce how to use Socket services in the Perl language.
Creating a Server
-
Use socket function to create a socket service.
-
Use bind function to bind the port.
-
Use listen function to listen on the port.
-
Use accept function to accept client requests.
Create a client
-
Use socket function to create a socket service.
-
Use connect function to connect to the socket server.
The following diagram illustrates the communication flow between the client and the server:

Server-side socket functions
The socket function
In Perl, we use the socket() function to create a socket. The syntax is as follows:
socket( SOCKET, DOMAIN, TYPE, PROTOCOL );
Parameter analysis:
-
DOMAIN Specifies the protocol family for the created socket. For example:
AF_INETrepresents the IPv4 network protocolAF_INET6represents IPv6AF_UNIXrepresents a local socket (using a file)
-
TYPE Socket types can be divided into SOCK_STREAM or SOCK_DGRAM depending on whether they are connection-oriented or connectionless
-
PROTOCOL should be (getprotobyname('tcp'))[2]. Specifies the actual transport protocol used.
So the socket function is called as follows:
use Socket # 定义了 PF_INET 和 SOCK_STREAM
socket(SOCKET,PF_INET,SOCK_STREAM,(getprotobyname('tcp'))[2]);
The bind() function
Use bind() to assign an address to a socket:
bind( SOCKET, ADDRESS );
SOCKET is a socket descriptor. ADDRESS is the socket address (TCP/IP) and contains three elements:
-
Address family (for TCP/IP, it is AF_INET, which may be 2 on your system)
-
Port number (for example, 21)
-
Network address (for example, 10.12.12.168)
After creating a socket with socket(), only the protocol it uses is assigned, no address is allocated. Before accepting connections from other hosts, you must first call bind() to assign an address to the socket.
A simple example is as follows:
use Socket # 定义了 PF_INET 和 SOCK_STREAM
$port = 12345; # 监听的端口
$server_ip_address = "10.12.12.168";
bind( SOCKET, pack_sockaddr_in($port, inet_aton($server_ip_address)))
or die "无法绑定端口! \n";
or die Executed after binding the address fails.
By setting the setsockopt() option SO_REUSEADDR, the port can be reused immediately.
pack_sockaddr_in() function converts the address into binary format.
The listen() function
After a socket is bound to an address, the listen() function begins listening for possible connection requests. However, this can only be used when reliable data stream is guaranteed:
listen( SOCKET, QUEUESIZE );
SOCKET: a socket descriptor.
QUEUESIZE: an integer that determines the size of the listen queue. When a connection request arrives, it enters this listen queue; when a connection request is accepted by accept(), it is removed from the queue; when the queue is full, new connection requests return an error.
Once a connection is accepted, 0 is returned for success and -1 for error.
The accept() function
The accept() function accepts the requested socket connection. If successful, it returns the packed network address; otherwise it returns FALSE:
accept( NEW_SOCKET, SOCKET );
NEW_SOCKET: a socket descriptor.
SOCKET: a socket descriptor.
accept() is usually used in an infinite loop:
while(1) {
accept( NEW_SOCKET, SOCKT );
.......
}
The above example can listen to client requests in real time.
Client-side functions
The connect() function
The connect() system call establishes a connection for a socket, with parameters of a file descriptor and a host address.
connect( SOCKET, ADDRESS );
The following creates an example that connects to a server socket:
$port = 21; # ftp 端口
$server_ip_address = "10.12.12.168";
connect( SOCKET, pack_sockaddr_in($port, inet_aton($server_ip_address)))
or die "无法绑定端口! \n";
Complete example
Next, we'll look at the use of all socket functions through a complete example:
Server-side server.pl code:
Example
#!/usr/bin/perl -w
Filename : server.pl
use strict
;
use Socket
;
Use port 7890 as the default
my
$port
=
shift
||
7890
;
my
$proto
=
getprotobyname
(
'
tcp
'
)
;
my
$server
=
"
localhost
"
;
Set the local address
Create a socket, with reusable port, allowing multiple connections
socket
(
SOCKET
,
PF_INET
,
SOCK_STREAM
,
$proto
)
or
die
"
Unable to open socket
$!
\n
"
;
setsockopt
(
SOCKET
,
SOL_SOCKET
,
SO_REUSEADDR
,
1
)
or
die
"
Unable to set SO_REUSEADDR
$!
\n
"
;
Bind the port and listen
bind
(
SOCKET
,
pack_sockaddr_in
(
$port
,
inet_aton
(
$server
)
)
)
or
die
"
Unable to bind port
$port
!
\n
"
;
listen
(
SOCKET
,
5
)
or
die
"
listen:
$!
"
;
"
Service started:
$port
\n
"
;
Receive requests
my
$client_addr
;
while
(
$client_addr
=
accept
(
NEW_SOCKET
,
SOCKET
)
)
{
send them a message, close connection
my
$name
=
gethostbyaddr
(
$client_addr
,
AF_INET
)
;
NEW_SOCKET
"
I am a message from the server
"
;
"
Connection recieved from
$name
\n
"
;
close
NEW_SOCKET
;
}
Open a terminal and execute the following code:
$ perl sever.pl
访问启动:7890
Client-side client.pl code:
Example
#!/usr/bin/perl -w
Filename : client.pl
use strict
;
use Socket
;
Initialize the address and port
my
$host
=
shift
||
'
localhost
'
;
my
$port
=
shift
||
7890
;
my
$server
=
"
localhost
"
;
Host address
Create the socket and connect
socket
(
SOCKET
,
PF_INET
,
SOCK_STREAM
,
(
getprotobyname
(
'
tcp
'
)
)
[
2
]
)
or
die
"
Unable to create socket
$!
\n
"
;
connect
(
SOCKET
,
pack_sockaddr_in
(
$port
,
inet_aton
(
$server
)
)
)
or
die
"
Unable to connect: port
$port
!
\n
"
;
my
$line
;
while
(
$line
= <
SOCKET
)
{
"
$line
\n
"
;
}
close
SOCKET
or
die
"
close:
$!
"
;
Open another terminal and execute the following code:
$ perl client.pl
我是来自服务端的信息