It is necessary that the client is constantly in standby mode of entering commands from the user. These commands are then sent to the server .

Found many examples of client-server communication (Java).
For example this: The simplest client-server application

In this example, the client is waiting for input from the console. How do I wait for input from a control component, such as TextField (JavaFX)?

There is an idea, to save the text from TextField in a certain variable. The client will check this variable for changes once a second. When changing, send a string to the server .

Or handle the ActionEvent event for TextField . Is it possible to determine in the client the function that will receive the string and send it to the server ? This function, I suppose, should be called from an ActionEvent handler.

The client class runs in a separate thread relative to the main application, so as not to hang the user interface. Is there a way to put the client in standby mode and turn it on only if you need to send a message to the server ?

  • send something to the server after the button is pressed after entering? - michael_best pm
  • @michael_best Yes, what should I write in the event handler for an ActionEvent event? Is it possible to determine in the Client the function that will receive the string and send it to the Server. This function, I suppose, should be called from the ActionEvent handler - Evgeniy Tkachenko 4:19 pm
  • I think it makes sense to write onClickEvent . And it already has String s = whereStringIs.getText() . And then s.sendSomeWhere(); - michael_best pm
  • @michael_best The trick is that when using the System.in stream, you do not need to think about what the Client should do while waiting. System.in is blocking and the Client is automatically in standby mode. Roughly speaking, how to change the input stream, what would be with TextField and not from the console? - Evgeniy Tkachenko
  • How do you determine when a user stops typing something in a field? - michael_best

1 answer 1

You can organize the suspension and resumption of the client stream using the functions wait () , notify () . Here is the minimum working part of the client class code:

 public class MyClient extends Thread{ private DataOutputStream ostream ; private DataInputStream istream; private Socket socket; private boolean isPause; private boolean isContinue; private String command; private final String HOST; private final int PORT; private final int BUFF_SIZE=64; private final GuiElement guiElement; public MyClient(String HOST, int PORT,GuiElement guiElement) { this.HOST=HOST; this.PORT=PORT; this.guiElement=guiElement; this.isPause=true; this.isContinue=true; this.setDaemon(true); try { socket = new Socket(HOST, PORT); ostream = new DataOutputStream(socket.getOutputStream()); istream = new DataInputStream(socket.getInputStream()); } catch (Exception e) { isPause=false; isContinue=false; } start(); } public void run() { while(isContinue) { //-----waiting synchronized(this) { while(isPause) { try { wait(); } catch (InterruptedException e) { closeSocket(); } } } //-----sending command sendCommand(); } closeSocket(); } //-----invoked from the class that creates this Client thread //-----when the user has clicked the enter button on the input field synchronized public void setCommand(String command) { this.command=command; isPause=false; notify(); } private void sendCommand() { try { ostream.write(command.getBytes()); ostream.flush(); byte buf[] = new byte[BUFF_SIZE]; int r; r = istream.read(buf); String data = new String(buf, 0, r); } catch (Exception e) { closeSocket(); } Platform.runLater(() -> guiElement.print(data)); /* display a message on a GUI element */ isPause=true; } private void closeSocket() { try { if(socket.isClosed())return; else socket.close(); } catch (Exception e) { isPause=false; isContinue=false; } } } 

After starting the thread, the client is in standby mode. When called from the outside (from the main thread that accesses the client, for example, the client.setCommand (some message)) the current command is set to the setCommand () function and is notified when the wait (notify ()) is stopped. Next, the stream exits the while (isPause) {...} block to send a command to the server by itself starting the sendCommand () function. After the execution of the sendCommand () function, the stream goes back to the standby mode, getting into the while (isPause) block {...}