1
我在perl中编写了一个多客户套接字脚本。我可以使用fork与多个客户端建立连接。但是,建立新的连接后,我只能发送和接收一次。我无法进一步读取或写入客户端套接字,以进行扩展对话。Multiclient全双工Perl套接字
目的:客户端获取连接,将其ID发送给服务器
后。在接收到ID后,服务器将使用其ID来获取客户端的文件并逐页发送给客户端。由于客户端只能接受有限的字节数据,因此我们无法通过单个套接字发送它。所以我需要发送一个1024字节的数据。客户必须承认并再次发送下一页。像这样,我必须同时为每个客户端同时建立套接字连接,直到他们都接收到整个文件。
代码示例:
use strict;
use warnings;
use IO::Socket::INET;
# flush after every write
$| = 1;
my $data;
my ($server_socket, $client_socket);
my ($peer_address, $peer_port);
$server_socket = new IO::Socket::INET(
LocalHost => '192.168.0.7',
LocalPort => '1008',
Proto => 'tcp',
Listen => 5,
Reuse => 1
) or die "ERROR in Socket Creation : $!\n";
print "SERVER Waiting for client connection on port 1008";
#$SIG{CHLD}=sub {wait()}; # Signal waiting for the Child process created
while ($client_socket = $server_socket->accept()) {
my $pid = fork(); # Creating child as soon client creates a connection
if ($pid == 0) # Checking the child process
{
my $peer_address = $client_socket->peerhost();
my $peer_port = $client_socket->peerport();
print "Accepted New Client Connection From : $peer_address, $peer_port\n";
$client_socket->recv($data, 1024);
print $data;
$client_socket->send("FileLine1");
#Get the acknowledgement
$client_socket->recv($data, 1024);
$client_socket->send("FileLine2");
#Get the end acknowledgement
$client_socket->recv($data, 1024);
exit(0); # Exiting child process
}
}
我的问题是:我们可以依靠的perl这样做呢?或者我们是否需要去其他平台,如Java等
我真的需要一些使用Net :: Server模块的'使持久性多客户套接字连接'的例子。我找不到一个 – gzix 2014-09-23 13:25:05