I have made a coded to receive the TCP request in Ruby, but I am not able to get the Ip addresss of the coming request.
My code is as:
require 'socket'
puts "Starting the Server..................."
server = TCPServer.new 53492 # Server bound to port 53492
loop do
Thread.start(server.accept) do |client|
# client = server.accept # Wait for a client to connect
# client.puts "Hello you are there!"
result = ''
ansiString = client.recv(100).chomp
p "String = #{ansiString}"
begin
# How to get the request IP adress here
rescue Errno::EPIPE
puts "Connection broke!"
end
end
end
See IPSocket#peeraddr
.
p client.peeraddr(false)[3]
Or a bit more legibly:
address_family, port, hostname, numeric_address = client.peeraddr(false)
p numeric_address