[英]Simple server created in Ruby (TCPServer), how to access from another IP?
require 'socket' # Provides TCPServer and TCPSocket classes
# Initialize a TCPServer object that will listen
# on localhost:2345 for incoming connections
server = TCPServer.new(2345)
loop do
# Wait until a client connects, then return a TCPSocket
# that can be used in a similar fashion to other Ruby
# I/O objects. (In fact, TCPSocket is a subclass of IO.)
socket = server.accept
socket.puts "What do you say?"
they_said = socket.gets.chomp
until they_said == "quit"
socket.puts "You said: #{they_said}!"
they_said = socket.gets.chomp
end
socket.puts "You said: #{they_said}. Goodbye!"
# Close the socket, terminating the connection
socket.close
end
In above code I've created a simple server in ruby (a topic I've just been introduced to). 在上面的代码中,我用ruby创建了一个简单的服务器(我刚刚介绍了一个主题)。 As part of the exercise, I have following instruction:
作为练习的一部分,我有以下指示:
"Connect to your pair's laptop from your laptop. You'll need the network IP address of the laptop with the server on it for this" “从笔记本电脑连接到配对笔记本电脑。为此,您需要带有服务器的笔记本电脑的网络IP地址”
We were using telnet, but it looks like we can't use that with our Macs because it's not allowed on High Siera (at least without further configuration). 我们使用的是telnet,但看来我们无法在Mac上使用telnet,因为High Siera不允许使用它(至少在没有进一步配置的情况下)。
Can anyone suggest how we would do this, or point me in direction of material that will help? 谁能提出我们将如何做的建议,或向我指出可以提供帮助的材料的方向?
If your OS does not accept connections then there is nothing you can do about it on the Ruby level. 如果您的操作系统不接受连接,那么在Ruby级别上您将无能为力。 You will need to disable the Firewall or add an exception for this port.
您将需要禁用防火墙或为此端口添加例外。
Here are the three steps you'll need to take: 这是您需要采取的三个步骤:
Make sure you bind the TCPServer to the "public" IP 确保将TCPServer绑定到“公共” IP
Update: as @siegy22 pointed out in the comments: you can also use "0.0.0.0". 更新:正如@ siegy22在评论中指出的:您也可以使用“ 0.0.0.0”。
You can find it in the Network Preferences or through ipconfig
您可以在“网络偏好设置”中或通过
ipconfig
找到它
en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
ether ca:fe:ca:fe:ca:fe
inet y.y.y.y netmask 0xffffff00 broadcast x.x.x.x
media: autoselect
status: active
Here yyyy
is the IP you are interested in. Create the TCTServer like so: 这里
yyyy
是您感兴趣的IP。像这样创建TCTServer:
TCPServer.new("y.y.y.y", 2345)
Stop the Firewall of your Mac (or allow specific connection to that Port) 停止Mac的防火墙(或允许特定连接到该端口)
This you can do in Preferences/Securiry Section. 您可以在“首选项/安全性”部分中执行此操作。
RE-ENABLE it again after testing 测试后再次重新启用
Connect to the server from another computer 从另一台计算机连接到服务器
telnet y.y.y.y 2345
The above steps assume both computers are in the same Network, no connection through internet. 上述步骤假定两台计算机都在同一网络中,没有通过Internet的连接。 Should you be on a different Network (eg bot connected to the internet) there are two solutions that come to my mind
如果您使用的是其他网络(例如,连接到Internet的漫游器),那么我想到两种解决方案
I suggest you try ngrok (or something similar) first. 我建议您首先尝试ngrok(或类似方法)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.