I want to create a TCP chat server using Ruby. When I attempt to do this in the Ubuntu terminal (WSL), I encounter the following problem: when I execute the command telnet localhost 2000, I get the following output: Trying ::1... Trying 127.0.0.1... Trying 127.0.0.1... telnet: Unable to connect to remote host: Connection refused enter image description here
Ruby Code :
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
What I Tried: I attempted to create a TCP chat server using Ruby in the Ubuntu terminal (WSL).
What I Expected to Happen: I expected the server to successfully start and listen on the specified port (in this case, port 2000). When I used the telnet localhost 2000 command, I anticipated a successful connection to the server.
What Actually Resulted: Instead, the telnet command output showed the following: Trying ::1... Trying 127.0.0.1... Trying 127.0.0.1... telnet: Unable to connect to remote host: Connection refused
If the host is returning connection refused, that is because there is no process listening on the server at that port.
In this case, since you are on the localhost -> run netstat
from that system and see if you can confirm it is listening.
Pls. keep keep in mind, this the error displayed is from a specific network interaction. In some cases, something "in-between" (like a local software firewall or system config), could also intercept the TELNET connection, and decide to reject the connection even if there is a process happily listening on the port you requested.