Home > Software design >  Java socket getting only NUL characters after writing byte array
Java socket getting only NUL characters after writing byte array

Time:11-25

I'm trying to send files in chunks from a client to server via byte arrays. I'm using ObjectInputStream. The write works and the filesize matches but when I open the file, I only get a blank textfile(which when opened in a IDE, shows NUL,NUL,NUL...).

Server code:

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();
}

Client code:

 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();
        }
    }

I tried writing the byte array to another file on the client side and the copy was identical. So the problem must be during sending data over socket.

The Server.X_MSG is just a constant string. I don't know if mixing readobject() and read(bytearray) on the same ObjectInputStream causes any issues though.

CodePudding user response:

Maybe it's because fileOut.write(arr, 0, len); use arr and len = in.read(bytes,0, chunkSize); use bytes? They are not the same array.

  • Related