How to open a port in Windows using Ruby? - 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
}

Related

Ruby: How to verify TCP connection alive

Server:
s = TCPServer.open(6000)
loop do
Thread.start(s.accept) do |client|
# Keep receive and handle message from client
...
end
end
Clients:
server = TCPSocket.open(server_ip, 6000)
... # Send message if event, will keep TCP connection
Question:
Sometimes network down or client crash, How does sever know the TCP connection is alive? Is there a method or command the verify the connection?
Thanks
The most reliable way to verify the state of a TCP connection is to send an empty packet to the server and check if you get a response or an error. That will give you the current connection state of the socket.

Keeping TCPSocket open: server or client's responsibility?

I have been reading examples online about Ruby's TCPSocket and TCPServer, but I still don't know and can't find what's the best practice for this. If you have a running TCPServer, and you want to keep the socket open across multiple connections/clients, who should be responsible in keeping them open, the server or the clients?
Let's say that you have a TCPServer running:
server = TCPServer.new(8000)
loop do
client = server.accept
while line = client.gets
# process data from client
end
client.puts "Response from server"
client.close # should server close the socket?
end
And Client:
socket = TCPSocket.new 'localhost', 8000
while line = socket.gets
# process data from server
end
socket.close # should client close the socket here?
All of the examples I have seen have the socket.close at the end, which I would assume is not what I want as that would close the connection. Server and clients should maintain open connection as they will need to send data back and forth.
PS: I'm pretty a noob on networking, so just kindly let me know if my question sounds completely dumb.
The server is usually responsible for keeping the connections open because the client (being the one connecting to the server) can break the connection at anytime.
Servers are usually in charge of everything that the client doesn't care about. A video game doesn't really care about the connection to the server as long as it's there. It just wants its data so it can keep running.

UDP Socket and ruby

I am having trouble to bind to a server address. I have a connection, to a server (using Savon XML Library). Now I just need to listen to that server and gather its HTTP packets that sends. The server each time sends:
http://200.34.12.168/Videos/1/frame/0
http://200.34.12.168/Videos/1/frame/1
http://200.34.12.168/Videos/1/frame/2
http://200.34.12.168/Videos/1/frame/3
http://200.34.12.168/Videos/1/frame/4
...
..
which are HTTP packets. I am trying to create a UDP server that listens to these. This is what I have so far:
s = UDPSocket.new
s.bind('200.34.12.168', 80)
5.times do
text, sender = s.recvfrom(16)
puts text
end
it fails at the bind function. How can I listen to a UDP connection with ruby?
The Error I get:
"`bind': Can't assign requested address - bind(2) (Errno::EADDRNOTAVAIL)"
Do you have a web browser running - it may already be bound to port 80..

ruby socket connection to outside

I trying to make a connection with following ruby code
server
require 'socket'
puts "server"
socket = TCPServer.open(1000)
client = socket.accept
puts "connected"
client
require 'socket'
puts "client"
server= TCPSocket.open("172.30.1.2",1000)
puts "connected"
but client side always says target machine refused it.
no "connected" printing on both side
when I change "172.30.1.2" to "localhost", it works.
I wonder why I can't connect to my self
"172.30.1.2" is my IPv4 address
when I type ipconfig on cmd,
it says "172.30.1.2" is my ip.
I configure DMZ and port-forwarding too.
and all firewall is off.
I tried on windows xp and windows 7.
can someone help me?
(I want to connect to my computer from outside. and that's why I tried "172.30.1.2")
thanks.
In order to create server use TCPServer.open("172.30.1.2",1000)

Monitoring of an active SMTP service on a website

I am trying to check if network services i.e. HTTP, HTTPs,TCP, SMTP, FTP, Telnet are activated on a website and running correctly without any breaks. I wrote the following code to open the TCP connection to the remote host on remote port and check if the connection can be established with the website.
require 'timeout'
require 'socket'
def ping(host,port)
begin
Timeout::timeout(5) do
s=TCPSocket.new(host,port)
s.close
return true
end
rescue Err::ECONNREFUSED
return false
rescue Err::Timeout
return false
end
end
I also wrote code to monitor the SMTP service already running on a website.
smtp=Net::SMTP.start("mail.google.com",25).started?
But the output is false whereas gmail supports SMTP service. So the question is:
What should be done to monitor the SMTP service already running on a website?

Resources