开发者

How to open a port in Windows using Ruby?

开发者 https://www.devze.com 2023-02-17 03:48 出处:网络
My windows system has t开发者_运维技巧he firewall enabled. I would like to allow incoming connections on a particular port (say: 4546).

My windows system has t开发者_运维技巧he firewall enabled.

I would like to allow incoming connections on a particular port (say: 4546).

Is there a ruby library that can help me do this ?

Detail: I have a sinatra application (webserver) running on port 4546. I needed to bring down the firewall in order for it to work. I am looking for a way to not keep the port 4546 under the firewall list.


Yes, you can do that with this:

require 'socket'               # Get sockets from stdlib

server = TCPServer.open(4546)  # Socket to listen on port 4546
loop {                         # Servers run forever
  client = server.accept       # Wait for a client to connect
  client.puts(Time.now.ctime)  # Send the time to the client
  client.puts "Closing the connection. Bye!"
  client.close                 # Disconnect from the client
}
0

精彩评论

暂无评论...
验证码 换一张
取 消