Linux- Socket server listen on localhost, not on the machine IP

Discussion in 'Java' started by cyril, Jun 24, 2004.

  1. cyril

    cyril Guest

    Hi,

    I have a little problem with my socket server running on Linux (RH pro
    3). Actually, my server socket is listening on the wanted port but at
    the localhost IP and not at the "external" IP of my PC. Is there
    something to add in my code to specify the wanted IP to listen
    (external or not) ?

    Or maybe this is a Linux pb ? Maybe someone have ever meet the same
    pb.

    Thanks for all.
     
    cyril, Jun 24, 2004
    #1
    1. Advertising

  2. On 24 Jun 2004 09:48:38 -0700, cyril wrote:
    > Actually, my server socket is listening on the wanted port but at
    > the localhost IP and not at the "external" IP of my PC. Is there
    > something to add in my code to specify the wanted IP to listen
    > (external or not) ?


    There are various ServerSocket constructors to let you do that.

    If you only specify a port number when you create the ServerSocket, it
    will listen on all of the interfaces.

    If you only want it to listen on the external interface, specify the
    address of that interface in addition to the port number.

    Also, have a look at java.io.NetworkInterface, which provides a
    mechanism for enumerating all of the machines interfaces and getting
    their addresses.

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Jun 24, 2004
    #2
    1. Advertising

  3. cyril

    iksrazal Guest

    (cyril) wrote in message news:<>...
    > Hi,
    >
    > I have a little problem with my socket server running on Linux (RH pro
    > 3). Actually, my server socket is listening on the wanted port but at
    > the localhost IP and not at the "external" IP of my PC. Is there
    > something to add in my code to specify the wanted IP to listen
    > (external or not) ?
    >
    > Or maybe this is a Linux pb ? Maybe someone have ever meet the same
    > pb.
    >
    > Thanks for all.


    Your linux box has two NIC's, right? So does mine. Do an ifconfig .
    You should see two ip`s . There's a lot of ways to do java sockets,
    but you could have something like:

    new InetSocketAddress(hostName, port)

    Change hostname from localhost to your external ip. Should be as
    simple as that. if in doubt, do a print on Socket.getInetAddress() or
    SocketChannel.getInetAddress() .

    Note however, typically you would want your socket to listen on your
    internal ip, and use NAT/iptables to port forward/masquerade as a
    firewall to your external ip. That`s a bit OT here.

    HTH

    Outsource to an American programmer living in brazil!
    http://www.braziloutsource.com/
    iksrazal
     
    iksrazal, Jun 25, 2004
    #3
  4. Re: Linux- Socket server listen on localhost, not on the machineIP

    Are you binding to the correct ip/interface?

    try running the following command:

    netstat -an | grep LISTEN | grep tcp

    You should see something like:

    tcp 0 0 0.0.0.0:32770 0.0.0.0:* LISTEN
    tcp 0 0 127.0.0.1:32771 0.0.0.0:* LISTEN
    tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN
    tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN
    tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
    tcp 0 0 :::22 :::*

    If the linux box is listening on ,say, port 25 on all ips, you should
    see something like:

    tcp 0 0 0.0.0.0:25 0.0.0.0:* LISTEN

    If it is listening on a specific interface only, you should see the ip
    address:
    i.e. 192.168.77.2:25
    or 127.0.0.1:25 (for localhost)

    If it is listening on a specific ip address or 0.0.0.0 and it is still
    not responding then you probably have a firewall. Most standard RedHat
    distros ship with iptables or ipchains.

    -Misk


    cyril wrote:
    > Hi,
    >
    > I have a little problem with my socket server running on Linux (RH pro
    > 3). Actually, my server socket is listening on the wanted port but at
    > the localhost IP and not at the "external" IP of my PC. Is there
    > something to add in my code to specify the wanted IP to listen
    > (external or not) ?
    >
    > Or maybe this is a Linux pb ? Maybe someone have ever meet the same
    > pb.
    >
    > Thanks for all.
     
    Missaka Wijekoon, Jun 25, 2004
    #4
    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. Jiong Feng
    Replies:
    0
    Views:
    844
    Jiong Feng
    Nov 19, 2003
  2. Alex
    Replies:
    4
    Views:
    577
    Hasani Blackwell
    Feb 8, 2005
  3. Donny
    Replies:
    13
    Views:
    672
    Donny
    Aug 5, 2004
  4. koicat
    Replies:
    0
    Views:
    230
    koicat
    Jul 2, 2010
  5. nephish
    Replies:
    0
    Views:
    133
    nephish
    Mar 2, 2011
Loading...

Share This Page