0

I am trying to send a file from client to server and then the server response the client. The server has received the file, but the client doesn't receive the response.

This is my Main and client code:

public static void main(String[] args) throws IOException {
    startServer();
    startSender();
}

public static void startSender() {
    (new Thread() {
        @Override
        public void run() {
            try {
                Socket client = new Socket("localhost", 10000);
                ObjectOutputStream oos = new ObjectOutputStream(client.getOutputStream());
                ObjectInputStream ois = new ObjectInputStream(client.getInputStream());

                //sent a file
                FileInputStream fis = new FileInputStream(new File("C:\\Users\\Administrator\\Desktop\\constructor.jpg"));
                byte[] buffer = new byte[1024];
                int length = 0;
                while((length = fis.read(buffer, 0, buffer.length)) > 0){
                    oos.write(buffer, 0, length);
                    oos.flush();
                }
                //response
                String response = (String) ois.readObject();


            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }

        }
}).start();

}

Server:

public static void startServer() {
    (new Thread() {
        @Override
        public void run() {
            ServerSocket server = null;
            Socket client = null;
            ObjectInputStream ois = null;
            ObjectOutputStream oos = null;

                try {
                    server = new ServerSocket(10000);
                    client = server.accept();
                    System.out.println("Connected!");
                    ois = new ObjectInputStream(client.getInputStream());
                    oos = new ObjectOutputStream(client.getOutputStream());

                    //receive the file
                    FileOutputStream fos = new FileOutputStream(new File("E:\\Java\\server\\constructor1.jpg"));
                    byte[] sendBytes = new byte[1024];
                    while(true) {
                        int read =0;
                        read = ois.read(sendBytes);
                        if(read == -1)
                            break;
                        fos.write(sendBytes, 0, read);
                        fos.flush();
                    }
                    //response
                    oos.writeObject("true");
                    oos.flush();

                } catch (IOException e1) {
                    e1.printStackTrace();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
        }
    }).start();
}

What's wrong?

2 Answers2

-1

You have to create the ObjectOutputStream before the ObjectInputStream. See below:

ObjectOutputStream oos = new ObjectOutputStream(client.getOutputStream());
ObjectInputStream ois = new ObjectInputStream(client.getInputStream());

This should fix your problem with it outputting TEST.

baseman101
  • 352
  • 1
  • 4
  • 19
  • Source: http://stackoverflow.com/questions/12642066/objectoutputstream-hanging-forever – baseman101 Jan 19 '16 at 02:45
  • It doesn't output TEST, and he is creating the streams in that order in the sender, which is sufficient to overcome the problem mentioned in the linked question, which is not this problem, otherwise he wouldn't have said the server received the file. Do read he question. – user207421 Jan 19 '16 at 04:36
  • @EJP before you dislike my answer, please be aware that my answer was posted before the OP made revisions to his question. http://stackoverflow.com/posts/34867416/revisions – baseman101 Jan 19 '16 at 04:50
  • @EJP Not only was my answer correct, but I even tested the code. Completely works. Read the question again in the context that I read it, and do consider removing your dislike. Thank you. – baseman101 Jan 19 '16 at 04:53
  • @EJP Sorry!!! I did make revisions to my question before. And this post owner did solve parts of my question. So please remove the dislike. And thanks for your answer, now my problem is solved. – lifeisamoive Jan 19 '16 at 05:33
  • @lifeisamoive Completely alright. Would you mind accepting a post answer you like? Also, welcome to the community! – baseman101 Jan 19 '16 at 05:35
  • 1. There is no evidence in the edits that the OP's code ever output TEST. In any case the fact remains that your answer addresses something not present in the question as it is now. 2. Nobody said these two lines of code don't work: I said they don't *solve the problem.* 3. You have no business speculating about who downvoted this. It's a secret ballot. – user207421 Jan 19 '16 at 06:28
  • @EJP Is it really necessary to keep arguing this? 1. Yes, there is evidence. The client sends the word TEST to the server, and it never gets outputted on the server console indicated by the line oos.writeObject("TEST");. 2. These lines of code did solve the problem before the user edited it (he wrote that the server doesn't receive the file, which it does after my edits). 3. That's my bad, I shouldn't base my assumptions off of that. – baseman101 Jan 19 '16 at 13:10
-1

Your receiver is reading the stream until end of stream, but the sender isn't closing the socket, so end of stream never happens. You will need to change your sending protocol in some way:

  • shutdown the socket for output after sending the file, but then you can only send one file
  • send the file length ahead of the file, read it at he receiver, and then only read exactly that many bytes at the receiver.
user207421
  • 305,947
  • 44
  • 307
  • 483