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.