skip to Main Content

I am now learning Docker and I want to create 2 simple client-server java containers in which the client sends an order and the server responds with a confirmation message that the order has been received.

What is the protocol that I should use to send a message from the client to the server?

Client-Server sample code

public class Client {

    private static int counter = 0;
    private final String name;
    private final int id;
    private final Server server;
    private Order order;

    public Client(String name, @NotNull Server server) {
            this.name = name;
            this.id= ++counter;
            this.server = server;
        }
    
    public void submitOrder(Order order) {
            this.order = order;
    
            if (server!= null) {
                server.receiveOrder(this);
            } else {
                throw new UnsupportedOperationException("Client did not select a preferred server.");
            }
        }
}

public class Server {

    private String serverName;

    public Server(String serverName) {
           this.serverName = serverName;
        }
    
    public void receiveOrder(Client client) {
            sendConfirmationEmailToClient();
        }
}

Is there any sample code that I could review?

2

Answers


  1. Client/server communication can be done with different protocols, but the most common using HTTP.

    You can also use a WebSocket to have real-time communication.

    Login or Signup to reply.
  2. While you could use TCP or UDP and build your own protocol on top of that I believe you should go with something easy to handle, possibly already existing so you can focus on the docker specialities. Otherwise the learning curve may be too steep.

    I suggest you use HTTP and then just need HTTP server and client – presumably in Java as you raised the question here. So how about

    For Tomcat you can even use already existing containers as a start.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search