Socket s = new Socket(InetAddress.getLocalHost(),8500);
这个不行
改为:
InetAddress addr = InetAddress.getByName("localhost");
Socket s = new Socket(addr,8500);
就行

解决方案 »

  1.   

    类型为 static 的函数不能访问类的 instance member, 因为有可能类并没有被实例化。
      

  2.   

    不需要用socket,因为它会涉及到安全性问题。只需要InetAddress就可以了。使用如下代码:
    try{
    InetAddress addr = InetAddress.getLocalHost();
    System.out.println(addr.toString());
    }catch (UnknownHostException e){
    e.printStackTrace();
    };
    }
      

  3.   

    to:alphazhao(绿色咖啡) 
    我改为:
    import java.io.*;
    import java.net.*;public class Test
    {
    public static void main(String args[])
    {
    InetAddress addr = InetAddress.getByName("localhost");
    Socket s = new Socket(addr,8500);
    System.out.println(s);
    }
    }出错提示:
    E:\javatest\socket\Test.java:8: Exception java.net.UnknownHostException must be caught, or it must be declared in the throws clause of this method.
    InetAddress addr = InetAddress.getByName("localhost");
                                            ^
    E:\javatest\socket\Test.java:9: Exception java.io.IOException must be caught, or it must be declared in the throws clause of this method.
    Socket s = new Socket(addr,8500);
               ^
    2 errors