0

Currently trying to have the server send a file to the client.

I'm able to see the file being created in the client directory, but it won't send all the bytes. Sometimes its 90% of the bytes, and sometimes its not even 10 bytes.

I keep getting a message from the terminal window saying: "malformed input around byte...."

Here's the Server code for this section:

String cMessage;
DataInputStream input= new DataInputStream( remote_socket.getInputStream() );   

File filename = new File(cMessage);

try {
    input = new DataInputStream(new FileInputStream(filename));
    DataOutputStream out = new DataOutputStream(remote_socket.getOutputStream());

    byte[] buffer = new byte[(int) filename.length()];
    int count;
    while((count = input.read(buffer)) >0){
        out.write(buffer, 0, count);
    }

    System.out.println("Sending " + cMessage + " that is " + filename.length());
    out.flush();

} catch (IOException e) {
    System.out.println(e + "......");
    e.printStackTrace();
}

Output for the Server when running looks like: Sending test.png that is 723915

Here's the Client for this section:

try {
    BufferedReader reader= new BufferedReader( 
         new InputStreamReader(System.in) );
    String message;
    BufferedReader fileMessageReader= new BufferedReader( 
             new InputStreamReader(System.in) );
    String fileMessage;

    DataOutputStream output= new DataOutputStream(this.client_socket.getOutputStream() );
    DataInputStream input = new DataInputStream(this.client_socket.getInputStream());

    while(true) {
        while( (message = reader.readLine()) != null ) {

            System.out.println("1. For a text message. 2. For a file");

            if (message.equals("2")) {

                System.out.println("Name of file?");
                fileMessage = fileMessageReader.readLine();
                output.writeUTF(fileMessage);

                try {

                    File filename = new File(fileMessage);
                    DataOutputStream fileOutput = new DataOutputStream(new FileOutputStream(filename));

                    byte[] buffer = new byte[1024];
                    int count;
                    while((count = input.read(buffer)) >0){
                        fileOutput.write(buffer, 0, count);
                    }

                    fileOutput.flush();
                    fileOutput.close();

                } catch (IOException e) {
                    System.out.println(e + "...");
                }

            } else {    
                output.writeUTF( message );
            }

        }

    }

    } catch (Exception e) {
    System.out.println(e.getMessage());
    }
Karth
  • 75
  • 7
  • Try `output.writeUTF(fileMessage.getBytes("UTF-8"));` and `output.writeUTF(message.getBytes("UTF-8"));` – Jay Smith May 31 '17 at 05:46
  • The method writeUTF(String) in the type DataOutputStream is not applicable for the arguments (byte[]) :( – Karth May 31 '17 at 05:51

1 Answers1

0

while ((c = input.read()) != -1) { output.write(c); } This piece is killing it. If you read javadoc carefully, rather than just checking method signature, you will see that read() reads a single byte from the input (it returns int only to be able to signal end of stream with -1). So you read byte as int, no big deal, but then you blindly write aforementioned int (4 bytes) into the output stream.

You should use read(byte[] b) instead and then write the whole array (or only part of it that was read in the last round).

Jan
  • 4,369
  • 14
  • 25
  • Just went ahead and updated the code. Might have misunderstood you, but have a look when you get a chance. Same error btw. – Karth May 31 '17 at 06:34
  • Hmmm, as per https://stackoverflow.com/questions/25897627/datainputstream-read-vs-datainputstream-readfully try to change `read()` to `readFully()`. – Jan May 31 '17 at 14:56