黑马程序员技术交流社区

标题: 为什么出现的ip会是0.0.0.0 [打印本页]

作者: 微笑=.一瞬间    时间: 2014-4-14 18:29
标题: 为什么出现的ip会是0.0.0.0
程序如下

import java.io.*;
import java.net.*;
class Tc1
{
        public static void main(String[] args)throws Exception  
        {
                Socket s=new Socket("172.30.81.92",10007);
        }
}
class Ts1
{
        public static void main(String[] args)throws Exception
        {
                ServerSocket ss=new ServerSocket(10007);
                Socket s=ss.accept();
                String ip=ss.getInetAddress().getHostAddress();
                System.out.println(ip);
        }
}
172.30.81.92 是本机ip  服务端输出的ip是0.0.0.0是怎么回事?
作者: lwy0319    时间: 2014-4-14 20:21
  1. import java.io.IOException;
  2. import java.net.*;

  3. public class  Ts1 {
  4.         public static void main(String[] args) throws IOException {
  5.                 ServerSocket ss = new ServerSocket();
  6.                 ss.bind(new InetSocketAddress("172.30.81.92", 10007));   
  7.                 System.out.println(ss.getInetAddress().getHostAddress());
  8.         }
  9. }
复制代码

你的客户端没有问题
作者: 张稳    时间: 2014-4-14 21:48
  1. import java.io.*;
  2. import java.net.*;
  3. class Tc1
  4. {
  5.         public static void main(String[] args)throws Exception  
  6.         {
  7.                 Socket s=new Socket("172.30.81.92",10007);
  8.         }
  9. }
  10. class Ts1
  11. {
  12.         public static void main(String[] args)throws Exception
  13.         {
  14.                 ServerSocket ss=new ServerSocket(10007);
  15.                 Socket s=ss.accept();
  16. //你写的是ip=ss.getInetAddress().getHostAddress();应该是以下:
  17.                 String ip=s.getInetAddress().getHostAddress();

  18.                 System.out.println(ip);
  19.         }
  20. }
复制代码






欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2