0

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.

9
  • 1
    You need to use Threading here for better and efficient implementation. Now for the actual implementation, You can either use concepts of Socket or Remote Method Invocation. Commented May 14, 2017 at 11:48
  • Thanks. I tried to do it like this example but now I have the problem how to start the client? oracle.com/technetwork/java/socket-140484.html Commented May 14, 2017 at 15:09
  • Refer this. Commented May 14, 2017 at 15:10
  • Thats exactly the problem. I don't want to have 2 different programs to achive this. I want to have server and client in one. Commented May 14, 2017 at 15:15
  • 1
    codereview.stackexchange.com/questions/163328/… Just in case, if someone have also ideas to my code) Commented May 14, 2017 at 15:43

1 Answer 1

1

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?

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.