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.

vogs
  • 51
  • 1
  • 6
  • 1
    You need to use `Threading` here for better and efficient implementation. Now for the actual implementation, You can either use concepts of [Socket](https://docs.oracle.com/javase/tutorial/networking/sockets/definition.html) or [Remote Method Invocation](https://docs.oracle.com/javase/tutorial/rmi/). – Sanket Makani May 14 '17 at 11:48
  • Thanks. I tried to do it like this example but now I have the problem how to start the client? http://www.oracle.com/technetwork/java/socket-140484.html – vogs May 14 '17 at 15:09
  • Refer [this](https://www.javatpoint.com/socket-programming). – Sanket Makani May 14 '17 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. – vogs May 14 '17 at 15:15
  • That's not good practice even if you successfully implement it. Look professionally, you should follow [Design Patterns](https://www.javatpoint.com/design-patterns-in-java) while implementing a module in `Java`. Most of the patterns say that Responsibilities should be divided such that all classes have a strong responsibility. So you need to here separate both the logic of `Client` as well as `Server` to have [High Cohesion](http://stackoverflow.com/questions/10830135/what-is-high-cohesion-and-how-to-use-it-make-it). – Sanket Makani May 14 '17 at 15:23
  • Yeah, I have a Server class and a Client class of course. My idea for the main is, to start the server first, then start the client. So everything remains in one project. – vogs May 14 '17 at 15:27
  • Okay, That's fine. No problem in that. But if you want to have reviews for your existing codes, You can try [CodeReview](https://codereview.stackexchange.com/). – Sanket Makani May 14 '17 at 15:29
  • Just for sharing my code there (at CodeReview)? – vogs May 14 '17 at 15:35
  • 1
    https://codereview.stackexchange.com/questions/163328/java-server-and-client-in-one-program Just in case, if someone have also ideas to my code) – vogs May 14 '17 at 15:43

1 Answers1

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?

vogs
  • 51
  • 1
  • 6