【问题标题】:Java socket getting only NUL characters after writing byte arrayJava套接字在写入字节数组后仅获得NUL字符
【发布时间】:2022-01-03 11:33:59
【问题描述】:

我正在尝试通过byte 数组将文件以块的形式从客户端发送到服务器。我正在使用ObjectInputStream。写入有效并且文件大小匹配,但是当我打开文件时,我只得到一个空白文本文件(在 IDE 中打开时,显示 NUL,NUL,NUL...)。

服务器代码:

try(
    FileOutputStream fileOut = new FileOutputStream(file);
    ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
    ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
){
byte[] arr = new byte[chunkSize];
try {
    int len = 0;
    long bytesRead = 0;

    byte[] bytes = new byte[chunkSize];
    int chunkNo = 1;


    while(true)
    {
        len = in.read(bytes,0, chunkSize);
        System.out.println();
        if(len < 0)
            break;

        fileOut.write(arr, 0, len);

        bytesRead += len;

        out.writeObject(Server.CHUNK_ACKNOWLEDGE_MSG);
        String ackReply = (String) in.readObject();

        if(ackReply.equalsIgnoreCase((Server.UPLOAD_ACKNOWLEDGE_RECEIVE_TIMEOUT_MSG))){
            if(Server.DEBUG)
                System.out.println(fileName + " send timeout.");

            deleteFile();
            break;
        }else if (ackReply.equalsIgnoreCase(Server.UPLOAD_COMPLETE_MSG)){
            if(bytesRead != fileSize){

                System.out.println(fileName + " File size mismatch");
                deleteFile();
                break;
            }else{
                System.out.println( fileName + " File written");
                break;
            }
        }
    
    }
}catch (IOException ioe){
    if(Server.DEBUG)
        ioe.printStackTrace();
}

客户端代码:

 try(
        FileInputStream fileInput = new FileInputStream(file);
        ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
        ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
    ){
        byte[] arr = new byte[chunkSize];
        try {
            int len = 0;
            int bytesRead = 0;
            int chunkCount = 1;
            while((len = fileInput.read(arr, 0, chunkSize)) != -1)
            {
                out.write(arr, 0, len);
                out.flush();

                bytesRead += len;

                }
                    try {
                        System.out.println("wait ack");

                        socket.setSoTimeout(timeout);
                        String ack = (String) in.readObject();
                        System.out.println(ack);

                        if(bytesRead >= fileSize){
                            out.writeObject(Server.UPLOAD_COMPLETE_MSG);
                            System.out.println(Server.UPLOAD_COMPLETE_MSG);
                            break;
                        }else{
                            out.writeObject(Server.CHUNK_ACKNOWLEDGE_MSG);
                        }
                    }catch (SocketTimeoutException e){
                        out.writeObject(Server.UPLOAD_ACKNOWLEDGE_RECEIVE_TIMEOUT_MSG);
                        System.out.println(Server.UPLOAD_ACKNOWLEDGE_RECEIVE_TIMEOUT_MSG);

                        break;
                    }finally {
                        socket.setSoTimeout(0);
                    }
                }
            }

        }catch (IOException ioe){
            ioe.printStackTrace();
        }
    }catch (FileNotFoundException fnfe){
        System.out.println("No such file: " + fileName);
    }catch (Exception e){
        e.printStackTrace();
    }
    finally {
        try {
            socket.close();
        }catch (Exception ex){
            ex.printStackTrace();
        }
    }

我尝试将byte 数组写入客户端的另一个文件,副本是相同的。所以问题一定是在通过套接字发送数据期间。

Server.X_MSG 只是一个常量字符串。我不知道在同一个ObjectInputStream 上混合readobject()read(bytearray) 是否会导致任何问题。

【问题讨论】:

  • 我不懂try( 的语法,没见过。您使用的是哪个 Java 版本?
  • @kiner_shah 这是资源语法的正常尝试。它应该相当于用 finally 语句关闭它们。

标签: java sockets networking inputstream objectinputstream


【解决方案1】:

也许是因为fileOut.write(arr, 0, len); 使用arrlen = in.read(bytes,0, chunkSize); 使用bytes?它们不是同一个数组。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-04
    • 1970-01-01
    • 2018-05-20
    • 1970-01-01
    • 2021-12-10
    • 1970-01-01
    • 2023-04-10
    • 2011-11-07
    相关资源
    最近更新 更多