title: Multi threaded server and client Java specify the client thread
body: <p>I made a UDP server client architecture with multi-thread the problem is when I send from the server to the client another client thread get the packet and so for all .. how can I specify the thread in which UDP should send back the packet ?? </p>
  
  <pre><code> Client
  
   public UDPClient(int port) throws SocketException
  {
      this.socket = new DatagramSocket();
      this.arg1 = (int) (Math.random() * 1000) ;
      this.arg2 = (int) (Math.random() * 1000) ;
      this.port = port ;
  }
      public void run()
  {
      try{
          String x = arg1 + " + " + arg2;
          BUFFER = x.getBytes();
          InetAddress ip = InetAddress.getByName("127.0.0.1");
          packet = new DatagramPacket(BUFFER , BUFFER.length,ip,port);
          printOutput(new String("Client send" + x));
          socket.send(packet);
          socket.receive(packet);
          String output = new String(packet.getData(),0,packet.getLength());
          printOutput(new String("receive " + x + "=" + output));
      }
      catch(IOException e)
      {
          System.out.println("UDP sending problem " + e.getMessage());
      }
  }
  </code></pre>
  
  <p>Server</p>
  
  <pre><code>public void run()
  {
      while(true)
      {
          try{
              packet = new DatagramPacket(BUFFER,BUFFER.length);
              socket.receive(packet);
              executor.execute(new UDPServerCore(socket,packet,BUFFER));
          }
          catch(IOException e)
          {
              System.out.println("UDP receiving packet problem "
              + e.getMessage());
          }
      }
  }
  </code></pre>
  
  <p>ServerCore</p>
  
  <pre><code>@Override
  public void run(){
      String x = new String(packet.getData(),0,packet.getLength());
      String y = parseString(x);
      BUFFER = y.getBytes();
      //packet.setData(y.getBytes());
      DatagramPacket res = new DatagramPacket(BUFFER , BUFFER.length 
      ,packet.getAddress(),packet.getPort());
      try{
      socket.send(res);
      }
      catch(IOException e)
      {
          System.out.println("Something went wrong " + e.getMessage());
      }
  }
  
  synchronized private static String parseString(String x )
  {
      String arr[] = x.split(" ");
      int z = Integer.parseInt(arr[0]);
      int y = Integer.parseInt(arr[2]);
      y = y + z;
      writeServer.append(x+ "=" + y +"\n");
      return String.valueOf(y);
  }
  </code></pre>
  
  <p>I solved it as the problem was when i created the port of the thread i should have create all the client threads with specified port first then starting each thread.</p>