gpt4 book ai didi

perl - 在perl套接字编程中如何从客户端发送数据并从服务器接收数据

转载 作者:行者123 更新时间:2023-12-01 18:14:20 24 4
gpt4 key购买 nike

我正在Perl中使用Socket模块进行套接字编程。现在我想从客户端发送一个数据并从服务器端接收它。我将如何实现这一目标。请帮忙。

下面给出了我使用的代码

服务器

#!/usr/bin/perl -w
# Filename : server.pl

use strict;
use IO::Socket;
use Socket;
use Sys::Hostname;
use constant BUFSIZE => 1024;

# use port 7890 as default
my $port = shift || 7890;
my $proto = getprotobyname('tcp');
my $server = "localhost"; # Host IP running the server

# create a socket, make it reusable
socket(SOCKET, PF_INET, SOCK_STREAM, $proto)
or die "Can't open socket $!\n";
setsockopt(SOCKET, SOL_SOCKET, SO_REUSEADDR, 1)
or die "Can't set socket option to SO_REUSEADDR $!\n";

# bind to a port, then listen
bind( SOCKET, pack_sockaddr_in($port, inet_aton($server)))
or die "Can't bind to port $port! \n";

listen(SOCKET, 5) or die "listen: $!";
print "SERVER started on port $port\n";

# accepting a connection
my $client_addr;
my $val = 100;

while ($client_addr = accept(NEW_SOCKET, SOCKET)) {
# send them a message, close connection
my $name = gethostbyaddr($client_addr, AF_INET );
print NEW_SOCKET "Smile from the server";
print NEW_SOCKET $val;
print "Connection recieved from $name\n";
close NEW_SOCKET;
}

客户端

#!/usr/bin/perl -w
# Filename : client.pl

use strict;
use IO::Socket;
use Socket;
use Sys::Hostname;
use constant BUFSIZE => 1024;

# initialize host and port
my $host = shift || 'localhost';
my $port = shift || 7890;
my $server = "localhost"; # Host IP running the server

# create the socket, connect to the port
socket(SOCKET,PF_INET,SOCK_STREAM,(getprotobyname('tcp'))[2])
or die "Can't create a socket $!\n";
connect( SOCKET, pack_sockaddr_in($port, inet_aton($server)))
or die "Can't connect to port $port! \n";

my $line;
my $req = 1000;
while ($line = <SOCKET>) {
print "$line\n";
}
close SOCKET or die "close: $!";

最佳答案

这是一个基本示例。下面的代码添加到您所拥有的内容中,但请注意模块 IO::Socket::IP或核心IO::Socket::INET使其比您使用的较低级别的调用更容易。

对代码的唯一更改(除了下面显示的)是从 SOCKET 到词法 my $socket,并且现有声明被移动到 while 内 条件。

每个服务器-客户端系统都需要一个协议(protocol),即消息交换方式的安排。在这里,一旦客户端连接,服务器就会发送一条消息,然后它们交换单个打印。

server.pl

# ... code from the question, with $socket instead of SOCKET

use IO::Handle; # for autoflush

while (my $client_addr = accept(my $new_socket, $socket))
{
$new_socket->autoflush;
my $name = gethostbyaddr($client_addr, AF_INET );
print "Connection received from $name\n";

print $new_socket "Hello from the server\n";

while (my $recd = <$new_socket>) {
chomp $recd;
print "Got from client: $recd\n";

print $new_socket "Response from server to |$recd|\n";
}
close $new_socket;
}

您可以使用select使句柄热(自动刷新),而不是加载IO::Handle .

client.pl

我添加了一个计数器 $cnt 来模拟一些导致条件中断的处理。

# ... same as in question, except for $socket instead of SOCKET

use IO::Handle;
$socket->autoflush;

my $cnt = 0;
while (my $line = <$socket>) {
chomp $line;
print "Got from server: $line\n";
last if ++$cnt > 3; # made up condition to quit

print $socket "Hello from client ($cnt)\n";
}
close $socket or die "close: $!";

这符合预期。客户端在三个消息后退出,服务器保持等待。如果您确实希望只写一次,则简单的 print 和 read 替换 while 循环。

交换可以更加复杂,请参阅最后链接的 perlipc 中的示例。

一些评论

  • 使用提到的模块使这变得更加容易

  • 刷新中的任何故障都可能导致死锁,其中一方写入并等待读取,而另一方没有获取消息仍然位于管道中,因此, ,等待阅读

  • 检查一切。为了简洁起见,省略了所有检查

  • use warnings;-w 开关更好。请参阅discussion on warnings page

这只是为了回答如何在它们之间实现通信的问题。。一个很好的学习资源是 perlipc ,其中还有 full example 。相关模块的文档也提供了大量信息。

关于perl - 在perl套接字编程中如何从客户端发送数据并从服务器接收数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40915269/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com