I have a UDP server and need to perform an "all" function. That means when a user sends "all blah blah" the server checks a list of clients and sends "blah blah" to all of them.

I've stored the clients in a HashMap and I'm trying to figure a way to iterate through, create a packet and send for each record.

The record would look like key:name value ip,port. I know this isn't an ideal way to do it, but I've spent a week or so on this and just kept making things more and more complicated.

private void sendToAll(InetAddress clientIP, int clientPort, String message, String fromUserName) {

for (Object key : cHASH.keySet())
        {
            Object value = cHASH.get(key);
            //System.out.print(value);
            //split value
            //clientIP = [0]
            //clientPort = [1]
            //serverResponse(clientIP,ClientPort);


        }
}

Clearly the trouble is actually coding it and I just can't seem to get it. I have the serverResponse code, which is just a method that takes the inetAddress, Port and some other objects to make and send the packets.

Any help with this is appreciated.

 for (Object key : cHASH.keySet())
        {
            Object value = cHASH.get(key);
            Object[] newArr = ((String) value).split(",");
            System.out.print("element 0 "+newArr[0]);
            clientPort = Integer.parseInt((String) newArr[1]);
            //clientIP = (InetAddress) newArr[0];
            //clientIP = (InetAddress) newArr[1];
             InetAddress.getByName((String) newArr[1]);

            try {
            byte[] buf = new byte[256];
            buf = sudMessage.getBytes();
            DatagramPacket sPacket = new DatagramPacket(buf, buf.length,
                    clientIP, clientPort);
            serverSocket.send(sPacket);


        } catch (IOException ex) {
            System.err.println(ex);
        }   


        }

This is very sloppy but it works. Sad that I can't get the client to client working.

Seems I have more time than I thought. I've managed to get the All chat working, but Client-to-Client is proving harder than expected.

This article has been dead for over six months. Start a new discussion instead.