请查看this question。
这正是我正在寻找的东西,但我还没有达到。我正在通过socketChannel发送多个图像。我尝试将图像大小附加到(已清除的ByteBuffer)中,然后将图像数据附加到相同的缓冲区中(缓冲区大小足够大!)。
首先,在接收端,将通过ByteBuffer.read(...)读取图像大小,然后是ByteBuffer.flip()和ByteBuffer.getLong()(例如imageData = readBuffer.getLong())读取以下图像数据,我可以做ByteBuffer.comact()吗?我怎么知道要读取imageData的确切字节数?
否,我有以下内容: 在发送方:
socketChannelEntity.getWriteBuffer().clear();
if(socketChannelEntity.getFileToProcessCounter() < fileList.size()){
this.fileName = fileList.get(socketChannelEntity.getFileToProcessCounter()).toString();
System.out.println("NIO_CLIENT: " + socketChannelEntity.getEntityName() + ": Send next image: " + fileName);
} else {
System.out.println("NIO_CLIENT: No more images to send.");
socketChannelEntity.setSendReceiveStatus(SendReceiveStatusClient.DONE);
socketChannel.register(this.selector, SelectionKey.OP_READ, socketChannelEntity);
return;
}
Path path = Paths.get(fileName);
long fileSize = Files.size(path);
System.out.println(fileName + " size: " + fileSize);
socketChannelEntity.getWriteBuffer().putLong(fileSize);
try{
FileChannel fileChannel = FileChannel.open(path);
int numRead = 0;
int counter = 0;
while((numRead = fileChannel.read(socketChannelEntity.getWriteBuffer())) > 0){
counter += numRead;
socketChannelEntity.getWriteBuffer().flip();
do {
numRead -= socketChannel.write(socketChannelEntity.getWriteBuffer());
// socketChannelEntity.getWriteBuffer().clear();
} while (numRead > 0);
}
fileChannel.close();
System.out.println("NIO_CLIENT: " + socketChannelEntity.getEntityName() + ": Image " + fileName + " sent: " + counter + " bytes long");
socketChannelEntity.setCheckSum(fileSize);
socketChannelEntity.setSendReceiveStatus(SendReceiveStatusClient.RECEIVE_CHECKSUM_IMAGE);
} catch(IOException e) {
e.printStackTrace();
}
在接收方:
if(socketChannel.socket().getLocalPort() == 10000){
outputFile = Config.HOME_PATH_SERVER_1 + "receivedImage" + fileNameCounter + ".jpg";
} else if(socketChannel.socket().getLocalPort() == 10001){
outputFile = Config.HOME_PATH_SERVER_2 + "receivedImage" + fileNameCounter + ".jpg";
}
Path path = Paths.get(outputFile);
FileChannel fileChannel = FileChannel.open(path,
EnumSet.of(StandardOpenOption.CREATE,
StandardOpenOption.TRUNCATE_EXISTING,
StandardOpenOption.WRITE));
int numRead = 0;
int counter = 0;
readBuffer.clear();
socketChannel.read(readBuffer);
readBuffer.flip();
checkSum = readBuffer.getLong();
System.out.println("Server erwartet ein Bild der Groesse: " + checkSum);
readBuffer.limit((int)checkSum+8);
fileChannel.write(readBuffer);
fileChannel.close();
if(readBuffer.hasRemaining()){
System.out.println("Ist noch was im ReadBuffer!");
}
prepareWriteBuffer(checkSum);
System.out.println("NIO_SERVER: Received image.");
sendReceiveStatus = SendReceiveStatusServer.SEND_CHECKSUM_IMAGE;
对于第一个图像,一切正常。发送方的文件计数器增加,发送方尝试发送下一张图像。 接收方现在得到:下一张图像的大小:例如。 1591323337052742968 怎么了?
答案 0 :(得分:0)
尝试一下:
在客户端(发送)端
ByteBuffer
(ByteBuffer.putLong
)。ByteBuffer
(ByteBuffer.put(byte[])
)。ByteBuffer.flip
。ByteBuffer
写入套接字通道。在服务器(接收)端
ByteBuffer
。ByteBuffer.flip
。ByteBuffer
(ByteBuffer.getLong
)中获取图像长度。ByteBuffer
(ByteBuffer.get(byte[])
)中读取图像。