You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在WSL Ubuntu环境下使用Ruby搭建TCP聊天服务器时遭遇'Connection refused'错误的排查求助

在WSL Ubuntu环境下使用Ruby搭建TCP聊天服务器时遭遇'Connection refused'错误的排查求助

各位好,我最近在WSL的Ubuntu终端里尝试用Ruby搭建一个TCP聊天服务器,但遇到了连接失败的问题,希望能得到大家的帮助。

当我启动服务器代码后,执行telnet localhost 2000命令,得到的输出如下:

Trying ::1...
Trying 127.0.0.1...
Trying 127.0.0.1...
telnet: Unable to connect to remote host: Connection refused

我的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

我已进行的操作

  • 在WSL Ubuntu终端中运行上述Ruby代码,尝试启动TCP聊天服务器
  • 使用telnet localhost 2000命令尝试连接到服务器

预期结果

我原本期望服务器能成功启动并监听2000端口,执行telnet命令后可以顺利建立连接,看到服务器发送的输入用户名提示。

实际结果

telnet命令返回了连接被拒绝的错误,完全无法和服务器建立连接。

备注:内容来源于stack exchange,提问作者Kernel-rb

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.20 11:18:11