套接字编程:广播功能仅发送给一个客户端

时间:2011-11-13 15:56:00

标签: java sockets client broadcast

我正在创建一个程序,其中多个客户端可以连接到服务器。客户端发送的消息将广播到服务器上的所有其他客户端连接。

我的问题是该邮件只向其来自的客户端广播,我无法在代码中发现错误。

任何人都可以帮我找出问题所在或者我如何改进代码吗?谢谢。

编辑:

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?");
        }
    }
}

2 个答案:

答案 0 :(得分:0)

  1. broadcast()方法在哪里?

  2. 您正在服务器中创建两组流。 accept循环不应创建任何流或执行任何I / O.所有这些都应该在处理连接的线程中完成。

  3. 您根本不需要ObjectInput/OutputStreams

  4. 如果在套接字上获得除读取超时之外的任何IOException,则必须将其关闭。您还应该打印出异常自己的消息,而不仅仅是编写自己的消息。

答案 1 :(得分:0)

另一个问题在于MsgClient代码:

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

客户端在发送邮件之后才会收到邮件。