Java RMI (Remote Method Invocation) might do.
(No longer the rmic
compiler is needed - should you still find such an obsolete sample.)
The newer version of RMI uses interfaces, an implementaion class and a port, for discovery.
I used it to implement a single instance class: if a second instance of an application is started, it discovers its class on a port, possible searches another port if already occupied, and delegates its command line to the first instance of itself.
Without administrator rights could be tickly.
Client application:
public static void main(String[] args) {
try {
DictServer server = (DictServer)
Naming.lookup("//localhost:1024/dictserver");
String[] words = server.getWords();
System.out.println(Arrays.toString(words));
} catch (RemoteException | MalformedURLException
| NotBoundException ex) {
ex.printStackTrace();
}
}
Common interface:
public interface DictServer extends Remote {
public String[] getWords() throws RemoteException;
}
Server application:
public class DictServerImpl extends UnicastRemoteObject implements DictServer {
public DictServerImpl() throws RemoteException {
}
@Override
public String[] getWords() throws RemoteException {
return new String[] { "unu", "du", "tri", "kvar", "kvin", "ses" };
}
}
public class Main {
public static void main(String[] args) {
try {
int port = 1024;
Registry registry = LocateRegistry.createRegistry(port);
registry.rebind("dictserver", new DictServerImpl());
} catch (RemoteException ex) {
ex.printStackTrace();
}
}
}
Here used port 1024 and assuming running on same machine.