+ -
当前位置:首页 → 问答吧 → perl 本机调试,提示connect() 拒绝连接

perl 本机调试,提示connect() 拒绝连接

时间:2011-12-04

来源:互联网

各位好,现在学习perl编程,在本机调试的时候,出现connect() 拒绝连接,应该怎么处理呀?
如果是后面跟一个网址的话,就会提示:
connect() failed:连接超时 at ./tcp.pl line 14.

Perl code

#! /usr/bin/perl

use strict;
use warnings;
use Socket;
use IO::Handle;
my ($bytes_out,$bytes_in) = (0,0);
my $host = shift() || 'localhost';
my $port = shift() || getservbyname('echo','tcp');
my $protocol = getprotobyname('tcp');
$host = inet_aton($host) or die "$host:unknow host.";
socket(SOCK,AF_INET,SOCK_STREAM,$protocol) or die "socket() failed:$!";
my $dest_addr = sockaddr_in($port,$host);
connect(SOCK,$dest_addr) or die "connect() failed:$!";
SOCK->autoflush(1);
while(my $msg_out = <>)
{
    print SOCK $msg_out;
    my $msg_in = <SOCK>;
    print SOCK $msg_in;
    $bytes_out += length($msg_out);
    $bytes_in += length($msg_in);
}
close SOCK;
print STDERR "bytes_send = $bytes_out,bytes_received = $bytes_in\n";


作者: gallanthunter   发布时间: 2011-12-04

服务器可能没有侦听echo端口。

作者: wangmu7206   发布时间: 2011-12-04