javasocketsjakarta-eeserialization

How to connect a JavaSE client to JavaEE server and send a Serializable object?


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 poin

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 which 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:


Solution

  • Not sure if you have explored using REST services to address your problem, so I would recommend creating REST service. There are some great resources on the net. Here is one link for your reference.

    https://spring.io/guides/gs/rest-service/

    Doing this you will be able to achieve the following with ease.

    1. Leverage HTTP to make your iOS and Android interact with your application on the Server. This does not just mean that you use HTTP only for a transport layer request, but you will be able to do a comprehensive content-negotiation using the protocol, and your server will control the negotiation

    2. Your resources on the server will have a flexible representation of your choice.

    There are other benefits of this approach to the one you suggested, but I leave it up to you to explore. I am listing a few links below for your reference.

    A tutorial on REST services https://www.tutorialspoint.com/restful/index.htm

    To help you avoid comparison of this approach to SOAP SOAP vs REST (differences)

    Multitier architecture for RESTful web services https://www.ibm.com/developerworks/library/wa-aj-multitier/