i want to synchronize 2 Collections with each other. If something changes at the Server side, the connected Clients get updated. I have a quite basic question. Do I now need to copy my java project and program the server in one and the client in the other one? But that sounds like quite a lot of unnecessary work. Can't I implement it all in one project an then start server and client in one main? Do I need threads? I'm kind of stuck what the best way is here. Thanks in advance.
1 Answer
Because codereview doesn't allow my code cause it's not yet working, i post it now here in the hope, that you can help me.
public class Server implements Runnable{ private String hostName = "127.0.0.1"; private int portNumber; private ServerSocket serverSocket; private Socket clientSocket; public Server(int port){ this.portNumber = port; } public void run(){ String line = ""; PrintWriter out = null; BufferedReader in = null; BufferedReader stdIn = null; try{ this.serverSocket = new ServerSocket(this.portNumber); }catch (IOException e) { System.out.println("Could not listen on port"); } try{ clientSocket = serverSocket.accept(); } catch (IOException e) { System.out.println("Accept failed"); } try{ in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); out = new PrintWriter(clientSocket.getOutputStream(), true); }catch (IOException e) { System.out.println("Read failed"); } while(true){ try{ line = in.readLine(); }catch (IOException e) { System.out.println("Read failed"); } System.out.println("line: "+line); } } protected void finalize(){ //Objects created in run method are finalized when //program terminates and thread exits try{ serverSocket.close(); }catch (IOException e) { System.out.println("Could not close socket"); } } }
public class Client implements Runnable{ private String hostName = "127.0.0.1"; private int portNumber = 6602; private Socket clientSocket = null; public Client(Socket client){ this.clientSocket = client; } public Client(int portNumber, String hostName){ this.portNumber = portNumber; this.hostName = hostName; } public void run(){ String line; PrintWriter out = null; BufferedReader in = null; BufferedReader stdIn = null; try{ if(clientSocket == null) this.clientSocket = new Socket(this.hostName, this.portNumber); out = new PrintWriter(clientSocket.getOutputStream(), true); in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); stdIn = new BufferedReader(new InputStreamReader(System.in)); out.println("Test string from client"); }catch (IOException e){ System.out.println("in or out failed"); } } }
public class DebugServerClient { public static void testServerClient(){ int port = 6602; Server srv = new Server(port); Client clt = new Client(port, "127.0.0.1"); Thread s = new Thread(srv); s.start(); Thread c = new Thread(clt); c.start(); } }
I changed it now to this and it seems to work. Is this a good way?
Threadinghere for better and efficient implementation. Now for the actual implementation, You can either use concepts of Socket or Remote Method Invocation.