Ruby TCPServer跨IP访问咨询:Mac High Sierra替代Telnet方案
Hey there! Let's figure out how to connect to your partner's Ruby TCP server from your Mac High Sierra, since telnet isn't available by default. Here's a step-by-step breakdown:
First, you need your partner's laptop's local network IP (not 127.0.0.1—that only works for connections on the same device). Here's how to find it:
- On Mac/Linux: Open Terminal and run
ifconfig(orip addron newer Linux distros). Look for theinetline under the active network interface (usuallyen0for Wi-Fi) — it’ll look like192.168.1.105. - On Windows: Open Command Prompt and run
ipconfig, then find the "IPv4 Address" under your active network adapter.
Since telnet isn’t enabled out of the box, here are three reliable options to connect to the server:
Option 1: Use nc (Netcat) — Built into Mac
Mac comes pre-installed with Netcat (nc), a versatile network tool. Open your Terminal and run:
nc [partner's-ip] 2345
For example, if their IP is 192.168.1.105, run nc 192.168.1.105 2345. You’ll immediately see the server’s "What do you say?" prompt and can start sending messages until you type quit.
Option 2: Install Telnet via Homebrew
If you prefer to use telnet, you can install it easily with Homebrew:
- First install Homebrew (if you don’t have it already):
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" - Then install telnet:
brew install telnet
Once installed, connect with:
telnet [partner's-ip] 2345
Option 3: Write a Simple Ruby Client Script
Since you’re already working with Ruby, create a custom client script. Save this as tcp_client.rb:
require 'socket' # Replace with your partner's actual IP address server_ip = "192.168.1.105" socket = TCPSocket.new(server_ip, 2345) # Receive the initial message from the server puts socket.gets.chomp loop do print "You: " input = gets.chomp socket.puts input response = socket.gets.chomp puts "Server: #{response}" break if input == "quit" end socket.close
Run it with ruby tcp_client.rb, and you’ll have a fully functional client to interact with the server.
- Same Local Network: Ensure both laptops are connected to the same Wi-Fi or wired network. If they’re on different networks, you’ll need port forwarding on the server’s router (stick to the same network for simplicity first).
- Firewall Settings: On your partner’s laptop, make sure the firewall isn’t blocking port 2345. For Mac, go to System Preferences > Security & Privacy > Firewall > Firewall Options, then add an exception for Terminal or allow incoming connections on port 2345.
- Verify Server is Listening: On the server’s Terminal, run
lsof -i :2345— you should see a line forrubywithTCPSERVERlisted, confirming the server is active and waiting for connections.
内容的提问来源于stack exchange,提问作者Robert Faldo

