Control java application on the outside

Asked

Viewed 211 times

2

I created a java SE application that runs in the background, without swing or anything... But how do I control this application? for example, in python applications we can execute commands (python something.py -c command1 -u command2), so I can have a control over the application, as I would with my java application?

And can I control the application through another java application? For example: a swing panel with options to pause the thread of the controlled application, continue, change configuration information, etc.

The application will run on Linux only.

1 answer

1


a solution for your case may be the use of the Serversocket class. In your background application you can leave as a server listening to a specific port. And in another application you can send commands through the socket, with this you can manipulate your server application knowing only the IP and port;

Server side

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.BufferedReader;  
import java.io.InputStreamReader;  

public class Server {  

    public static void main(String[] args) {  

        ServerSocket serv=null;  // Declara o socket 
        Socket s= null; // Socket de comunicação  

        BufferedReader entrada=null; //Leitor para a entrada de dados  

        try{  
            //Cria o ServerSocket na porta 9000   
            serv = new ServerSocket(9000);  
            //Aguarda uma conexão na porta especificada e cria   
            s = serv.accept();  
            //Cria um BufferedReader para o canal da stream de entrada de dados do socket  
            entrada = new BufferedReader(new InputStreamReader(s.getInputStream()));  

            //Aguarda por algum dado e imprime a linha recebida quando recebe  
            System.out.println(entrada.readLine());      

        }catch(IOException e){  
            System.out.println("Falha ao criar socket.");  
        }finally{  
            try{  
                //Encerra o socket  
                s.close();  
                //Encerra o ServerSocket  
                serv.close();  
            }catch(IOException e){  
            
            }  
        }  

}  

}

Client side

import java.io.IOException;  
import java.io.PrintStream;  
import java.net.Socket;  

public class Client {  

public static void main(String[] args){           
    Socket mSocket = null;  
    PrintStream mPrintStream = null;  

    try{  

    int x = 20;  
    mSocket = new Socket("<_ENDEREÇO_IP_>",9000);  
    mPrintStream = new PrintStream(mSocket.getOutputStream());  
    mPrintStream.println(x);  
    
    }catch(IOException e){                
    System.out.println("Falha ao enviar dados." + e.getMessage());  

    }finally{  
        try{  
            //Fecha o socket  
            mSocket.close();  
        }catch(IOException e){}  

    }  
}  
} 
  • There is no other way, no network use?

  • You can use server/client socket on the same machine, using loopback ip 127.0.0.1

  • I can’t think of anything right now

Browser other questions tagged

You are not signed in. Login or sign up in order to post.