I am writing in Java. It is necessary to create a client-server application with support for multiple clients. I would like to implement this using the observer pattern. The server is started separately, the client separately. It is necessary to transfer the server in the parameter to the client. If you understand something, then answer me. I just want to send messages from the server to clients the same as in the "observer" pattern.

для полного прикола добавлю код public class Client implements Observer { private String result = ""; private Subject server; public Client(Subject server){ this.server = server; server.registerObserver(this); } @Override public void update(String result) { this.result = result; } static void connectionToServer(){ try { Socket fromServer = new Socket("localhost",8189); BufferedReader in = new BufferedReader(new InputStreamReader(fromServer.getInputStream())); PrintWriter out = new PrintWriter(fromServer.getOutputStream(),true); BufferedReader inu = new BufferedReader(new InputStreamReader( System.in )); String fUser; String fServer; while((fUser = inu.readLine()) != null){ out.println(fUser); fServer = in.readLine(); System.out.println(fServer); if (fUser.equals("close")) break; } fromServer.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { connectionToServer(); } 


do not look at the connection. How do I create a customer in Maine? to transfer to it in parameter the server? I cannot create a server in the framework of this mein, since it is already running.

  • 2
    And what is the problem? If the template is not clear - in the same wiki a great example, incl. on java. If the essence is not completely clear, then start writing, as a more specific question will appear - ask and they will help you. Just the phrase "If you understand something, then answer me," as I understood, hints that you yourself do not know what exactly you need :) - Zowie
  • In general, I understand the pattern, as I also understand the distribution to several from the server to several clients using streams. It is necessary to do the dissemination of messages using the observer pattern. but how to pass in the parameter running server? - Natasha
  • @ Natasha either I did not understand your question, or you chose the wrong approach. Why do you need to transfer a running server? Consider a small example of a tcp server and your task (o5 such if I understand it correctly) - the client connects to the server, after that the client has some serverConnection object, and the clientSocket server. Actually, your task is to wrap the first and second, that's all. As for sending events to several clients, again - for this we keep a collection of connections, passing on which we send. If you have any questions - ask - Zowie
  • mailing is a classic example of using a pattern, an interface for a client, a class with a collection and methods for subscribing, unsubscribing, notifying clients for a server. - Gorets
  • in another way I will ask))) how to add a client to the clients collection of the previously created server. I will clarify the server and the client are different applications. They can all interact with each other. Only I do not know how to tell the server "I am your client. Please add me to your collection." - Natasha

1 answer 1

If I understand correctly @Natashenka then the virgin is trying to implement the Observer pattern as follows:

  1. There is a server that keeps a list of clients; in the terms of the selected pattern, it is the Subject - that is, the one that holds the list of Observer .
  2. There are clients that are Observer , which receive certain messages / events from the server / subject .
  3. The question is how to add observers to your server list.

The answer (to the best of my understanding) is: obviously after authorization or some process after which the server understands that the client who knocks on him is his client. This process is called handshaking .

And as for the rules of handshaking, please develop it yourself! For example, in SSL handshake looks like this , but in TCP it looks like this