当尝试使用Ruby on Ubuntu(WSL)设置TCP聊天服务器时,如何解决“Connection refused”错误?

m3eecexj  于 5个月前  发布在  Ruby
关注(0)|答案(1)|浏览(82)

我想使用Ruby创建一个TCP聊天服务器。当我尝试在Ubuntu终端(WSL)中执行此操作时,我遇到以下问题:当我执行命令telnet localhost 2000时,我得到以下输出:Trying::1... Trying 127.0.0.1... Trying 127.0.0.1... telnet:Unable to connect to remote host:Connection refusedenter 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命令输出显示以下内容:Trying::1... Trying 127.0.0.1... Trying 127.0.0.1... telnet:Unable to connect to remote host:Connection refused

rseugnpd

rseugnpd1#

如果主机返回拒绝连接,那是因为在该端口的服务器上没有监听进程。
在这种情况下,由于您在本地主机上->从该系统运行netstat,看看您是否可以确认它正在侦听。
请记住,这个错误显示是从一个特定的网络交互.在某些情况下,“中间”的东西(如本地软件防火墙或系统配置),也可以拦截TELNET连接,并决定拒绝连接,即使有一个进程愉快地侦听您请求的端口.

相关问题