我现在正在学习<<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.
}
}
}
发送线程类:
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.
}
}
}
还有可能是PORT被屏蔽了.
1。你的地址写的不对;
2。防火墙过滤了某些规则的包;给分
怎么知道是否可以运行icmp协议,再说我是发给自己,也没有发给别的机器啊。
你完全是按照书上写的吗? 没有任何改动?
发送程序:UdpSend.java
import java.net.*;
public class UdpSend
{
public static void main(String [] args) throws Exception
{
DatagramSocket ds=new DatagramSocket();
String str="hello world";
DatagramPacket dp=new DatagramPacket(str.getBytes(),str.length(),
InetAddress.getByName("127.0.0.1"),3000);
ds.send(dp);
ds.close();
}
}接收程序:UdpRecv.java
import java.net.*;
public class UdpRecv
{
public static void main(String [] args) throws Exception
{
DatagramSocket ds=new DatagramSocket(3000);
byte [] buf=new byte[1024];
DatagramPacket dp=new DatagramPacket(buf,1024);
ds.receive(dp);
String strRecv=new String(dp.getData(),0,dp.getLength()) +
" from " + dp.getAddress().getHostAddress()+":"+dp.getPort();
System.out.println(strRecv);
ds.close();
}
}
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) {
this.server = server;
this.port = port;
try {
this.socket = new DatagramSocket();
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 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.
}
}
}import java.net.*;
import java.io.*;public class ReceiverThread extends Thread {
DatagramSocket socket; private boolean stopped = false; public ReceiverThread() {
try {
socket = new DatagramSocket(444);
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} 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.
}
}
}
}import java.net.*;
import java.io.*;public class UDPEchoClient {
public final static int DEFAULT_PORT = 444; public static void main(String[] args) {
try {
String hostname = "localhost";
int port = DEFAULT_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();
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.
}
}
}
在CSDN上还能见到摇友, 不容易啊. 缘份啊.不能用一个Socket是我猜的, 具体原因我也不清楚了, 反正既然用两个可以以后记着就用两个就成了.