# Gserver io.peeraddr and io.addr not working

**URL:** <https://rubytalk.org/t/gserver-io-peeraddr-and-io-addr-not-working/64494>\
**Category:** ruby-talk\
**Created:** [31 December 2011 02:42 UTC](https://rubytalk.org/t/gserver-io-peeraddr-and-io-addr-not-working/64494 "2011-12-31T02:42:34Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shelvacu\_V](https://avatars.discourse-cdn.com/v4/letter/s/ea5d25/32.png) [@Shelvacu\_V](https://rubytalk.org/u/Shelvacu_V)\
**Post date:** [31 December 2011 02:42 UTC](https://rubytalk.org/t/gserver-io-peeraddr-and-io-addr-not-working/64494/1 "2011-12-31T02:42:34Z")

</div>

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).

Attachments:  
[http://www.ruby-forum.com/attachment/6874/current\_gserver\_chat.rb](http://www.ruby-forum.com/attachment/6874/current_gserver_chat.rb)

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)\
**Post date:** [1 January 2012 17:29 UTC](https://rubytalk.org/t/gserver-io-peeraddr-and-io-addr-not-working/64494/2 "2012-01-01T17:29:28Z")

</div>

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:

&nbsp;&nbsp;&nbsp;&nbsp;begin  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "[New Client]IP:" + io.peeraddr + " Client #:" + my\_client\_id  
&nbsp;&nbsp;&nbsp;&nbsp;rescue Exception =\> e  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;STDERR.puts "Ooops: #{e}"  
&nbsp;&nbsp;&nbsp;&nbsp;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:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
