2017-05-14 18 views
0

2つのコレクションを互いに同期したいと考えています。サーバー側で何かが変更された場合、接続されたクライアントは更新されます。 私はかなり基本的な質問があります。私は今私のJavaプロジェクトをコピーして、1つのサーバーともう1つのクライアントをプログラムする必要がありますか?しかし、それはかなりの不必要な作業のように聞こえる。 1つのプロジェクトですべてを実装することはできませんし、次にサーバーとクライアントを1つのメインで起動できますか?スレッドが必要ですか?私は、ここに最善の方法があるのか​​なんだか固執しています。 ありがとうございます。1つのプログラム内のJavaサーバーとクライアント

+1

より効率的に実装するには、ここで 'Threading'を使う必要があります。実際の実装では、[ソケット](https://docs.oracle.com/javase/tutorial/networking/sockets/definition.html)または[Remote Method Invocation](https:// docs .oracle.com/javase/tutorial/rmi /)。 –

+0

ありがとうございます。私はこの例のようにしようとしましたが、今はクライアントを起動する方法に問題がありますか? http://www.oracle.com/technetwork/java/socket-140484.html – vogs

+0

[this](https://www.javatpoint.com/socket-programming)を参照してください。 –

答えて

0

codereviewは私のコードがまだ動作していないため、ここに希望を添えて投稿してください。

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(); 


} 

}

私はこれに今それを変更し、動作しているようです。これは良い方法ですか?

関連する問題