0

So I have a Java Client server application. When I am trying to send a single file to the server (using SSLSockets) it works fine (because the connection is closed straight after the byte array of the file is sent). Although, as soon as I try to send a second file inputStream reader loop hangs (probably receives more bytes after the transfer? IDK). I ll write the block of code slightly modified so the structures are visible underneath.

client

OutputStream = sslsocket.getOutputStream();

public void sendFile(Path pathOfFile){
    byte[] bytesToSend = Files.readAllBytes(pathOfFile);
    out.write(bytesToSend);
    out.flush();
}

server

public void receiver(File file){
    InputStream in = sslsocket.getInputStream();

    byte[] fileBytes = new byte[sslsocket.getReceiveBufferSize()];
    int temp = 0;
    while ((temp = in.read(fileBytes)) > 0) { // stalls after a couple of reads
        file.write(fileBytes , 0, temp);
    }
}

so if I close the connection straight after the sendFile method everything works fine. But if I even run the client on debug and breakpoint straight after the sendFile method the server just freezes on the read() method of the while loop. What is going on??

Rakim
  • 1,087
  • 9
  • 21
  • 40

1 Answers1

0

doesn't recognize the end of file

There is no end of file to recognize unless you close the connection.

You're reading the socket until end of stream, which only happens when the peer closes the connection. If you don't close the connection, the receiver will block.

user207421
  • 305,947
  • 44
  • 307
  • 483