多播组客户端不从服务器接收消息

时间:2018-01-04 01:40:12

标签: java udp multicast

我创建了一个聊天应用程序,用于将消息发送到与多个客户端的多播组对话。

必须将消息发送到MulticastServer并发送到多播组上的所有客户端。此时,消息在完美的条件下从客户端到达服务器。

但是当我回复客户端(即使是一个简单的字符串)时,该消息仅发送给发送消息的客户端,而不是发送给多播组中的所有用户。

StackTrace不会给我任何错误,但我仍然遇到这个问题。

我给你一些重要的代码。以下是指连接到组播服务器的连接。 DEFAULT_ADRESS是224.0.0.3。

    socket = new MulticastSocket();
    address = InetAddress.getByName(DEFAULT_ADRESS);
    socket.joinGroup(address);

向MulticastServer发送消息的代码部分:

String messtoSendServer = utilizadorOnline.getNome() + ":" + textfieldtocomunicateGroupe.getText();

        buf = messtoSendServer.getBytes();

        packet = new DatagramPacket(buf, buf.length, address, DEFAULT_PORT);

        try {
            // userOnline_Multicast.getSocketMulti().send(packet);
            socket.send(packet);

        } catch (IOException ex) {
            Logger.getLogger(ConversaGrupo.class.getName()).log(Level.SEVERE, null, ex);
        }

从服务器接收消息的代码部分:

  private void receberDadosServidor() throws IOException {



            try {

                DatagramPacket packet1 = new DatagramPacket(buf, buf.length);

                socket.receive(packet1);

                String received = new String(packet1.getData());

                textareatoGroupChat.setText(textareatoGroupChat.getText() + "\n" + received);

            } catch (IOException ex) {

                Logger.getLogger(ConversaGrupo.class.getName()).log(Level.SEVERE, null, ex);
                socket.close();
                socket.leaveGroup(address);
            }

这是服务器端。首先启动线程:

 public void run(JTextArea txtArea) throws IOException {


       new MulticastServerThread(txtArea).start();
    }

线程本身:

public class MulticastServerThread extends Thread {

    private final String DEFAULT_MULTICASTIP = "224.0.0.3";
    private final int DEFAULT_MULTICASTPORT = 4446;
    private final int FIVE_SECONDS =5000;
    private DatagramPacket packet;
    private JTextArea textA;
    private boolean moreQuotes = true;
    private MulticastSocket socket = null;

    private InetAddress adresstoConnectMulticast = null;

    public MulticastServerThread(JTextArea txt) throws IOException {
        super("MulticastServerThread");
        this.textA = txt;

    }

    @Override
    public void run() {

        while (true) {
            try {
                byte[] buf = new byte[1024];

                socket = new MulticastSocket(DEFAULT_MULTICASTPORT);
                adresstoConnectMulticast = InetAddress.getByName(DEFAULT_MULTICASTIP);

                socket.joinGroup(adresstoConnectMulticast);

                packet = new DatagramPacket(buf, buf.length, adresstoConnectMulticast, DEFAULT_MULTICASTPORT); //usado para receber um datagram do socket, o array de bytes contem dados do cliente especifico

                socket.receive(packet);

                String mensagem = new String(packet.getData()).trim();

                textA.setText(textA.getText() + "\n\nServer Multicast Receive from User:" + mensagem +" on IP Multicast " +DEFAULT_MULTICASTIP +" | "+ DEFAULT_MULTICASTPORT);

                buf = mensagem.getBytes();
                InetAddress ed = packet.getAddress();
                int portad = packet.getPort();

//manda de volta para o cliente.
                packet = new DatagramPacket(buf, buf.length, ed, portad);
                socket.send(packet);

                try {

                    sleep((long) Math.random() * FIVE_SECONDS);
                } catch (InterruptedException e) {
                }

            } catch (IOException ex) {
                Logger.getLogger(MulticastServerThread.class.getName()).log(Level.SEVERE, null, ex);
                socket.close();

            }

        }
    }
}

1 个答案:

答案 0 :(得分:0)

这里有几个问题。

socket = new MulticastSocket();

您的客户端都需要绑定到同一个端口。为构造函数提供固定的端口号。否则,您将获得系统分配的端口。

其次,您的服务器回复源的IP地址和端口号。它应该回复组IP地址和源端口号,或上面的固定端口号,它们应该是相同的。

第三,保留224.0.0.3。你无法使用它。

让服务器的插槽保持打开状态。不要在每次循环时打开和关闭它。