2017-04-12 11 views
0

コマンドプロンプトから実行される単純なマルチスレッドチャットサーバーを構築しようとしています。クライアントはサーバーに接続し、サーバーは複数のクライアントを保持しますが、あるクライアントから別のクライアントにメッセージを送信しようとするとき、またはログインしている別のユーザーのクライアントに通知するときにも、クライアントコマンドプロンプトには何も表示されません。あなたがPrintWriter.println()、データをバッファに書き込んでデータを送信するときマルチスレッドサーバーチャットJava - メッセージは接続していますが、メッセージはありません。

public class Server { 
private static ServerSocket servSock; 
private static Socket clientSock; 
private static ArrayList<ClientThread> clientList; 
private static int IDcount = 0; 


public static void main(String args[]){ 
     // Get command line arguments. 
     if (args.length != 3) { 
     System.out.println("Required arguments: server port, block duration, timeout"); 
     return; 
     } 
     int port = Integer.parseInt(args[0]); 
     int blockDur = Integer.parseInt(args[1]); 
     int timeout = Integer.parseInt(args[2]); 

     try{ 
     servSock = new ServerSocket(port); 
     clientList = new ArrayList<ClientThread>(); 
     } 
     catch(IOException ex){ 
      System.err.println(ex); 
     } 


     while (true) { 
     try { 
      clientSock = servSock.accept(); 
      ClientThread thread = new ClientThread(clientSock); 
      clientList.add(thread); 
      thread.start(); 
     } catch (IOException e) { 
      System.out.println(e); 
     } 
     } 
    } 

private synchronized static void broadcast(String msg){ 
    System.out.print(msg); 
    for(int i = 0; i < clientList.size(); i++){ 
     ClientThread client = clientList.get(i); 
     client.send(msg); 
    } 

} 

synchronized static void unlist(int id){ 
for(int i = 0; i < clientList.size(); i++){ 
    ClientThread thread = clientList.get(i); 
    if(thread.id == id){ 
     clientList.remove(i); 
     return; 
     } 
    } 
} 

static class ClientThread extends Thread { 
    Socket sock; 
    BufferedReader tIn; 
    PrintWriter tOut; 

    int id; 
    String username; 
    String msg; 

    ClientThread(Socket sock){ 
     id = IDcount++; 
     this.sock = sock; 
     try{ 
      tIn = new BufferedReader(new InputStreamReader(sock.getInputStream())); 
      tOut = new PrintWriter(sock.getOutputStream()); 
      username = tIn.readLine(); 
      broadcast(username + " logged in"); 
     } 
     catch(IOException ex){ 
      System.err.println(ex); 
     } 
    } 

    public void run(){ 
     boolean loggedIn = true; 
     while(loggedIn){ 
      try{ 
       msg = tIn.readLine(); 
      } 
      catch (IOException ex){ 
       System.err.println(ex); 
      } 
      String[] parts = msg.split("\\s",2); 
      String type = parts[0]; 

クライアントコードは

public class Client{ 
private static Socket clientSock; 
private static BufferedReader in; 
private static PrintWriter out; 
private static Scanner scan; 

public static void main(String[] args) throws IOException { 
     if (args.length != 2) { 
      System.out.println("Required arguments: server IP, server port"); 
      return; 
      } 
     String host = args[0]; 
     int port = Integer.parseInt(args[1]); 

     clientSock = new Socket(host, port); 
     in = new BufferedReader(new InputStreamReader(clientSock.getInputStream())); 
     out = new PrintWriter(clientSock.getOutputStream()); 
     scan = new Scanner(System.in); 

     new ListenFromServer().start(); 
     boolean online = true; 

     System.out.println("Enter your username:"); 
     String username = scan.nextLine(); 

     out.println(username); 

     while(online){ 
      System.out.println("> "); 
      String msg = scan.nextLine(); 
      String[] parts = msg.split("\\s"); 
      String type = parts[0]; 
      send(msg); 
      if(type.equalsIgnoreCase("logout")){ 
       online = false; 
      } 


     } 
     logoff(); 
} 

     static void send(String msg) throws IOException{ 
      out.println(msg); 
     } 

     private static void logoff() throws IOException{ 
       in.close(); 
       out.close(); 
       scan.close(); 
       clientSock.close(); 
     } 

    static class ListenFromServer extends Thread{ 
     public void run(){ 
      while(true){ 
       try{ 
        String msg = in.readLine(); 
        System.out.println(msg); 
       } 
       catch(IOException ex){ 
        System.err.println(ex); 
       } 
      } 
     } 
    } 

    } 
+0

コードをステップ実行しようとしましたか?また、キャッチエラーは、あなたがしたくない場所です。 –

答えて

0

似ています。 printlnの後にPrintWriter.flush()に電話すると、すぐにサーバーとの間でデータを送受信する必要があります。

ClientThreadコンストラクタでusername = tIn.readLine();を呼び出すと、メインスレッドのコンストラクタ呼び出しのためにメインスレッドがブロックされます。したがって、接続されたユーザーはユーザー名を送信しませんが、他のクライアントは接続できません。

関連する問題