-1

My program doesn't work properly. The problem is that second client can't see messages from the first. I think problem is in while loop. Just not reading from IP adress. Can you help me? Thank you in advance.

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) {
            //Receiving message
            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);   
            //Sending message
            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);
                }
            }         
        }
    }
}
ilyablbnv
  • 161
  • 2
  • 10

1 Answers1

1

Assuming you are using the same program at both ends of the chat nothing will happen. Each instance starts up by reading from the socket, before anything's been written, so it will block forever.

No instance ever gets to where it can write to the socket.

Note that socket programming usually requires several threads as things are happening asynchronously on at least two "channels" (user interaction and socket I/O). A full explanation of how to write networking code is far beyond what StackOverflow is designed for.

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
  • 'Usually' is a strong word. There are plenty of network-aware UI programms operating in a single-thread model. – SergeyA Dec 02 '15 at 19:11
  • A single-thread model "breaks" as soon as network I/O blocks for any reason -- the UI becomes unresponsive. It also breaks when the UI is blocked waiting for input and enough data arrives over the network to overflow buffers. – Jim Garrison Dec 02 '15 at 19:14
  • 1
    learn to love poll() :) I am speaking from experience, and nothing 'breaks' in the world I am talking about. – SergeyA Dec 02 '15 at 19:15