简体   繁体   中英

How to open a port in Windows using Ruby?

My windows system has the 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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM