如何获取在Ruby TCP服务器中请求的IP地址?

我已经编写了一个代码来接收Ruby中的TCP请求,但是我无法获取即将到来的请求的IP地址。
我的代码是:

j
catheone 回答:如何获取在Ruby TCP服务器中请求的IP地址?

请参见IPSocket#peeraddr

p client.peeraddr(false)[3]

或更清晰一点:

address_family,port,hostname,numeric_address = client.peeraddr(false)
p numeric_address
,
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!"
    p "Client address = #{client.peeraddr[3]}"  ## Answer
    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
本文链接:https://www.f2er.com/3076400.html

大家都在问