Search code examples
javaudpdatagram

How to use UDP with multiple Clients in Java


I have the following Situation.

I have a Server class. I have a Client class. I have a MultiServerThread class.

When a Client connects to a Server, the Server creates a new MultiServerThread, which is processing the Input from the Client. That way I can have multiple Clients. So far so good. The connection goes via TCP.

A short example:

Server class:

...
public static void main(String[] args) throws IOException {     
    ServerSocket serverSocket = null;
    boolean listening = true;


    try {
        serverSocket = new ServerSocket(9999);
    } catch (IOException e) {
      System.err.println("Could not listen on port: " +  serverSocket.getLocalPort() + ".");
        System.exit(-1);
    }

    while (listening) {
        new MultiServerThread(serverSocket.accept()).start();
    }
    serverSocket.close();
}
...

Client class:

...
public static void main(String[] args) throws IOException {

    socket = new Socket(hostname, port);
    out = new PrintWriter(socket.getOutputStream(), true);
    in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

    stdIn = new BufferedReader(new InputStreamReader(System.in));
    String fromServer;
    String fromUser;

    while ((fromServer = in.readLine()) != null) {
        System.out.println("Server: " + fromServer);
        if (fromServer.equals("Bye.")) {
            break;
        }

        fromUser = stdIn.readLine();
        if (fromUser != null) {
            System.out.println("Client: " + fromUser);
            out.println(fromUser);
        }
    }

    out.close();
    in.close();
    stdIn.close();
    socket.close();
}
...

MultiServerThread class:

...
public MultiServerThread(Socket socket) throws SocketException {
    super("MultiServerThread");
    this.socket = socket;
    //   dSocket = new DatagramSocket(4445);
}

public void run() {
    try {
        PrintWriter myOutput = new PrintWriter(socket.getOutputStream(), true);
        BufferedReader myInput = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        myOutput.println("Connected to client and ready to accept commands.");

        while ((clientInput = myInput.readLine()) != null) {

            //A SIMPLE LOGIN A USER
            if (clientInput.contains("!login")) {

            //save some string given by client into loggedUser
            String loggedUser = clientInput.substring(7);

            }

        myOutput.close();
        myInput.close();
        socket.close();

    } catch (IOException e) {
        e.printStackTrace();
    }
}

What I need is:

I need to implement a notification that comes from a Server when for example the Username is "Bob". If the username is "Bob", the server should give a notification to the Client "Bob is here again!". In my project/homework this should be done with datagrams in Java.

So if the clientinput is "!login bob" then a datagram packet with the message ("Bob is here again!") should be sent to the client.

Question: Where exactly should I put the code of the Datagram request in? Can I put the datagram packet request into the MultiServerThread or into the Client?

It would be easier in the MultiServerThread because it already handles the !login. Here:

     if (clientInput.contains("!login")) {
            //save some string given by client into loggedUser
            String loggedUser = clientInput.substring(7);

            //send datagram request to Server???

            }

But this is going against the principle of networking?


Solution

  • I got it working ;-)

    I definied a udp port in the thread and client class... the client class got his port with arguments... it gave the udp Port to the thread... so both had the udp ports ;)