Gserver io.peeraddr and io.addr not working

I have been trying to get the ip address of clients connected to a
simple chat server for logging purposes, but every time I use io.addr or
io.peeraddr, the client disconnects “connection to host lost” and the
server reports no errors. Is this because the client is connecting to
localhost? io is defined by serve(io).

Shelvacu V. wrote in post #1038960:

I have been trying to get the ip address of clients connected to a
simple chat server for logging purposes, but every time I use io.addr or
io.peeraddr, the client disconnects “connection to host lost” and the
server reports no errors. Is this because the client is connecting to
localhost?

Nope. Try this:

begin
  puts "[New Client]IP:" + io.peeraddr + " Client #:" + my_client_id
rescue Exception => e
  STDERR.puts "Ooops: #{e}"
end

and you will see:

TCPSocket
[verbose]serve() has been called
Ooops: can’t convert Array into String

That is, you need io.peeraddr[3]. And when you do that, you’ll find
my_client_id needs turning into a string too, because you can’t
concatenate a String with a Fixnum.

Safer to use interpolation, which calls to_s automatically if required:

  puts "[New Client]IP:#{io.peeraddr[3]} Client #:#{my_client_id}"

You would have found this problem more easily if you’d told gserver to
report exceptions rather than hide them. You can do this by setting
self.debug=true inside your initialize method in your ChatServer class.

HTH,

Brian.