2016-03-21 51 views
0

我正在嘗試製作一個小聊天系統。我有一個控制檯和一個客戶端。現在只有客戶端需要發送消息到控制檯。我可以成功連接到服務器,並且我可以從客戶端向控制檯發送一條消息。發送第一條消息後,麻煩就開始了。當第一條消息我不能發送任何其他消息。無法通過套接字發送多個郵件

我不知道是否控制檯不會讀取消息或不會發送消息的客戶端。在這種情況下,我如何解決這個問題?

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){} 
    } 
} 
} 

予以明確。我可以連接到服務器。我可以從客戶端向控制檯發送一條消息,但只能發送一條消息。

回答

3

你從不讀第二行。您的服務器接受連接,從該連接讀取一行,然後等待新連接,丟棄可能到達第一個連接的所有內容。

然而,您的客戶使用第一個(也是唯一的)連接發送所有輸入,這是絕對正確的。

這一特定問題可以解決這樣的:

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

這將導致你的程序打印所有到達的是第一個連接上,但它永遠不會接受第二個連接。

爲了處理多個客戶端,您需要一個Thread來處理每個客戶端。

+0

不錯,謝謝。我在while循環之外移動了socket.accpet(),現在它工作。謝謝。 –

+0

請記住,如果您不在循環中調用「accept」,那麼您的服務器將只處理第一個連接,而不處理任何其他連接。 – f1sh

+0

正確,但現在我只想要一對一的聊天。 –