ICMP Port Unreachable是什么错误
我现在正在学习<<java网络编程》,书上有个例子,是做2个线程,一个发送udp包,另一个接收udp包,收发均使用同一个socket,但我一运行就提示:java.net.PortUnreachableException: ICMP Port Unreachable
发送线程类:
package test;
import java.net.*;
import java.io.*;
public class SenderThread extends Thread{
private InetAddress server;
private DatagramSocket socket;
private boolean stopped=false;
private int port;
public SenderThread(InetAddress server, int port) throws SocketException{
this.server = server;
this.port = port;
this.socket=new DatagramSocket();
this.socket.connect(server,port);
}
public void halt(){
this.stopped=true;
}
public DatagramSocket getSocket(){
return this.socket;
}
public void run() {
BufferedReader userInput=new BufferedReader(
new InputStreamReader(System.in)
);
try {
while(true){
if (stopped){
return;
}
String theLine=userInput.readLine();
if (theLine.equals(".")){
break;
}
byte[] data=theLine.getBytes();
DatagramPacket output=new DatagramPacket(data,data.length,server,port);
socket.send(output);
Thread.yield();
}
} catch (Exception e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
}
接收线程类:
package test;
import java.net.*;
import java.io.*;
public class ReceiverThread extends Thread{
DatagramSocket socket;
private boolean stopped=false;
public ReceiverThread(DatagramSocket socket) {
this.socket = socket;
}
public void halt(){
this.stopped=true;
}
public void run() {
byte[] buffer=new byte[3000];
while (true){
if (stopped){
return;
}
DatagramPacket dp=new DatagramPacket(buffer,buffer.length);
try {
socket.receive(dp);//异常出现在这行。!!!!!!!!!!!!!!!!
String s=new String(dp.getData(),0,dp.getLength());
System.out.println(s);
Thread.yield();
} catch (Exception e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
}
}
调用者:
package test;
import java.net.*;
import java.io.*;
public class UDPEchoClient {
public final static int DEFAULT_PORT=7;
public static void main(String[] args) {
try {
String hostname="localhost";
int port;
try {
port=Integer.parseInt(args[1]);
} catch (Exception e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
port=DEFAULT_PORT;
}
if (args.length>0){
hostname=args[0];
}
InetAddress ia=InetAddress.getByName(hostname);
SenderThread sender=new SenderThread(ia,port);
sender.start();
Thread receiver=new ReceiverThread(sender.getSocket());
receiver.start();
} catch (UnknownHostException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (Exception e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
}