Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Perl的客户机-服务器程序中发送数组?_Perl_Sockets - Fatal编程技术网

如何在Perl的客户机-服务器程序中发送数组?

如何在Perl的客户机-服务器程序中发送数组?,perl,sockets,Perl,Sockets,我有一个客户机-服务器Perl程序。我想将存储在数组中的消息发送到服务器 服务器代码: use IO::Socket::INET; # Creating a a new socket $socket=new IO::Socket::INET->new(LocalPort=>5000,Proto=>'udp'); print "\nUDPServer Waiting for client on port 5000"; while(1) { $socket->r

我有一个客户机-服务器Perl程序。我想将存储在数组中的消息发送到服务器

服务器代码:

use IO::Socket::INET;

# Creating a a new socket
$socket=new IO::Socket::INET->new(LocalPort=>5000,Proto=>'udp');

print "\nUDPServer Waiting for client on port 5000";

while(1)
{
    $socket->recv($recieved_data,1024);
    $peer_address = $socket->peerhost();
    $peer_port = $socket->peerport();
    print "\n($peer_address , $peer_port) said : $recieved_data";
}
客户端代码::

use IO::Socket::INET;

# Create a new socket
$socket=new IO::Socket::INET->new(PeerAddr=>'127.0.0.1',PeerPort=>5000,Proto=>'udp');

@message_array = ("message", 120, "sample");
$socket->send(@message_array);
在服务器端,我更改为

$socket->recv(@recieved_data,1024);
但是我遇到了这样的错误

UDPServer Waiting for client on port 5000usage: $sock->recv(BUF, LEN [, FLAGS]) at udp_server.pl line 17

如何发送阵列并将其打印或显示在服务器端。

您不能直接发送阵列。如果您有简单的字符串,请使用
pack/unpack
序列化您的数组,或者使用
Storable
来获得更通用的解决方案。

IO::Socket::INET中的send
/
recv
仅继承自IO::Socket.:

IO::Socket::INET提供了一个对象接口,用于在AF_INET域中创建和使用套接字。它基于IO::Socket接口构建,并继承IO::Socket定义的所有方法

围绕相同名称的内置Perl函数

正如我们看到的
send
()

这些函数只显式地接受标量消息,因此您需要以某种方式将数组转换为标量


您可以使用自己的序列化协议(例如,如果保证数组的成员在字符串中没有逗号,则使用逗号连接字符串);或使用包装;或者使用
Data::Dumper->Dump
Storable
或您喜欢的任何其他序列化包。如果您想比较不同的方法,可以研究“perl序列化”。

您必须序列化数据。通过线路发送序列化的数据块,然后取消序列化

这方面有很多选择。印尼的Perl贩子最近做出了

Perl随附,虽然您必须注意模块没有以某种方式更改,以便一个版本的序列化可以与另一个版本一起工作,但它可以正常工作

有些数据格式,如或,可能更好,因为它们不依赖于模块版本。两者都可以处理复杂的数据结构,尽管YAML可以处理Perl对象,JSON不能(尽管可以)。每个选项都有不同的权衡

send SOCKET,MSG,FLAGS

    Sends a message on a socket. 
    Attempts to send the **scalar** MSG to the SOCKET filehandle.