I am trying to build an application with a JavaEE server and a client in JavaSE (and possibly clients running on Android and IOS). I would like the clients to be able to send serializable objects, or similar , to the server. The server should then do some action and send a response to the client. I am not sure where to start but I have tried to outline my intention below.
I am not looking for the full code to make this happen rather some pointers and a confirmation (or refutation) whether I am on the right track. And perhaps a starting point :)
I have a serializable class, like this:
public class TestMessage implements Serializable {
public String str;
}
On the client side I would like to do something like this:
try {
InetAddress address = InetAddress.getByName(
new URL("http://127.0.0.1/hello").getHost());
Socket connection = new Socket(address, 46588);
connection.setSoTimeout(5000);
try (ObjectOutputStream outgoing = new ObjectOutputStream(
new BufferedOutputStream(connection.getOutputStream()
))) {
TestMessage test = new TestMessage();
test.str = "Hello World";
outgoing.writeObject(test);
outgoing.flush();
}
} catch (UnknownHostException ex) {
System.out.println("Unknown Host...");
} catch (IOException ex) {
System.out.println("i/o exception...");
}
I was thinking that I could use a @ServerEndpoint Bean but this creates a WebSocket wich I don't seem to be able to connect to using Sockets. I suspect I need to implements the HTTP stuff in Sockets but then how do I send my objects?
I was also considering using a @Startup Bean and open a ServerSocket but then it seems I would have to handle the threads myself and that would sort of defeat the purpose of using JavaEE. It sort of boils down to:
- How can I start a JavaEE Bean that multiple JavaSE clients can connect to simultaneously?
- How can I send serializable objects, or similar like JSON, back and forth between the JavaEE server and the JavaSE clients?