sockets - How to get computer's WAN IP address in Java? -
how wide area network of computer java? try this:
serversocket ss = new serversocket(port); system.out.println(ss.getinetaddress().gethostaddress()); //wich return 0.0.0.0
then try this:
system.out.println(inetaddress.getlocalhost().tostring()); //which return keenan-a658368c/192.168.1.100 < yes connected router
like function said, return local ip address
how wan ip address? such 118.137.43.219
you can http://whatismyip.com/automation/n09230945.asp. can open httpurlconnection site , parse output.
this program should helpful :
import java.net.httpurlconnection; public class getexternalip { public static void main(string args[]) { try { java.net.url url = new java.net.url( "http://whatismyip.com/automation/n09230945.asp"); java.net.httpurlconnection con = (httpurlconnection) url .openconnection(); java.io.inputstream stream = con.getinputstream(); java.io.inputstreamreader reader = new java.io.inputstreamreader( stream); java.io.bufferedreader breader = new java.io.bufferedreader(reader); system.out.print("your ip address " + breader.readline()); } catch (exception e) { e.printstacktrace(); } } }
referenced :
http://www.daniweb.com/forums/thread192872.html
http://www.coderanch.com/t/411356/java/java/public-ip-address-time-limit
Comments
Post a Comment