尝试在 Ubuntu (WSL) 上使用 Ruby 设置 TCP 聊天服务器时,如何解决“连接被拒绝”错误?

问题描述 投票:0回答:1

我想使用 Ruby 创建一个 TCP 聊天服务器。当我尝试在 Ubuntu 终端 (WSL) 中执行此操作时,遇到以下问题:当我执行命令 telnet localhost 2000 时,得到以下输出: 尝试::1... 正在尝试 127.0.0.1... 正在尝试 127.0.0.1... telnet:无法连接到远程主机:连接被拒绝 enter image description here

Ruby 代码:

require 'socket'


server = TCPServer.new(2000) # Server bound to port 2000

while true 
    connection = server.accept   # Wait for a client to connect

    connect.print" Enter ur Username: "
    name = connection.gets.chomp # chomps removes the new line character
    connection.puts "Welcome #{name} to the chat room"
    connection.close             # Disconnect from the client


end 

我尝试过的: 我尝试在 Ubuntu 终端 (WSL) 中使用 Ruby 创建 TCP 聊天服务器。

我期望发生的事情: 我希望服务器成功启动并侦听指定端口(在本例中为端口 2000)。当我使用 telnet localhost 2000 命令时,我预计会成功连接到服务器。

实际结果: 相反,telnet 命令输出显示以下内容: 正在尝试::1... 正在尝试 127.0.0.1... 正在尝试 127.0.0.1... telnet:无法连接到远程主机:连接被拒绝

ruby ubuntu networking windows-subsystem-for-linux telnet
1个回答
0
投票

如果主机返回连接被拒绝,那是因为该端口上没有进程在服务器上侦听。

如果您有权访问服务器,请从该系统运行

netstat
并查看是否可以确认它正在侦听。

请。请记住,显示的错误来自特定的网络交互。在某些情况下,中间的某些东西(网络设备或本地软件防火墙)也可能拦截 TELNET 连接,并决定拒绝连接,即使有一个进程正在愉快地侦听您请求的端口。

© www.soinside.com 2019 - 2024. All rights reserved.