Socket s = new Socket();
s.setSoTimeout(3000);
s.connect(new InetSocketAddress("baidu.com",80));
s.getOutputStream().write("g\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n".getBytes());
s.getOutputStream().flush();
System.out.println(s.getInputStream().read());
System.out.println("local"+s.getLocalAddress());
上面代码按理说应该返回本机IP, 比如192.168.0.xxx,在手机上运行正常。
但是电脑上运行一直得到127.0.0.1。。。郁闷了好几天,终于发现机关,原来。。。。