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

Discussion in 'Ruby' started by Victor Reyes, Dec 27, 2007.

  1. Victor Reyes

    Victor Reyes Guest

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

    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
    Victor Reyes, Dec 27, 2007
    #1
    1. Advertising

  2. Victor Reyes

    MonkeeSage Guest

    On Dec 27, 9:13 am, Victor Reyes <> 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
    ....

    > 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


    HTH,
    Jordan
    MonkeeSage, Dec 27, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. AWieminer
    Replies:
    0
    Views:
    751
    AWieminer
    Jul 12, 2005
  2. vi
    Replies:
    5
    Views:
    16,827
    Roedy Green
    Sep 19, 2005
  3. Gabriel Genellina

    Re: 'Address already in use' ... with TCPServer

    Gabriel Genellina, Jan 30, 2009, in forum: Python
    Replies:
    4
    Views:
    548
    Gabriel Genellina
    Jan 30, 2009
  4. Microlong®
    Replies:
    3
    Views:
    181
    Evertjan.
    Oct 12, 2005
  5. Replies:
    19
    Views:
    520
    Dan Katorza
    Sep 20, 2012
Loading...

Share This Page