F
F
FR342015-12-02 14:36:47
Java
FR34, 2015-12-02 14:36:47

MulticastChat, why isn't there a constant reading from the ip address through a loop?

An almost ready-made multicast chat, I can’t understand what the error is, the while loop is written to constantly read packets from the address of the chat room, but it doesn’t read anything. I can’t understand whether the program doesn’t read packets in principle, or doesn’t send them, because the second client running on the same room doesn’t see the messages sent there.
Here is the evil code:

package multicastchat;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;

public class MulticastChat {
    
    int chatRoom = 0;
    int port = 0;
    String ipAdress = "";
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    
    public MulticastChat(){
        
    }
    
    public void choosingChatRoom() throws IOException{
        while (!(chatRoom > 0 && chatRoom < 5)) {
            System.out.println(
                    "We have four chat rooms, choose one of them (1, 2, 3, 4)");
            chatRoom = Integer.valueOf(br.readLine());
            switch (chatRoom) {
                case 1:
                    port = 4441;
                    ipAdress = "230.0.0.1";
                    break;
                case 2:
                    port = 4442;
                    ipAdress = "230.0.0.2";
                    break;
                case 3:
                    port = 4443;
                    ipAdress = "230.0.0.3";
                    break;
                case 4:
                    port = 4444;
                    ipAdress = "230.0.0.4";            
                    break;
                default:
                    System.out.println("Sorry, but we haven't this room, try another room");
                    this.choosingChatRoom();
                    break;
            }
            System.out.println("Welcome to room " + chatRoom);
        }
    }

    public static void main(String[] args) throws IOException {
        MulticastChat mc = new MulticastChat();
        mc.choosingChatRoom();
        MulticastSocket socket = new MulticastSocket(mc.port);
        InetAddress address = InetAddress.getByName(mc.ipAdress);
        socket.joinGroup(address);

        DatagramPacket outMessage;
        DatagramPacket inMessage;

        String userInput;

        while (true) {
            //Приём сообщения
            inMessage = new DatagramPacket(new byte[4096], 4096);
            inMessage.setLength(inMessage.getData().length);
            socket.receive(inMessage);
            String received = new String(
                    inMessage.getData(), 0, inMessage.getLength());
            System.out.println(received);   
            //Отправка сообщения
            if ((userInput = mc.br.readLine()) != null) {
                if (userInput.equals("quit")) {
                    System.out.println("Bye, see you later ^_^");
                    socket.leaveGroup(address);
                    socket.close();
                    System.exit(0);
                } else {
                    byte[] buff = userInput.getBytes();
                    outMessage =
                            new DatagramPacket(buff, buff.length, address, mc.port);
                    socket.send(outMessage);
                }
            }         
        }
    }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
FR34, 2015-12-06
@FR34

The answer was found a couple of days ago
, the br.readLine() function, as well as the socket.recieve (inMessage) function, are blocking. It
helped to run the send and receive functions on two different threads

V
Viktor Maksimov, 2015-12-03
@ValorVl

Debuger - this is
Wireshark - see if IGMP is working correctly

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question