# How do I get the hostname or IP address during a TCPServer "conversation"

**URL:** <https://www.ruby-forum.com/t/how-do-i-get-the-hostname-or-ip-address-during-a-tcpserver-conversation/124313>\
**Category:** Ruby\
**Created:** [December 27, 2007, 4:16pm UTC](https://www.ruby-forum.com/t/how-do-i-get-the-hostname-or-ip-address-during-a-tcpserver-conversation/124313 "2007-12-27T16:16:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ribes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@ribes](https://www.ruby-forum.com/u/ribes)\
**Post date:** [December 27, 2007, 4:16pm UTC](https://www.ruby-forum.com/t/how-do-i-get-the-hostname-or-ip-address-during-a-tcpserver-conversation/124313/1 "2007-12-27T16:16:06Z")

</div>

Hello Team,

I have a client which connects to the simple server listed below.  
I would like to be able to get either the hostname or the IP address of  
the  
client.  
Is there a way to do this? I am sure that this information is hidden in  
some  
place, as the server needs it to return the call.

Also, in the client code I specified the length of the arriving (recv)  
string. Is there a way to receive any size output from the server?

Thank you

This is the server code:

```
    require 'socket'
    port = 19557
    server = TCPServer.new("", port)

    while (session = server.accept)
            input = session.gets
            userCMD_output = `#{input}`
            session.write("#{userCMD_output}")
            session.close
    end

```

This is the client code:

def runIt( srvr, comm )  
require ‘socket’  
port = 19557  
streamSock = TCPSocket.new( srvr, port )  
streamSock.puts("#{comm}\n")  
str = streamSock.recv( 64000 )  
puts “Output from Server: #{srvr}”  
print str  
puts “\n”  
streamSock.close  
end

Victor

---

<div class="post-metadata">

**Author:** ![ribes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@ribes](https://www.ruby-forum.com/u/ribes)\
**Post date:** [December 27, 2007, 5:16pm UTC](https://www.ruby-forum.com/t/how-do-i-get-the-hostname-or-ip-address-during-a-tcpserver-conversation/124313/2 "2007-12-27T17:16:04Z")

</div>

On Dec 27, 9:13 am, Victor R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> [Note: parts of this message were removed to make it a legal post.]
> 
> Hello Team,
> 
> I have a client which connects to the simple server listed below.  
> I would like to be able to get either the hostname or the IP address of the  
> client.  
> Is there a way to do this? I am sure that this information is hidden in some  
> place, as the server needs it to return the call.

TCPSocket#peeraddr. E.g.,

…  
peer = session.peeraddr.values\_at(2 ,3)  
p peer # =\> [“localhost”, “127.0.0.1”]  
…

> Also, in the client code I specified the length of the arriving (recv)  
> string. Is there a way to receive any size output from the server?

TCPSocket#read. E.g.,

…  
str = streamSock.read  
…

> ```
> userCMD_output = `#{input}`
> streamSock.puts("#{comm}\n")
> str = streamSock.recv( 64000 )
> puts "Output from Server: #{srvr}"
> print str
> puts "\n"
> streamSock.close
> 
> ```
> 
> end
> 
> Victor

HTH,  
Jordan
