How to do network programming in Perl?
In Perl, network programming is commonly done using the Socket module. Here is a simple example:
use strict;
use warnings;
use IO::Socket::INET;
my $host = 'localhost';
my $port = 12345;
# 创建一个socket
my $socket = new IO::Socket::INET (
PeerAddr => $host,
PeerPort => $port,
Proto => 'tcp',
) or die "无法连接到服务器: $!\n";
print "连接成功\n";
# 发送数据到服务器
my $data = "Hello, World!";
$socket->send($data);
# 接收来自服务器的数据
my $response;
$socket->recv($response, 1024);
print "来自服务器的响应: $response\n";
# 关闭socket
$socket->close();
The code above connects to the local host on port 12345, sends the string “Hello, World!” to the server, receives the response from the server, prints it out, and then closes the socket connection. This is just a simple example, actual network programming can be more complex and detailed.