Java - Start Upload after Download

147 views Asked by At

My program just can do 1 transaction which is Downloading or Uploading but can't Downloading then Uploading

The Server:

    ServerSocket serverSocket = new ServerSocket(4444);
    Socket socket = serverSocket.accept();

    /* GET */
    InputStream is = socket.getInputStream();        
    BufferedOutputStream bos =  new BufferedOutputStream(new FileOutputStream("E:\\23c-dl.mkv"));


    int count;
    byte[] bytes = new byte[8192];

    while ((count = is.read(bytes)) > 0) {
        bos.write(bytes, 0, count);
        System.out.println("-->" + count);
    }

    System.out.println("DONE DOWNLOADING");


    /* SEND */
    BufferedInputStream fileInput = new BufferedInputStream(new FileInputStream(new File("E:\\23s.mkv")));
    BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream());

    int count2;
    byte[] bytes2 = new byte[8192];

    while ((count2 = fileInput.read(bytes2)) > 0) {
        out.write(bytes2, 0, count2);
        System.out.println("--> " + count2);
    }

    /* CLOSE */
    socket.close();
    serverSocket.close();

The Client

    Socket socket = new Socket("127.0.0.1", 4444);

    /* SEND */
    BufferedInputStream fileInput = new BufferedInputStream(new FileInputStream(new File("E:\\23c.mkv")));
    BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream());

    int count;        
    byte[] bytes = new byte[8192];

    while ((count = fileInput.read(bytes)) > 0) {
        out.write(bytes, 0, count);
        System.out.println("-->" + count);
    }
    System.out.println("DONE UPLOADING");

    /* GET */
    InputStream is = socket.getInputStream();        
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("E:\\23s-dl.mkv"));

    int count2; 
    byte[] bytes2 = new byte[8192];

    while ((count2 = is.read(bytes2)) > 0) {
        bos.write(bytes2, 0, count2);
        System.out.println("-->" + count2);
    }

    /* CLOSE */
    socket.close();

If I comment out The Server's Get and The Client's Send: Program works. Server can download the file.

If i comment out The Server's Send and The Client's Get: Program works. Client can download the file.

Not comment out anything: Only Server can download the file. What's the problem?

1

There are 1 answers

0
flaz14 On BEST ANSWER

This is because once you started reading from the input stream (on Server's side) your code will be blocked (until client will close its own output stream). To investigate this situation run your Server and then connect to it using telnet, like this:

telnet 127.0.0.1 4444

Type several lines with Enter press after each. And see that your Server will print --> for each string, and Server will not proceed further until you close telnet session.

So your Client should send some data to server. Then it should close output stream and reconnect to Server. At the same time Server should wait for the new connection. And then Client can download file from Server.

More generally, you can read socket in separate thread, so your main code will not hang up.

And the code (also I changed names of files slightly):

Server

    ServerSocket serverSocket = new ServerSocket(4444);
    Socket socket = serverSocket.accept();

    /* GET */
    InputStream is = socket.getInputStream();        
    BufferedOutputStream bos =  new BufferedOutputStream(new FileOutputStream("serverlog.txt"));


    int count;
    byte[] bytes = new byte[8192];

    while ((count = is.read(bytes)) > 0) {
        bos.write(bytes, 0, count);
        System.out.println("-->" + count);
    }

    System.out.println("DONE DOWNLOADING");
    bos.close();
    socket.close();


    /* SEND */
    socket = serverSocket.accept();
    BufferedInputStream fileInput = new BufferedInputStream(new FileInputStream(new File("server.txt")));
    BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream());

    int count2;
    byte[] bytes2 = new byte[8192];

    while ((count2 = fileInput.read(bytes2)) > 0) {
        out.write(bytes2, 0, count2);
        System.out.println("--> " + count2);
    }
    out.close();

    /* CLOSE */
    socket.close();
    serverSocket.close();

Client

    Socket socket = new Socket("127.0.0.1", 4444);

    /* SEND */
    BufferedInputStream fileInput = new BufferedInputStream(new FileInputStream(new File("client.txt")));
    BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream());
    int count;        
    byte[] bytes = new byte[8192];
    while ((count = fileInput.read(bytes)) > 0) {
    out.write(bytes, 0, count);
        System.out.println("-->" + count);
    }
    System.out.println("DONE UPLOADING");
    out.close();
    socket.close();

    /* GET */
    socket = new Socket("127.0.0.1", 4444);
    InputStream is = socket.getInputStream();        
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("clientlog.txt"));

    int count2; 
    byte[] bytes2 = new byte[8192];

    while ((count2 = is.read(bytes2)) > 0) {
        bos.write(bytes2, 0, count2);
        System.out.println("-->" + count2);
    }
    bos.close();

    /* CLOSE */
    socket.close();
}