2011-11-13 39 views
0

我正在創建一個程序,其中多個客戶端可以連接到服務器。客戶端發送的消息將被廣播到服務器上的所有其他客戶端連接。套接字編程:廣播功能只發送給一個客戶端

我的問題是,該消息是廣播到只有客戶端來自它,我不能發現我的代碼中的錯誤。

任何人都可以幫助我發現問題所在,或者我可以如何改進代碼?謝謝。

編輯:

public class MsgClient{ 

private Socket client; 
private ObjectInputStream input; 
private DataOutputStream output; 
private BufferedReader keyboard; 
private String cmdInput; 


public MsgClient(String name, String server, int port){ 

    try{ 

     client = new Socket(server, port); 

     DataInputStream sInput = new DataInputStream(client.getInputStream()); 
     output = new DataOutputStream(client.getOutputStream()); 
     input = new ObjectInputStream(client.getInputStream()); 
     keyboard = new BufferedReader(new InputStreamReader(System.in)); 


     output.writeUTF(name); 


     while(true){ 
      System.out.println("Send a msg to the server: "); 
      cmdInput = keyboard.readLine(); 
      output.writeUTF(cmdInput); 
      System.out.println(sInput.readUTF()); 
     } 

    } 
    catch (Exception e){ 
     e.printStackTrace(); 
    } 
}// end constructor 


public static void main(String args[]) throws IOException { 
    if(args.length != 3) 
     throw new RuntimeException("Syntax: java MsgClient <username> <servername> <port>"); 
    MsgClient aClient = new MsgClient(args[0], args[1], Integer.parseInt(args[2])); 
} // end main 

}

public class MsgServer { 


public MsgServer(int PORT) throws IOException{ 

    ServerSocket server = new ServerSocket(PORT); 
    System.out.println("Server Established..."); 


    while(true){ 

     Socket client = server.accept(); 

     DataInputStream input = new DataInputStream(client.getInputStream()); 
     ObjectOutputStream oo = new ObjectOutputStream(client.getOutputStream()); 
     DataOutput output = new DataOutputStream(client.getOutputStream()); 

     System.out.println("New client accepted"); 

     String clientName = input.readUTF(); 
     ClientHandler handler = new ClientHandler(clientName, client); // construct and run thread. 

     handler.start(); 
     System.out.println("Handler started!"); 

    }//end while 

}//end of constructor 


public static void main(String args[]) throws IOException { 
    if(args.length != 1) 
     throw new RuntimeException("Syntax: java MsgServer requires <PORT> number"); 
    new MsgServer(Integer.parseInt(args[0])); 
} 

}

public class ClientHandler extends Thread { 

Socket client; 
DataInputStream din; 
DataOutputStream dout; 
String name; 

String clientMsg; 

protected static Vector socketVector = new Vector(); 


public ClientHandler (String name, Socket client) throws IOException{ 
    this.name = name; 
    this.client = client; 
    din = new DataInputStream(client.getInputStream()); 
    dout = new DataOutputStream(client.getOutputStream()); 
} 

// Code run at every start() 
public void run(){ 
    try{ 
     socketVector.addElement(this);  
     clientMsg = din.readUTF(); // inside or outside loop? 

     while(true){ 
      broadcast(name + " has joined auction on IP " + client.getInetAddress()); 
      broadcast(name + " says: " + clientMsg); 
     } 

    } catch(IOException ex){ 
     System.out.println("-- Connection to user lost"); 
    } finally{ 
     socketVector.removeElement(this); 
     broadcast(name + " has left"); 
     try{ 
      client.close(); 
     }catch (IOException ex){ 
      System.out.println("socket to user already closed?"); 
     } 
    } 
} 
+0

你能發佈更多信息嗎?目前還不清楚你的類有多少個實例,以及每個實例是否有自己的'socketVector'。 – Vlad

+0

我的猜測是,您正在收到一個您忽略的異常,或者您的客戶端不是異步讀取套接字。 –

+0

嗨,我已編輯該帖子以包含我用於此問題的三個類文件。 – BodhiByte

回答

0

的另一個問題是在這裏,在MsgClient代碼:

cmdInput = keyboard.readLine(); 
output.writeUTF(cmdInput); 
System.out.println(sInput.readUTF()); 

客戶端將不會收到一條消息,直到它已派出一個後。

0
  1. 哪裏broadcast()方法?

  2. 您正在服務器中創建兩組流。接受循環不應該創建任何流或做任何I/O。所有這些都應該在處理連接的線程中完成。

  3. 你不需要ObjectInput/OutputStreams的都在這裏。

  4. 當您在套接字上發現讀取超時之外的任何IOException時,必須將其關閉。您還應該打印出例外自己的信息,而不是僅僅編寫自己的信息。

相關問題