Mookies 2018-11-25 11:51 采纳率: 100%
浏览 3564
已采纳

UDP给指定IP和端口传输数据包,为什么一直显示Network is unreachable: Datagram send failed

_ UDPClient.java

package com.bill.udp.client;
import java.io.IOException;

import java.io.RandomAccessFile;

import java.net.DatagramPacket;

import java.net.DatagramSocket;

import java.net.InetAddress;

import java.net.InetSocketAddress;

import com.bill.udp.util.UDPUtils;

public class UDPClient {

private static final String SEND_FILE_PATH = "D:/imgmap.jpg";

public static void main(String[] args){

    long startTime = System.currentTimeMillis();    

    byte[] buf = new byte[UDPUtils.BUFFER_SIZE];

    byte[] receiveBuf = new byte[1];

    RandomAccessFile accessFile = null;

    DatagramPacket dpk = null;

    DatagramSocket dsk = null;

    int readSize = -1;

    try {

        accessFile = new RandomAccessFile(SEND_FILE_PATH,"r");
        //构造数据报包,用来将长度为 length 的包发送到指定主机上的指定端口号。 
        dpk = new DatagramPacket(buf, buf.length,new InetSocketAddress(InetAddress.getByName("192.168.1.106"), 5000));
        //创建数据报套接字,将其绑定到指定的本地地址。
        dsk = new DatagramSocket(5001, InetAddress.getByName("192.168.1.106"));

        int sendCount = 0;

        while((readSize = accessFile.read(buf,0,buf.length)) != -1){

            dpk.setData(buf, 0, readSize);

            dsk.send(dpk);

            // wait server response 

            {

                while(true){

                    dpk.setData(receiveBuf, 0, receiveBuf.length);

                    dsk.receive(dpk);



                    // confirm server receive

                    if(!UDPUtils.isEqualsByteArray(UDPUtils.successData,receiveBuf,dpk.getLength())){

                        System.out.println("resend ...");

                        dpk.setData(buf, 0, readSize);

                        dsk.send(dpk);

                    }else

                        break;

                }

            }



            System.out.println("send count of "+(++sendCount)+"!");

        }

        // send exit wait server response

        while(true){

            System.out.println("client send exit message ....");

            dpk.setData(UDPUtils.exitData,0,UDPUtils.exitData.length);

            dsk.send(dpk);



            dpk.setData(receiveBuf,0,receiveBuf.length);

            dsk.receive(dpk);

            if(!UDPUtils.isEqualsByteArray(UDPUtils.exitData, receiveBuf, dpk.getLength())){

                System.out.println("client Resend exit message ....");

                dsk.send(dpk);

            }else

                break;

        }

    }catch (Exception e) {

        e.printStackTrace();

    } finally{

        try {

            if(accessFile != null)

                accessFile.close();

            if(dsk != null)

                dsk.close();

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

    long endTime = System.currentTimeMillis();

    System.out.println("time:"+(endTime - startTime));

}

}
UDPUtils.java

package com.bill.udp.util;

public class UDPUtils {

private UDPUtils(){}

/** transfer file byte buffer **/
public static final int BUFFER_SIZE = 50 * 1024;
/** controller port  **/
public static final int PORT = 50000;
/** mark transfer success **/
public static final byte[] successData = "success data mark".getBytes();
/** mark transfer exit **/
public static final byte[] exitData = "exit data mark".getBytes(); 

public static void main(String[] args) {

    byte[] b = new byte[]{1};

    System.out.println(isEqualsByteArray(successData,b));

}
public static boolean isEqualsByteArray(byte[] compareBuf,byte[] buf){

    if (buf == null || buf.length == 0)

        return false;   

    boolean flag = true;

    if(buf.length == compareBuf.length){

        for (int i = 0; i < buf.length; i++) {

            if(buf[i] != compareBuf[i]){

                flag = false;

                break;

            }

        }

    }else

        return false;

    return flag;

}

public static boolean isEqualsByteArray(byte[] compareBuf,byte[] buf,int len){

    if (buf == null || buf.length == 0 || buf.length < len || compareBuf.length < len)

        return false;

    boolean flag = true;

    int innerMinLen = Math.min(compareBuf.length, len);

        for (int i = 0; i < innerMinLen; i++) {  

            if(buf[i] != compareBuf[i]){

                flag = false;

                break;

            }

        }

    return flag;

}

}

  • 写回答

2条回答 默认 最新

  • threenewbee 2018-11-25 16:08
    关注

    网络不通,或者防火墙拦截了。你两个计算机要在一个网段,各自关闭防火墙,然后双方ping对方,能ping到,再运行你的程序看看。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)