Socket s = new Socket(InetAddress.getLocalHost(),8500);
这个不行
改为:
InetAddress addr = InetAddress.getByName("localhost");
Socket s = new Socket(addr,8500);
就行
这个不行
改为:
InetAddress addr = InetAddress.getByName("localhost");
Socket s = new Socket(addr,8500);
就行
try{
InetAddress addr = InetAddress.getLocalHost();
System.out.println(addr.toString());
}catch (UnknownHostException e){
e.printStackTrace();
};
}
我改为:
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