我创建了一个Perl脚本,它在我的服务器上打开一个新的套接字。当我用telnet连接到套接字并写(和接收)一些东西时,连接关闭。
#!/usr/bin/perl
use strict;
use warnings;
use IO::Socket;
use IO::Socket::INET;
$| = 1;
my $sock = IO::Socket::INET->new(Listen => 5,
LocalAddr => 'localhost',
LocalPort => 9000,
Reuse => 1,
Proto => 'tcp');
die "Socket not created $!\n" unless $sock;
print "Server waiting for connections\n";
while(1)
{
# waiting for a new client connection
my $client_socket = $sock->accept();
# get information about a newly connected client
my $client_address = $client_socket->peerhost();
my $client_port = $client_socket->peerport();
print "Connection from $client_address:$client_port\n";
# read up to 1024 characters from the connected client
my $data = "";
$client_socket->recv($data, 1024);
chomp($data);
print "Data: $data\n";
# write response data to the connected client
my $dataok = "OK";
$client_socket->send("$dataok\n");
$client_socket->send("$data\n");
if($data == 500){
close($sock);
exit();
}
elsif($data eq "Close\r") {
close($sock);
exit();
}
}
字符串
我的telnet会话:
telnet localhost 9000
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
e //(Sent)
e //(Received)
Connection closed by foreign host.
型
为什么我的脚本关闭了连接?提前感谢
1条答案
按热度按时间5w9g7ksd1#
我在代码中添加了一个循环,它工作了!感谢@simbabque和@SteffenUllrich。
字符串