0

I'm trying to run a server application in PC using ServerSocket, for that I trying to get the system's IP address to start the server and to wait for client to connect, for that I've written,

InetAddress inetAddress = InetAddress.getLocalHost();
ServerSocket serverSocket;
if (serverSocket == null)
serverSocket = new ServerSocket(1000, 0, inetAddress);
Socket socket=serverSocket.accept();

Its working correctly in the Window OS, when I try this application in Unix OS its not working for me, I tried to print the IP address using,

System.out.println(inetAddress.getHostAddress);

in Windows OS correct IP address gets printed but in Unix OS, what I got was

127.0.0.1

so the server is not working, I have not tried this in Mac OS, so is there any way to start the server using the system's default IP address in any OS.

Thanks.

Vignesh
  • 3,571
  • 6
  • 28
  • 44
  • Its normal its the loop back. 127.0.0.1 always refer to your localhost. – Lynch Jul 04 '11 at 05:38
  • So what that means? Its the address of lo, right? instead I want the address of eth0 to start the server. – Vignesh Jul 04 '11 at 05:43
  • Check this post [how-to-get-the-ip-of-the-computer-on-linux-through-java](http://stackoverflow.com/questions/901755/how-to-get-the-ip-of-the-computer-on-linux-through-java) it might help. – Lynch Jul 04 '11 at 06:06

4 Answers4

1

This appears to be a pretty common issue. See the following links for some possible solutions: java InetAddress.getLocalHost(); returns 127.0.0.1 ... how to get REAL IP?

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4665037

Specifically, the work around and evaluation in the sun bug link explains how the function works on linux and how to make sure your linux box is properly set up to return the correct ip when queried from java.

Community
  • 1
  • 1
Jon7
  • 7,165
  • 2
  • 33
  • 39
1

InetAddress localhost = InetAddress.getLocalHost();

    // this code assumes IPv4 is used

    byte[] ip = localhost.getAddress();

    for (int i = 1; i <= 254; i++) {

    ip[3] = (byte)i;
    InetAddress address = InetAddress.getByAddress(ip);
    if (address.isReachable(1000))
    {
        // machine is turned on and can be pinged
    }
    else if (!address.getHostAddress().equals(address.getHostName()))
    {

        // machine is known in a DNS lookup
    }
    else
    {
        // the host address and host name are equal, meaning the host name could not be resolved
    }

    }
Rupok
  • 2,062
  • 16
  • 16
1

Just pass null for the InetAddress. Also be aware that your specified backlog may be adjusted up or down by the system. If you don't have a really good reason for specifying it just use new ServerSocket(0).

user207421
  • 305,947
  • 44
  • 307
  • 483
1

Try this example :

import java.net.*;

import java.util.*;

import java.util.regex.Pattern;

public class GetPublicHostname { public static void main(String[] args) throws Throwable { System.out.println("The IP : " + tellMyIP()); }

public static String tellMyIP()
{
    NetworkInterface iface = null;
    String ethr;
    String myip = "";
    String regex = "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";
    try
    {
        for(Enumeration ifaces = NetworkInterface.getNetworkInterfaces();ifaces.hasMoreElements();)
        {
            iface = (NetworkInterface)ifaces.nextElement();
            ethr = iface.getDisplayName();

            if (Pattern.matches("eth[0-9]", ethr))
            {
                System.out.println("Interface:" + ethr);
                InetAddress ia = null;
                for(Enumeration ips = iface.getInetAddresses();ips.hasMoreElements();)
                {
                    ia = (InetAddress)ips.nextElement();
                    if (Pattern.matches(regex, ia.getCanonicalHostName()))
                    {
                        myip = ia.getCanonicalHostName();
                        return myip;
                    }
                }
            }
        }
    }
    catch (SocketException e){}
    return myip;
} }
Rakesh
  • 4,264
  • 4
  • 32
  • 58