Perl Socket Programming
Socket also known as "socket", the application is usually issued by a "socket" to the network request or response network request, so that the process between hosts or between a computer on can communicate.
This section is all we received Perl language how to use Socket services.
Create server
Use thesocket function to create a socket services.
Use thebind function to bind port.
Uselisten listening port function.
Useaccept function receives client requests.
Create a client
Use thesocket function to create a socket services.
Using theconnect function to connect to the server socket.
The following chart illustrates the flow of communication between the client and the server:
Server socket function
socket function
Perl, we use the socket () function to create a socket, syntax is as follows:
socket( SOCKET, DOMAIN, TYPE, PROTOCOL );
Analytical parameters:
Specifies the protocol set socket DOMAIN created. E.g:
-
AF_INET
indicates IPv4 network protocol -
AF_INET6
for IPv6 -
AF_UNIX
represents local socket (using a file)
-
TYPE socket type can be connection-oriented or connectionless into SOCK_STREAM or SOCK_DGRAM
PROTOCOL should be(getprotobyname ( 'tcp')) [2].Specify the actual transport protocol.
So the socket function call as follows:
use Socket # 定义了 PF_INET 和 SOCK_STREAM socket(SOCKET,PF_INET,SOCK_STREAM,(getprotobyname('tcp'))[2]);
bind () function
Use bind () assign an address to the socket:
bind( SOCKET, ADDRESS );
SOCKET a socket descriptor. ADDRESS is the socket address (TCP / IP) contains three elements:
Address family (TCP / IP, is AF_INET, on your system may be 2)
Port number (such as 21)
Network address (such as 10.12.12.168)
After using the socket () creates a socket, just give the protocol they use and does not assign addresses. Before accepting the other host connection, you must first call bind () assign an address to a socket.
Simple examples are 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 in the bind address failed execution.
By setting the setsockopt () SO_REUSEADDR option to set the port can be reused immediately.
pack_sockaddr_in () function to convert addresses to binary format.
listen () function
When the socket and an address binding, listen () function will start listening for possible connection requests. However, this can only guarantee reliable data stream when using:
listen( SOCKET, QUEUESIZE );
SOCKET: a socket descriptor.
QUEUESIZE: integer is a decision to monitor the size of the queue, when there is a connection request arrives, it will enter the listen queue; when a connection request is accept () to accept, removed from the queue monitor; when the queue is full, new connections request will return an error.
Once the connection is accepted, it returns 0 for success, error return -1.
accept () function
accept () function accepts the request socket connection. If successful in compressed form the network address, otherwise it returns FALSE:
accept( NEW_SOCKET, SOCKET );
SOCKET: a socket descriptor.
ADDRESS: ADDRESS is the socket address (TCP / IP) contains three elements:
Address family (TCP / IP, is AF_INET, on your system may be 2)
Port number (such as 21)
Network address (such as 10.12.12.168)
accept () which is usually applied in an infinite loop:
while(1) { accept( NEW_SOCKET, SOCKT ); ....... }
The above examples can monitor in real time to client requests.
Client functions
connect () function
connect () system call to set up a socket connection parameters file descriptor and the host address.
connect( SOCKET, ADDRESS );
The following creates a socket connection to the server instance:
$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 come to understand all socket applications function through a complete example:
Server server.pl Code:
#!/usr/bin/perl -w # Filename : server.pl use strict; use Socket; # 使用端口 7890 作为默认值 my $port = shift || 7890; my $proto = getprotobyname('tcp'); my $server = "localhost"; # 设置本地地址 # 创建 socket, 端口可重复使用,创建多个连接 socket(SOCKET, PF_INET, SOCK_STREAM, $proto) or die "无法打开 socket $!\n"; setsockopt(SOCKET, SOL_SOCKET, SO_REUSEADDR, 1) or die "无法设置 SO_REUSEADDR $!\n"; # 绑定端口并监听 bind( SOCKET, pack_sockaddr_in($port, inet_aton($server))) or die "无法绑定端口 $port! \n"; listen(SOCKET, 5) or die "listen: $!"; print "访问启动:$port\n"; # 接收请求 my $client_addr; while ($client_addr = accept(NEW_SOCKET, SOCKET)) { # send them a message, close connection my $name = gethostbyaddr($client_addr, AF_INET ); print NEW_SOCKET "我是来自服务端的信息"; print "Connection recieved from $name\n"; close NEW_SOCKET; }
Open a terminal, execute the following code:
$ perl sever.pl 访问启动:7890
Client client.pl Code:
#!/usr/bin/perl -w # Filename : client.pl use strict; use Socket; # 初始化地址与端口 my $host = shift || 'localhost'; my $port = shift || 7890; my $server = "localhost"; # 主机地址 # 创建 socket 并连接 socket(SOCKET,PF_INET,SOCK_STREAM,(getprotobyname('tcp'))[2]) or die "无法创建 socket $!\n"; connect( SOCKET, pack_sockaddr_in($port, inet_aton($server))) or die "无法连接:port $port! \n"; my $line; while ($line = <SOCKET>) { print "$line\n"; } close SOCKET or die "close: $!";
Open another terminal, execute the following code:
$ perl client.pl 我是来自服务端的信息