I have created a simple UDP client and server, but I have some problems with how to actually structure the servers operational handling, whether its if Else or case selections.

1.The user sends a Packet to the server, containing a string that they've typed in.
2.The server receives it and checks the IP AND Port number against a list of currently active users and thier details.
3.If the IP and PORT do not match, a new client is added to the list and the server responds with a message.
4. Being a member, i.e. in the list. a client can send messages to other clients or perform simple commands. whoami, whois, all,whisper.

So as an example, the client may send:

Client One:
"Jimmy"
ServerSays: Welcome to crapChatServer Jimmy: Use who, whoami,all,or clientName to chat.
"who"
ServerSays: Jimmy, Alex, Muggles, are on the server
Alex hi

Client two receives

Jimmy says: hi.
"all welcome Jimmy"

all clients receive message

Obviously its fickle, especially by forcing whisper to actually chat to people, but for the assignment its what we have to do. Now, I've if statements and corresponding methods to handle most of this, but I keep tripping up over the very first hurdle. Checking the users ip/port on every packet.

public class UDPChatServer {


   ArrayList<ClientDetails> clientList = new ArrayList<ClientDetails>();



 public static void main(String[] argc) throws Exception {


        UDPChatServer udp = new UDPChatServer();
        String clientName = null;

        BufferedReader stdIn = new BufferedReader(
                new InputStreamReader(System.in));
        byte[] buf = null;
        DatagramSocket socket = new DatagramSocket(4445);
        DatagramPacket packet = null;

        while (true) {

            try {

                buf = new byte[256];
                packet = new DatagramPacket(buf, buf.length);

                //Receive the request:
                socket.receive(packet);
                String request;
                request = new String(packet.getData());

                InetAddress address = packet.getAddress();
                int port = packet.getPort();

                String userName = null;




                if(!clientList.contains(ip, port);){
                addNewUser();{
                }else
                String requestVerified = request;(string = to content of received string.
                 switch(requestVerified){
                    case ("who"):
                         getActiveUsers();//not implemented

                        System.out.println("who is on the server");
                        break;

                    case ("whoami"):

                    getClientName(address, port);//not implemented
                    System.out.println("who is user");
                    break;

                    case ("all"):
                        System.out.println("send to all");
                    sendToAll();//not implemented
                    break;

                    case ("UserName")://send to specified user
                        System.out.println("send message to specified user"+userName);
                    break;

                    case ("bye"):
                        removeUser();//not implemented
                        System.out.println("User "+userName+" has left the server");

                    }

          }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }//end of main






 private static void removeUser() {
        // TODO Auto-generated method stub

    }

    private static void getActiveUsers() {
        // TODO Auto-generated method stub

    }

    private static void sendToAll() {
        // TODO Auto-generated method stub
    }

    }//end of class
  1. I can't check the contents of the ArrayList like that as the array is non-static, if I move the arraylist declaration into the main method, then the methods below won't actually work, like AddNewUSer();
  2. If I find a way to run that check of whether or not the user is a member, I don't know how to go from there to the actual operations. In psuedo-code its easy, but when you realise half the stuff you thought of doesn't work that way, it gets pretty frustrating.

That should read case switch instead of if selection. but I've got versions of this project with both.

Your methods are all static, and static methods cannot access instance variables without an instance. Your simplest fix is just to leave clientList where it is, but declare it as static so all the methods can access it. (There are all kinds of reasons why Java programs are usually not all static, but that's outside the scope of this particular issue - just be aware that it's generally a bad idea.)

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.