2016-03-21 38 views
0

私はちょっとしたチャットシステムを作ろうとしています。私はコンソールとクライアントを持っています。現在のところ、クライアントだけがコンソールにメッセージを送信する必要があります。私は正常にサーバーに接続することができ、私はクライアントからコンソールに1つのメッセージを送信することができます。問題は最初のメッセージを送信した後に始まります。いつ最初のメッセージ私は他のメッセージを送信することはできません。ソケット経由で複数のメッセージを送信できません

メッセージを送信しないコンソールまたはメッセージを送信しないクライアントであるかどうかわかりません。この場合、どうすればこの問題を解決できますか?

public class ClientMainClass { 


private static Socket socket; 
public static void main(String args[]) { 
    try { 

     String host = "localhost"; 
     int port = 25000; 
     InetAddress address = InetAddress.getByName(host); 
     socket = new Socket(address, port); 

     Scanner scanner = new Scanner(System.in); 
     System.out.println("Skriv dit username:"); 
     String name = scanner.nextLine(); 
     System.out.println("Du er logget ind som: " + name); 
     String input; 

     do{ 
      input = scanner.nextLine(); 
      if (input.equalsIgnoreCase("exit")) { 
       System.out.println("Du forlod serveren"); 
       socket.close(); 
       continue; 
      }else { 
       /*OutputStream os = socket.getOutputStream(); 
       OutputStreamWriter osw = new OutputStreamWriter(os); 
       BufferedWriter bw = new BufferedWriter(osw);*/ 
       PrintWriter printWriter = new PrintWriter(socket.getOutputStream(),true); 
       Date date = new Date(); 
       String time = date.getDate()+"/"+date.getMonth()+":"+date.getHours()+":"+date.getMinutes(); 
       //Send the message to the server 
       String message = time+ " - " + name + ": "+input; 
       printWriter.println(message); 
       System.out.println(message); 
       continue; 
      } 
     }while (!(input.equals("exit"))); 
    } catch (Exception exception) { 
     exception.printStackTrace(); 
    } finally { 
     //Closing the socket 
     try { 
      socket.close(); 
     } catch(Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 
} 

マイサーバー:

public class Main{ 

private static Socket socket; 

public static void main(String[] args) { 


    try { 

     int port = 25000; 
     ServerSocket serverSocket = new ServerSocket(port); 
     System.out.println("Server Started and listening to the port 25000"); 

     while(true) { 
      //Reading the message from the client 
      socket = serverSocket.accept(); 
      InputStream is = socket.getInputStream(); 
      InputStreamReader isr = new InputStreamReader(is); 
      BufferedReader br = new BufferedReader(isr); 
      System.out.println(br.readLine()); 
     } 
    } 
    catch (Exception e) { 
     e.printStackTrace(); 
    } finally { 
     try { 
      socket.close(); 
     } catch(Exception e){} 
    } 
} 
} 

明確にすること。私はサーバーに接続することができます。私はクライアントからコンソールに1つのメッセージを送ることができますが、メッセージは1つしか送信できません。

答えて

3

2行目を読むことはありません。サーバーは接続を受け付け、その接続から1行を読み取り、新しい接続を待って、最初の接続で到着した可能性のあるものをすべて破棄します。

しかし、クライアントは最初の(かつ唯一の)接続を使用してすべての入力を送信しますが、これは絶対に正しいです。

この特定の問題は次のように解決することができます:

while(true) { 
    //Reading the message from the client 
    socket = serverSocket.accept(); 
    InputStream is = socket.getInputStream(); 
    InputStreamReader isr = new InputStreamReader(is); 
    BufferedReader br = new BufferedReader(isr); 
    while(true){ 
    System.out.println(br.readLine()); 
    } 
} 

これはあなたのプログラムがその最初の接続に到着し、すべてを印刷するようになりますが、それは2番目の接続を受け入れることはありません。

複数のクライアントを処理するには、それぞれを処理するにはThreadが必要です。

+0

ニース、ありがとうございます。私はwhileループの外側でsocket.accpet()を動かし、今は動作します。ありがとうございました。 –

+0

ループ内で '' accept''を呼び出さないと、サーバーは最初の接続のみを処理し、それ以外の接続は処理しません。 – f1sh

+0

正解ですが、今は1対1のチャットだけが必要です。 –

関連する問題