以下是失败的示例:
#!/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确实有用。
答案 0 :(得分:7)
问题是包模板Sn4x8
出错 - 并且不应该首先使用。像Socket中记载的pack_sockaddr_in($port, inet_aton($server))
之类的东西更有可能发挥作用。
但理想情况下,根本不会使用低级Socket代码。这是使用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: $!";
答案 1 :(得分:1)
对我来说就像这样:
connect( SOCKET, pack( 'S n a4 x8', AF_INET, $port, $server))
or die "Can't connect to port $port! \n";
我认为您的原始脚本AF_INET
是unicode或其他内容。如果您删除A
并再次写入则可以。
答案 2 :(得分:0)
这是使包装工作的路线:
connect( SOCKET, pack( 'SnC4x8', AF_INET, $port, split /\./,$server ))
or die "Can't connect to port $port! \n";