title: How to use UDP with multiple Clients in Java
body: <p>I have the following Situation.</p>
  
  <p>I have a Server class.
  I have a Client class.
  I have a MultiServerThread class.</p>
  
  <p>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.</p>
  
  <p>A short example:</p>
  
  <p><strong>Server class:</strong></p>
  
  <pre><code>...
  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();
  }
  ...
  </code></pre>
  
  <p><strong>Client class:</strong></p>
  
  <pre><code>...
  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();
  }
  ...
  </code></pre>
  
  <p><strong>MultiServerThread class:</strong></p>
  
  <pre><code>...
  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();
      }
  }
  </code></pre>
  
  <p>What I need is:</p>
  
  <p>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.</p>
  
  <p>So if the clientinput is "!login  bob" then a datagram packet with the message ("Bob is here again!") should be sent to the client.</p>
  
  <p>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?</p>
  
  <p>It would be easier in the MultiServerThread because it already handles the !login.
  Here:  </p>
  
  <pre><code>     if (clientInput.contains("!login")) {
              //save some string given by client into loggedUser
              String loggedUser = clientInput.substring(7);
  
              //send datagram request to Server???
  
              }
  </code></pre>
  
  <p>But this is going against the principle of networking?</p>