2016-05-01 9 views
0

私は1つのTCPクライアントと1つのTCPサーバーをJavaで書いています。サーバーはクライアントからの指示を待っていますが、クライアントに指示を送信できる必要があります。 クライアントがサーバーに何かを送って返信を待つことができます。しかし、前に何かを送ることなく、メッセージを待つようなことはできません。Java TCPクライアントのリッスンとTCPサーバーへの書き込み

TCPクライアント

public class TCPClient { 

    static DataOutputStream toServer; 
    static BufferedReader fromServer; 
    static Socket socket; 

    public static void main(String[] args) throws Exception { 
     BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); 
     System.out.println("*************************"); 
     System.out.println("*  Client   *"); 
     System.out.println("*************************"); 
     System.out.println("INSTRUCTION  | EFFECT"); 
     System.out.println("aktiv    | ready to do something"); 
     System.out.println("exit    | disconnect"); 
     System.out.println(); 
     System.out.print("Please enter the IP-Address of the Server: "); 
     String ip = input.readLine(); 
     System.out.println(); 

     try { 
      socket = new Socket(ip, 9999); 
     } catch (Exception e) { 
      System.out.println("Can not connect to Server!"); 
     } 

     toServer = new DataOutputStream(socket.getOutputStream()); // Datastream FROM Server 
     fromServer = new BufferedReader(new InputStreamReader(socket.getInputStream()));  
     while (sendRequest()) {    
      receiveResponse();     
     } 
     socket.close(); 
     toServer.close(); 
     fromServer.close(); 
    } 

    private static boolean sendRequest() throws IOException { 
     BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); 
     String line; 
     boolean holdTheLine = true;   // Connection exists 

     System.out.print("> "); 
     line = input.readLine(); 

     switch (line) { 
      case "aktiv": 
       toServer.writeBytes("active" + '\n'); 
       break; 
      case "exit": 
       holdTheLine = false; 
       break; 
      default: 
       break; 
     } 

     return holdTheLine; 
    } 

    private static void receiveResponse() throws IOException { 
     System.out.println("Server: " + fromServer.readLine() + '\n'); 
    } 
} 

TCP-サーバー

public class TCPServer { 
    static boolean connected = true; 
    public static void main(String[] args) throws Exception { 
     System.out.println("********************************"); 
     System.out.println("*   Server    *"); 
     System.out.println("********************************"); 
     System.out.println("INSTRUCTION | EFFECT"); 
     System.out.println("ok   | send an ok to client"); 

     ServerSocket listenSocket = new ServerSocket(9999); 

     while (true) { 
      final Socket client = listenSocket.accept(); 

      Thread newClientThread = new Thread(new Runnable() { 
       @Override 
       public void run() { 
        multithreadedServer(client); 
       } 
      }); 
      newClientThread.start(); 
     } 
    } 

    public static void multithreadedServer(Socket client) { 
     String line; 
     final BufferedReader fromClient; 
     final DataOutputStream toClient; 
     Thread cmdForClient; 

     try { 
      fromClient = new BufferedReader(new InputStreamReader(client.getInputStream())); 
      toClient = new DataOutputStream(client.getOutputStream()); 

      while (connected) { 
       cmdForClient = new Thread(new Runnable() { 
        @Override 
        public void run() { 
         try { 
          String line = fromClient.readLine(); 
          System.out.println("Client: " + line); 
          if (line.equals("exit")) { 
           connected = false; 
          } 
         } catch (Exception e) { 
          System.out.println(e); 
         } 
        } 
       }); 
       cmdForClient.start(); 

       final BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); 

       try { 
        String reply = input.readLine(); 
        if (reply.equals("ok")) { 
          toClient.writeBytes("OK." + '\n'); 
        } 
       } catch (Exception e) { 
        System.out.println(e); 
       } 
      } 
      fromClient.close(); 
      toClient.close(); 
      client.close(); 
     } catch (Exception e) { 
      System.out.println(e); 
     } 
    } 

} 

答えて

0

クライアントサーバシナリオの典型的な動作は、サーバに要求を送信するクライアントのためです。ただし、ピアツーピア・アプリケーションでは、両方のエンドポイントがクライアントとサーバーの両方として機能する可能性があります。唯一の違いは、接続を開いたエンドポイントだけです。あなたの場合、問題は "サーバー"だけが受信者スレッドを使用していることです。クライアント側で受信スレッドを開始して、問題を解決する必要があります。クライアントのサーバーからスレッドコードを再利用するだけで十分です。サーバへの接続を開いた後、ソケットを受信スレッドに渡すだけです。

EDIT:あなたのクライアントで

  Thread newServerThread = new Thread(new Runnable() { 
       @Override 
       public void run() { 
        multithreadedServer(socket); 
       } 
      }); 
      newServerThread.start(); 

ソケットは、サーバーへのソケットです。クライアント操作の詳細や相違点については、multithreadedServerを更新する必要がありますが、原則は同じである必要があります。

+0

「クライアントのサーバーからのスレッドコード」の意味についての短い例を教えてください。 – user3653164

+0

私の言うことは、サーバーだけでなく、クライアントコードにもスレッドを使用する必要があるということです。すでにサーバーにコードがあるので、そのコードを再利用できます。 – Andy

関連する問題