Skip to content
Advertisement

Tag: serversocket

Java client hangs if I open two clients at once

I’m trying to build a chat-like thing, and the idea is that whenever someone types, it gets sent and printed on every client connected. If I only open the server and one client, everything runs smoothly — whenever I send something from the client, it goes to the server and then it comes back and gets printed on the client.

Proper way to close an AutoCloseable

What is the most reliable pattern to follow when closing an OutputStream, ServerSocket, or other object that implements the AutoCloseable interface? Should I use try-catch-finally? Or a shutdown hook. Answer The correct way to use an AutoCloseable instance is with a try-with-resources block, so the resource is reliably closed even if an exception is thrown. Like this: You can also

Advertisement