Hey guys,

I'm working on a chat program where a UDP console-based chat program's client sends a message to the server and subsequently the server sends it to all other clients on the network. As of this moment the program is able to:

  1. Client send message to server
  2. Server receives message
  3. Server sends message to "all" clients on the LAN
  4. Server just sends the message back to the sending client and not other client

                ....... //<-- Before this there's the input message which works fine and some initiation of variables
    
                byte outData[] = outMSG.getBytes();
    
                // address is just an arrayList of IP addresses
                for (int i = 0; i < address.size(); i++)
                {
                        // i was thinking that by iterating through the list the message will be sent to all addresses but this does not seem to be the case since it's not working
                    InetAddress cAddress = InetAddress.getByName(addressList.get(i));
    
                    int cPort = receivingPacket.getPort();
    
                    DatagramPacket outPacket = new DatagramPacket(outData, outData.length, cAddress, cPort);
                    socket.send(outPacket);
    
                }
    

I also believe my client is working as intended and the problem just lies with the server. I can't seem to make the server send the message to a different client (different to the sending client). Any help will be appreciated and if you need more code please let me know

What's the relationship between the address and addressList Lists?
Are you sure that addressList contains all the entries you expect? Is the size() of address as large as you expect?

Oh thats just a typo. The addressList works fine.

Your "just a typo" has probably wasted all kinds of time for people who have read your code trying to help you. An apology would be more appropriate than just laughing it off.

Anyway what exactly happens when you try to send to multiple clients?

Whoa i didn't mean to make it seem like i laughed it off. I just wanted to try and reply ASAP so i didn't cause any confusion. I had no idea writing a typo and not apologising for 1 human mistake could cause so much trouble for people. I'll keep that in mind for future posts. If i have caused any trouble for anyone out there i apologise.

In regards to the problem what happens is stated in the steps 1 - 4 above. However, i no longer need help with this problem. Thanks for the time and etiquette lesson though. I'll try to be more "friendly" with my words in the future.

Thanks for the apology and problem update. Just to be clear - the (only) take-away message is that typos in posted code can cause a lot of wasted time because people see them, assume that's the actual code, and try to analyse the resulting problem and how to fix it.

Anyway, I'm glad the problem is solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.