gpt4 book ai didi

java - DatagramSocket : File sending, 客户端未收到图像文件

转载 作者:行者123 更新时间:2023-11-30 04:56:00 25 4
gpt4 key购买 nike

我正在尝试使用 DatagramSocket 发送一堆文件。但有一个问题我无法弄清楚。传输图像文件以外的文件进展顺利,但在发送图像文件时,尽管服务器发送了文件,但客户端仍卡在接收位置。这是我的客户端和服务器端代码块:

服务器:

    while (true) {
System.out.print("DatagramSocket: Waiting for file request...\n");
buf = new byte[Integer.SIZE];
packet = new DatagramPacket(buf, buf.length);
datagramSocket.receive(packet);
bb = ByteBuffer.wrap(buf);

// Receive request
buf = new byte[bb.getInt()];
packet = new DatagramPacket(buf, buf.length);
datagramSocket.receive(packet);

System.out.print("DatagramSocket: File request received.\n");
System.out.print("DatagramSocket: Requested file: "+new String(buf)+"\n");

// Check file if it is exist.
File file = new File("kaynak/"+new String(buf));

if (!file.exists()) {
System.out.print("DatagramSocket: File not found!\n");
return;
}

// Send file length.
System.out.printf("DatagramSocket: Sending file length: %d\n", file.length());
bb = ByteBuffer.allocate(Integer.SIZE).putInt((int) file.length());
buf = bb.array();
packet.setData(buf);
packet.setLength(buf.length);
datagramSocket.send(packet);

// Send file's relative path.
String relativePath = file.getAbsolutePath().substring(System.getProperty("user.dir").length() + 1);
System.out.printf("DatagramSocket: Sending file relative path: %s\n", relativePath);
bb.putInt(relativePath.getBytes().length);
datagramSocket.send(packet);
packet.setData(relativePath.getBytes());
packet.setLength(relativePath.getBytes().length);
datagramSocket.send(packet);

// Save file to byte array.
bis = new BufferedInputStream(new FileInputStream(file));
fileByteArray = new byte[(int) file.length()];
bis.read(fileByteArray);

System.out.printf("DatagramSocket: Sending file.\n");
int r = (int) file.length();
int c = 0;

// Send file.
for (int i = 0; i < file.length(); i++) {
c = r < 512 ? r : 512;
packet.setData(ByteBuffer.wrap(fileByteArray, i, c).array());
packet.setLength(c);
datagramSocket.send(packet);
r -= 512;
i += 511;
}
System.out.printf("DatagramSocket: File send.\n\n");
}

客户:

// Send file request.
bb = ByteBuffer.allocate(Integer.SIZE).putInt(files.get(i).getBytes().length);
message = bb.array();
packet = new DatagramPacket(message, message.length, InetAddress.getByName(host), dport);
datagramSocket.send(packet);
message = files.get(i).getBytes();
System.out.print("Requesting: "+new String(message)+"\n");
packet.setData(message);
packet.setLength(message.length);
datagramSocket.send(packet);

// Receive file size.
System.out.print("Requesting file length.\n");
message = ByteBuffer.allocate(Integer.SIZE).array();
packet.setData(message);
packet.setLength(Integer.SIZE);
datagramSocket.receive(packet);
bb = ByteBuffer.wrap(message);
int arraySize = bb.getInt();
System.out.printf("File size = %d baytes.\n", arraySize);
fileByteArray = new byte[arraySize];

// Receive file's relative path.
System.out.print("Requesting file's relative path.\n");
datagramSocket.receive(packet);
message = ByteBuffer.allocate(message.length).array();
packet.setLength(message.length);
datagramSocket.receive(packet);
String htmlPath = new String(packet.getData());
System.out.printf("File's relative path = %s.\n", htmlPath);
File file = new File("hedef/"+htmlPath.substring("kaynak".length()));
file.getParentFile().mkdirs();
file.createNewFile();

// Receive file content.
System.out.print("Requesting file content.\n");
int r = arraySize;
int c = 0;

for (int j = 0; j < arraySize; j++) {
c = r < 512 ? r : 512;
packet.setData(ByteBuffer.wrap(fileByteArray, j, c).array());
packet.setLength(c);
datagramSocket.receive(packet);
r -= 512;
j += 511;
}

// Save file.
System.out.print("Saving file.\n");
bos = new BufferedOutputStream(new FileOutputStream(file));
bos.write(fileByteArray);
bos.flush();
System.out.print("File saved.\n\n");

如果能给我有关我的代码、性能或错误使用提示的提示,我也很感激。
提前致谢。

更新:

在发送文件本身的 for 循环中添加一点 sleep 时间后,现在我可以完成发送文件,但我收到的所有图像文件都已损坏。

for (int j = 0; j < arraySize; j++) {
c = r < 512 ? r : 512;
packet.setData(ByteBuffer.wrap(fileByteArray, j, c).array());
packet.setLength(c);
datagramSocket.receive(packet);
r -= 512;
j += 511;
Thread.sleep(50);
}

最佳答案

来自 DatagramSocket 文档:此方法会阻塞,直到收到数据报为止。显然有包裹丢失了。

另外:最好不要为要接收的孔数据分配数组。考虑一些 4GB 视频文件...而是分配一个读取大小 (512) 的缓冲区数组,并在 DatagramSocket.receive() 之后立即将其写入 BufferedOutputStream。

关于java - DatagramSocket : File sending, 客户端未收到图像文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8518184/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com