如何调试"Java NIO Socket Channel mismatch in size of sent and received bytes"?



请看这个问题。

这正是我正在寻找的,但我还没有达到它。我正在通过套接字通道发送多个图像。我尝试将图像大小附加到(清除的 ByteBuffer)中,然后将图像数据附加到相同的缓冲区中(缓冲区大小足够大!

在接收端,首先图像大小将由 ByteBuffer.read(...) 读取,然后由 ByteBuffer.flip(

) 和 ByteBuffer.getLong() 读取(例如 imageData = readBuffer.getLong()) 要读取以下图像数据,我是否执行 ByteBuffer.comact()?我如何意识到将读取图像数据的确切字节数?

不,我有以下几点:在发送端:

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怎么了?

试试这个:
在客户端(发送)端

  1. 将图像的大小写入ByteBufferByteBuffer.putLong )。
  2. 将图像数据写入同一ByteBufferByteBuffer.put(byte[]) )。
  3. 呼叫ByteBuffer.flip .
  4. ByteBuffer写入套接字通道。

在服务器(接收)端

  1. 从套接字通道读取到ByteBuffer
  2. 呼叫ByteBuffer.flip
  3. ByteBuffer中获取图像长度(ByteBuffer.getLong)。
  4. 分配图像字节[](使用图像大小)。
  5. ByteBuffer中读取图像(ByteBuffer.get(byte[]))。

相关内容

最新更新