gpt4 book ai didi

perl - 简单的 perl 程序无法执行

转载 作者:行者123 更新时间:2023-12-02 07:07:06 24 4
gpt4 key购买 nike

这是一个失败的例子:

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

use strict;
use Socket;

# initialize host and port
my $host = shift || 'localhost';
my $port = shift || 55555;
my $server = "10.126.142.22";

# 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( 'Sn4x8', AF_INET, $port, $server ))
or die "Can't connect to port $port! \n";

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

错误:

Argument "10.126.142.22" isn't numeric in pack at D:\send.pl line 16.
Can't connect to port 55555!

我正在使用这个版本的 Perl:

This is perl, v5.10.1 built for MSWin32-x86-multi-thread
(with 2 registered patches, see perl -V for more detail)

Copyright 1987-2009, Larry Wall

Binary build 1006 [291086] provided by ActiveState http://www.ActiveState.com
Built Aug 24 2009 13:48:26

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl". If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.

当我在服务器端运行 netcat 命令时。 Telnet 确实有效。

最佳答案

问题是包模板 Sn4x8 有误——一开始就不应该使用。 Socket 中记录的类似 pack_sockaddr_in($port, inet_aton($server)) 的东西更有可能工作。

但理想情况下,您根本不会使用低级套接字代码。这是使用 IO::Socket 完成的更好的代码,这也是过去15年perl的核心部分:

use strict;
use IO::Socket::INET;

my $host = shift || 'localhost'; # What is this here for? It's not used
my $port = shift || 55555;
my $server = "10.126.142.22";

my $socket = IO::Socket::INET->new(
PeerAddr => $server,
PeerPort => $port,
Proto => 'tcp',
) or die "Can't connect to $server: $@";

while (my $line = <$socket>) {
print $line; # No need to add \n, it will already have one
}

close $socket or die "Close: $!";

关于perl - 简单的 perl 程序无法执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9929138/

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