1

i have a client-server program in java where the client sends a request to the server and the server responds by sending a file. The connection with the server is made correctly and the first request sent by the client is correctly interpreted by the server.

At this point the server reads in a specific file which seems to be read well since the write sends a correct number of bytes and a string (tested with a print of the number of bytes and of the string).

After sending the first message, the client waits to receive the file but as soon as it receives the message, the read returns -1 as if it had read an EOF and I can't understand the reason, how to solve it?

method on the server when to send the file

private void sendFile(Socket client, String path, DataOutputStream writer) {
        File file = new File(path);
        byte[] bytes = new byte[1024];
        InputStream in = null;
        try {
            in = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        int count;
        try {
            while ((count = in.read(bytes)) > 0) {
                System.out.println("nBytes " + count);
                String tmp = new String(bytes);
                System.out.println(tmp);

                //i'm reading from a json file this string
                //[{"username":"user"},{"score":0},{"friendList":["us1","us2"]},{"password":"password"}]
                //and it is printed correctly

                writer.write(bytes, 0, count);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {
            in.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

client-side method

public void getFile (String username) {
        // first of all connects correctly with the server and sends a message, the message is received correctly by the server, after it waits for a reply message

        Socket socket = connect2server();
        DataInputStream reader = null;
        BufferedWriter writer = null;


        try {
            reader = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
            writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        //send message to the server using writer 

        OutputStream out = null;
        String file = username+".json";
        try {
            out = new FileOutputStream(file);
        } catch (FileNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        byte[] bytes = new byte[1024];

        int count;
        try {
            while ((count = reader.read(bytes)) >= 0) {
                System.out.println("nByte " + count);
                out.write(bytes, 0, count);
            }

            // the client is waiting correctly on the read but as soon as it receives a message, the read returns -1 as if it had only read an EOF and not the whole message

            System.out.println(count);
        } catch (IOException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }

        try {
            out.close();
            reader.close();
            writer.close();
            socket.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   
    }
JayJona
  • 469
  • 1
  • 16
  • 41

2 Answers2

1

In your sendFile() writer is not closed. You need to flush() and then close() the writer in order to guarantee the output is created. May be this will solve your problem

Klaus
  • 1,641
  • 1
  • 10
  • 22
1

With regards to write, you are not closing/flushing the output stream which in turns not releasing any resources associated with the stream. You have two options:

  1. Close the output stream, which calls its flush method, and then calls the close method of its underlying output stream.
  2. Flush the output stream buffer. Buffering is mainely done to improve the I/O performance.

From the doc, the general contract of read()

Reads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown. A subclass must provide an implementation of this method.

Returns: the next byte of data, or -1 if the end of the stream is reached.

fabfas
  • 2,200
  • 1
  • 21
  • 21