0
public boolean FileToByteArray(DatagramSocket socket, InetAddress dst_addr, int dst_port) throws Exception{
        File file = new File("input.txt");
        InputStream is = new FileInputStream(file);
        long length = file.length();
        byte[] bytes = new byte[(int) length];
        int offset = 0, n = 0;
        DatagramPacket out_pkt = null;

            while (offset < bytes.length && (n = is.read(bytes, offset, bytes.length - offset)) >= 0) {
                offset += n;
                out_pkt = new DatagramPacket(bytes, pkt_size, dst_addr, dst_port);
                packetCount++;
                System.out.println("*****************" + packetCount);
                System.out.println(out_pkt.getLength());
                socket.send(out_pkt);
            }
            is.close();

            String s = new String(bytes);
            System.out.println(s + " " + s.length()) ;
            return true;

      }

大家好, 我正在处理一个需要我通过 UDP 发送大文本文件的任务。我能够读取 10000 字节,但是当我将 pkt_size 设置为 1000 时,它只传送 1000 字节,即超过 1KB 的数据。我如何将大文件分成小数据包发送?

4

0 回答 0