//请高手指教一下,谢谢
//出异常 java.net.SocketTimeoutException: Receive timed outpublic class Ping implements Runnable{
private Thread t;
Ping(){
if(t == null){
t = new Thread(this);
t.start();
}
}
public void run()
{
String data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] msg = data.getBytes();
byte[] repBuf = new byte[msg.length]; 
try{
DatagramSocket socket = new DatagramSocket(); 
//设置IP
InetAddress fromIP = InetAddress.getByName("127.0.0.1");
DatagramPacket packet = new DatagramPacket( msg, msg.length, fromIP, 7 );
DatagramPacket reply = new DatagramPacket( repBuf,repBuf.length); 
socket.setSoTimeout(1000);
socket.send(packet);
socket.receive(reply);
System.out.println(repBuf);
}
catch(UnknownHostException ue){
//System.out.println(ue.toString());
}
catch(SocketException se){
//System.out.println(se.toString());
}
catch(IOException ie){
System.out.println(ie.toString());
}
}

解决方案 »

  1.   

    原因是你的发送和接受socket共用一个,这样等socket发送完以后,还占着资源,等用它接收数据时就会出现ICMP port不可达,建议你的发送和接受分开 
      

  2.   


    import java.io.IOException;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;
    import java.net.SocketException;
    import java.net.UnknownHostException;//发送
    public class Ping implements Runnable
    {
    DatagramSocket socket = null;
    private Thread t;
    Ping(){
    if(t == null){
    t = new Thread(this);
    t.start();
    }
    }
    public void run()
    {
    String data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
    byte[] msg = data.getBytes();
    try {
    send(msg);
    } catch (SocketException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }


    public void send(byte[] msg) throws SocketException{
    try {
    socket = new DatagramSocket();
    InetAddress fromIP = InetAddress.getByName("127.0.0.1");
    DatagramPacket packet = new DatagramPacket( msg, msg.length, fromIP, 3000); 
    socket.setSoTimeout(1000);
    socket.send(packet);
    //socket.close();
    } catch (SocketException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    catch (UnknownHostException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }

    public static void main(String[] args){
    new Ping();
    }
    } //接受package ICMP;import java.io.IOException;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.SocketException;public class Recive extends Thread { /**
     * @param args
     */
    private Thread t;
    public Recive(){
    if(t == null){
    t = new Thread(this);
    t.start();
    }
    }
    public void run()
    {
    byte[] repBuf = new byte[1024]; 
    System.out.println(recive(repBuf));
    }

    public String recive(byte[] repBuf){
    String rec = null;
    try {
    DatagramSocket socketre = new DatagramSocket(3000);
    DatagramPacket recf = new DatagramPacket(repBuf,repBuf.length);
    socketre.receive(recf);
    rec = new String(recf.getData(),0,recf.getLength());
    socketre.close();
    return rec;
    } catch (SocketException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return rec;
    }
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    new Recive();
    }}
      

  3.   

    在问问哈JAVA的网络类是不支持ICMP这些底层协议的。这个只是通过UDP去模拟的是吧?
      

  4.   

    是的,但是ICMP是错误报文信息, 网际控制报文,利用ICMP 来传输TCP/UDP的报文,如果你的端口被占用,ICMP就会报出一个ICMP port unreached 错误
      

  5.   

    呀的,兄弟,你用DatagramPacket来收发数据,
    要先启动了接受方,在启动发送发呀,,
    你把他们俩写到了一块,呵呵
      

  6.   

    import java.net.*;
    import java.io.*;
    public class Lesson10 extends Thread
    {
      public static void main(String[] args)
      {
        if(args.length>0)
          recv();
        else
          send();
      }
      public static void recv()
      {
        try {
          DatagramSocket ds=new DatagramSocket(6000);
          byte[] buf=new byte[100];
          DatagramPacket dp=new DatagramPacket(buf,100);
          ds.receive(dp);
          System.out.println(new String(buf,0,dp.getLength()));
          String str="Welcome you!";
          DatagramPacket dpSend=new DatagramPacket(str.getBytes(),str.length(),
                                               dp.getAddress(),dp.getPort());
          ds.send(dpSend);
          ds.close();
        }
        catch (Exception ex) {
          ex.printStackTrace();
        }
      }
      public static void send()
      {
        try {
          DatagramSocket ds=new DatagramSocket();
          String str="Hello,this is zhangsan";
          DatagramPacket dp=new DatagramPacket(str.getBytes(),str.length(),
                                               InetAddress.getByName("localhost"),
                                               6000);
          ds.send(dp);
          byte[] buf=new byte[100];
          DatagramPacket dpRecv=new DatagramPacket(buf,100);
          ds.receive(dpRecv);
          System.out.println(new String(buf,0,dpRecv.getLength()));
          ds.close();
        }
        catch (Exception ex) {
          ex.printStackTrace();
        }
      }
      /*private Socket s;
      public Lesson10(Socket s)
      {
        this.s=s;
      }
      public void run()
      {
        try {
          OutputStream os=s.getOutputStream();
          BufferedOutputStream bos=new BufferedOutputStream(os);
          InputStream is=s.getInputStream();
    //      os.write("Hello,welcome you!".getBytes());
          bos.write("Hello,welcome you!".getBytes());
          //bos.flush();
          byte[] buf=new byte[100];
          int len=is.read(buf);
          System.out.println(new String(buf,0,len));
          //os.close();
          bos.close();
          is.close();
          s.close();
        }
        catch (Exception ex) {
          ex.printStackTrace();
        }
      }
      public static void main(String[] args)
      {
        if(args.length>0)
          server();
        else
          client();
      }
      public static void server()
      {
        try {
          ServerSocket ss=new ServerSocket(6000);
          while(true)
          {
            Socket s = ss.accept();
            new Lesson10(s).start();
          }
          //ss.close();
        }
        catch (Exception ex) {
          ex.printStackTrace();
        }
      }
      public static void client()
      {
        try {
          Socket s=new Socket(InetAddress.getByName(null),6000);
          OutputStream os=s.getOutputStream();
          InputStream is=s.getInputStream();
          byte[] buf=new byte[100];
          int len=is.read(buf);
          System.out.println(new String(buf,0,len));
          os.write("Hello,this is wangwu".getBytes());
          os.close();;
          is.close();
          s.close();
        }
        catch (Exception ex) {
          ex.printStackTrace();
        }  }*/
    }