Hello!

How in Java to close a socket when a button is pressed? Made applications client server. It is necessary when you click on the button on the server to disable a specific client. I tried to use the flag. The problem is that in threads the application waits for text from the client, and after it accepts the text, it starts checking the flag and disconnects the socket ... And I need some interruption to do this ..

I still work, it does not work. Maybe today there is someone who knows how to do it ... Thank you.

  • I have not had time to work with sockets in Java yet, and maybe I don’t understand something, but doesn't Thread.stop () stop the stream? - dirtez
  • I do not need to stop the stream, I need to interrupt the operation of the String method input = in.readLine (); in the stream, and then decide to close the socket or not ... - Alerr

2 answers 2

The Socket class implements the Closable interface. Why is calling the close () method not appropriate?)

    setSoTimeout(); the wait time by calling the setSoTimeout(); method setSoTimeout();
    as soon as you get the socket, call it immediately

    • I need to somehow interrupt this String input = in.readLine (); The application is waiting for the line here ... You need to re-throw it chtoli ... - Alerr 2:19