# Ways of obtaining a remote peer's IP address from distributed ruby?

**URL:** https://rubytalk.org/t/ways-of-obtaining-a-remote-peers-ip-address-from-distributed-ruby/16670
**Category:** ruby-talk
**Created:** [23 February 2005 21:44 UTC](https://rubytalk.org/t/ways-of-obtaining-a-remote-peers-ip-address-from-distributed-ruby/16670 "2005-02-23T21:44:12Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Dido\_Sevilla](https://avatars.discourse-cdn.com/v4/letter/d/9dc877/32.png) [@Dido\_Sevilla](https://rubytalk.org/u/Dido_Sevilla)
#### Post date: [23 February 2005 21:44 UTC](https://rubytalk.org/t/ways-of-obtaining-a-remote-peers-ip-address-from-distributed-ruby/16670/1 "2005-02-23T21:44:12Z")

</div>

Is there a way to let a method called via druby over TCP/IP learn the  
IP address of the remote peer invoking it? If not, is there a way to  
allow a local node to obtain its own IP address? I'm writing a program  
that involves having clients connect to a server and register  
addresses where they can be contacted via druby whenever asynchronous  
events later occur. They will then get called by the server when the  
asynchronous event occurs. DRb.uri gives useless results for this  
application because DRb seems to always use the machine's actual  
hostname in the URI, which usually doesn't resolve because many of  
the clients have dynamic IP's and don't have valid hostnames as such,  
especially the Windows boxes which run the client.

---

<div class="post-metadata">

### Author: ![Anders\_Engstrom](https://avatars.discourse-cdn.com/v4/letter/a/8e7dd6/32.png) [@Anders\_Engstrom](https://rubytalk.org/u/Anders_Engstrom)
#### Post date: [23 February 2005 22:03 UTC](https://rubytalk.org/t/ways-of-obtaining-a-remote-peers-ip-address-from-distributed-ruby/16670/2 "2005-02-23T22:03:07Z")

</div>

I had the same question a couple of months ago - and thanks to  
#ruby-lang@FreeNode and some code-browsing I found out that the  
following should work (in most cases):

&nbsp;&nbsp;&nbsp;&nbsp;(in a distributed object instance)  
&nbsp;&nbsp;&nbsp;&nbsp;def client\_address()  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if client = Thread.current['DRb']['client']  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return client.peeraddr if client.respond\_to?(:peeraddr)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end

DRb binds some useful stuff to the executing thread - but this is very  
un-documented 😕 I'm not sure how reliable this is from one version to  
the next.

//Anders

> **···**
>
> On Thu, Feb 24, 2005 at 06:44:12AM +0900, Dido Sevilla wrote:
> 
> > Is there a way to let a method called via druby over TCP/IP learn the  
> > IP address of the remote peer invoking it? If not, is there a way to  
> > allow a local node to obtain its own IP address? I'm writing a program  
> > that involves having clients connect to a server and register  
> > addresses where they can be contacted via druby whenever asynchronous  
> > events later occur. They will then get called by the server when the  
> > asynchronous event occurs. DRb.uri gives useless results for this  
> > application because DRb seems to always use the machine's actual  
> > hostname in the URI, which usually doesn't resolve because many of  
> > the clients have dynamic IP's and don't have valid hostnames as such,  
> > especially the Windows boxes which run the client.
> 
> --  
> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  
> . Anders Engström aengstrom@gnejs.net  
> . [http://www.gnejs.net](http://www.gnejs.net) PGP-Key: ED010E7F  
> . [Your mind is like an umbrella. It doesn't work unless you open it.]
