0

So I'm creating a chat room, and the idea is that the servers and clients are multithreaded. So when a client sends a message, it goes to the server, the server puts that into a file with the rest of the chat log, and then every little while a client will (on the second thread) connect to the server (on the second thread) for the log. From there, the server sends the file and the client puts that into the messages box. Here's a diagram:

Diagram

If you follow along with the arrows, you'll hopefully figure it out. :) Anyway, so the Thread one part is perfect, and it works fine. However, once the thread one ends (once the client's message is in the server's file), the second thread starts, and that's when the bad stuff starts as well.

Right away, I get an EOFException from the client (it doesn't say where), and I get a BindException from the server (which I'm assuming is somehow because of the error on the client's side).

Here's the code from thread 2 of the server:

System.out.println("Waiting for a client...");
// Start a server
ServerSocket server = new ServerSocket(3210);
// Listen for anyone at that port
Socket socket = server.accept();
System.out.println("A client has connected!");
// Open the output stream
DataOutputStream outputStream = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
// Get the file and convert it to a byte array
Path path = Paths.get("history.txt");
byte[] bytes = Files.readAllBytes(path);
// Send the size of the file in bytes
outputStream.writeInt(bytes.length);
// Send the file
outputStream.write(bytes);
// Close everything
socket.close();
outputStream.close();
server.close();

And the code from Thread 2 of the client:

// Start socket
Socket socket = new Socket("DESKTOP-FUR3UF6", 3210);
// Get input stream
DataInputStream inputStream = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
// Get amount of bytes in file
int length = inputStream.readInt();
if (length > 0 && allMessagesTextBox.getText() != null) {
    // Get the bytes from the file and put that out to the message box
    byte[] bytes = new byte[length];
    inputStream.readFully(bytes, 0, bytes.length);
    String data = new String(bytes, "UTF-8");
    allMessagesTextBox.setMessage(data);
}
else {
    // Put this message on the message box
    allMessagesTextBox.setMessage("Connecting...");
}
// Close socket and input stream
inputStream.close();
socket.close();

Both the client and the server code are in try/catch blocks just so you know.

Part of my problem however is that I don't fully understand the EOFException. It occurs when an end of file or end of stream has been reached unexpectedly during input, according to Oracle, but I don't see why that would happen in my code.

Can someone please enlighten me on why this is going wrong, and how to fix it? Thank you!!

Community
  • 1
  • 1
Jackson
  • 7
  • 1
  • In most cases (at least from what I have seen) EOFExceptions tend to signify an abnormal disconnect has occurred. Have you considered of this case? – akortex Jun 29 '18 at 18:48
  • No they don't. They signal that a *normal* disconnect has occurred. @Aris – user207421 Jun 29 '18 at 23:40
  • If you're getting a `BindException` it means the server didn't start, or is already started. In the first case it is pin toes to even run the client. Why are you displaying 'connecting ...' when you aren't? And exceptions *do 'say where'. You just aren't displaying properly. – user207421 Jun 29 '18 at 23:42

0 Answers0