Search code examples
javasocketsipserversocketinetaddress

get IP from socket connect from localhost in java


how i get ip of socket conncet from localhost ?

i try this :

ServerSocket ss = new ServerSocket(1919);
        Socket test,s = new Socket("127.0.0.1",1919);

        test=ss.accept();
        System.out.println("we connect ");

        InetAddress IP=test.getInetAddress();
        System.out.println("IP of my system is := "+IP.getHostAddress()+"----"+IP);

the out put :

IP of my system is := 127.0.0.1----/127.0.0.1

but i need : IP:192.168.0.XXX

thanxx


Solution

  • I need : IP:192.168.0.XXX

    You will never get it. It would be a wrong answer. Your Socket is connected via 127.0.0.1, so the source address can only be 127.0.0.1.

    If you connected your a Socket to 193.168.0.XXX you would get that as the source address.