perl 聊天服务器出错 [英] Error in perl chat server

查看:60
本文介绍了perl 聊天服务器出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Perl 中的套接字创建聊天服务器.但是,当我运行 Server 程序时出现错误:

I'm trying to create a chat server using sockets in Perl. However, when I run the Server program I get the error:

"ERROR:(9)(Bad file descriptor)(6)(+The handle is invalid) at Server.pl line 21."

"ERROR:(9)(Bad file descriptor)(6)(+The handle is invalid) at Server.pl line 21."

当我运行客户端程序时出现错误:

and when I run the client program I get the error:

"无法创建套接字:无法建立连接,因为目标机器主动拒绝."

"Cannot create the socket: No connection could be made because the target machine actively refused it."

这是服务器程序:

#!usr/bin/perl
#server.pl

use IO::Socket;
$| = 1;

print "Server Program\n";
my $lp = 12000;

my $server_socket, $new_client, $addr, $port;

$server_socket = new IO::Socket::INET (
LocalHost => '127.0.0.1',
LocalPort => $lp,
Proto => 'tcp',
Reuse => 1) or die "Cannot create the socket: $!\n";

print "Server started at port $lp \n";

while (1) {
    $new_client = $server_socket->accept() or die sprintf "ERROR:(%d)(%s)(%d)(+%s)", $!,$!,$^E,$^E;

$addr = $new_client->peerhost();
$port = $new_client->peerport();
print "Connected to client at $addr at port $port ";
while(<$new_client>) {
    print "Following is the text entered by client: \n";
    print "$_";
}
print "Client now disconnecting..\n";
close $new_client;
}

$server_socker->close();

这里是客户:

#!usr/bin/perl
#client.pl

use IO::Socket;
$| = 1;

print "Client Program\n";

my $lp = 12000;

my $client_socket = new IO::Socket::INET (
PeerHost => '127.0.0.1',
PeerPort => $lp,
Proto => 'tcp',
Reuse => 1) or die "Cannot create the socket: $!\n";

print "Server connected at port $lp \n";
print "Enter the text to sent to the server: \n";
$user_input = <>;
chomp $user_input;
print $plient_socket;
$client_socket->send($user_input);
$client_socket->close();

我是新手,我不知道我哪里出错了.有人可以帮忙吗?

I am new to this and I'm not getting where I'm going wrong. Could anybody help?

推荐答案

您试图接受来自未侦听套接字的连接.添加

You trying to accept a connection from a socket that's not listening. Add

Listen => SOMAXCONN,

<小时>

现在是关于您的代码的离题评论:


And now for off-topic comments about your code:

  • 总是使用 use strict;使用警告;.它将突出显示您的代码的其他一些问题.

  • Always use use strict; use warnings;. It will highlight some other problems with your code.

shebang 线上的相对路径没有任何意义.您缺少 /.

It doesn't make any sense to relative paths on the shebang line. You're missing a /.

在样式方面,在使用变量之前声明变量被认为是不好的形式.声明变量的全部意义在于限制它们的作用域,因此在程序顶部声明它们违背了目的.

On the style front, it's considered bad form to declare variables ahead of where they are used. The whole point of declaring variables is to limit their scope, so declaring them at the top of the program defies the purpose.

LocalHost =>'127.0.0.1'(最好写成 LocalHost => INADDR_LOOPBACK)使您只能接收来自 127.0.0.1 的连接.这可能很有用,但我不知道你是不是故意这样做的.默认值 INADDR_ANY 允许来自任何接口的连接.

LocalHost => '127.0.0.1' (better written as LocalHost => INADDR_LOOPBACK) makes it so you can only receive connections from 127.0.0.1. That can be useful, but I don't know if you did that intentionally. The default, INADDR_ANY, allows connections from any interface.

这篇关于perl 聊天服务器出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆