2

I have a simple JavaFX app when I start the app it extablishes a connection with a server via Socket and listen for data from this server continously. Now I have a created a Button that should send dat to server.

This is how I did it

Controller:

 @FXML
private Button update;


 public void askOnline() throws UnknownHostException, IOException{
Socket socket = new Socket("127.0.0.1", 7000);
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
dos.writeUTF("AreYouOnline");
  dos.flush();
socket.close();
}

Main:

@Override
public void start(Stage primaryStage) throws IOException {
    this.primaryStage = primaryStage;
    this.primaryStage.setTitle("Thypheon Application");
    initRootLayout();
    controller2 = initDesign();
    addBot();
    con = new Connection(controller2);
    Thread t = new Thread(con);
    t.start();

}

Thread:

public class Connection implements Runnable {
 String result;
 Controller controller;
 Socket socket;
 DataOutputStream dos;

   public Connection(Controller controller) {
       this.controller = controller;
   }

@Override
public void run() {
   try {
        socket = new Socket("127.0.0.1", 7000);
        System.out.println("Connessooo");
         DataInputStream dis = new DataInputStream(socket.getInputStream());
         dos = new DataOutputStream(socket.getOutputStream());
         SeiOnline so = new SeiOnline(dos);
         Thread t = new Thread(so);
         t.start();

         while(true){
         result = dis.readUTF();
         System.out.println(result);              
           }


         }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

In this way I create a new Socket connection between Client and Server just to send data. But my question is this: Is possible to retrieve the already existing Socket connection (running in the thread) and use it in order to send data to server, instead of create a new Socket Connection?

Alessio Trecani
  • 713
  • 2
  • 10
  • 25
  • Have you tried to create a getSocket function inside the Connection Class and retrieve it from your controller? – Briesanji Feb 18 '16 at 14:19
  • Can you explain what the protocol between your client and the server is? Is it a request-response protocol, so you only expect data from the server in response to first sending data? If so, I would not use an infinite loop but would submit individual tasks to an executor for each request. Or are you expecting data coming in at other times? In that case, I would use two sockets in the `Connection` class and provide a method there for sending (over one socket) and use the other for receiving. – James_D Feb 18 '16 at 14:40
  • The socket created inside the thread Must running infinitely receiving data from server.. I don't have to modify this part. The only thing that I would like to do is to send data to the server without creating a new socket connection (If it is possible) – Alessio Trecani Feb 18 '16 at 14:43
  • So you are saying it is the second of the two options in my previous question? Is that right? Then I *think* you need a different socket to send the data, else you will potentially run into threading issues, as you need one thread to be dedicated to receiving data. But there's no need to create a new socket for every send, just create a second socket in the `Connection` class and have a `sendData(...)` method that uses it. – James_D Feb 18 '16 at 14:59
  • Ok appreciated..Can you please post this as answer.. I will check as ANSWERED.. – Alessio Trecani Feb 18 '16 at 16:04

0 Answers0