title: MQTT receiving message from topic
body: <p>i'm writing a program that receiving message from MQTT broker
  The server get ID from client and make it become name of topic: topic1, topic2 for examples.
  Then when subscribe, the server pass the topic's name and then read message from that topic.
  This is my server:</p>
  
  <pre><code>public class AnalyticServer {
  
        // The server socket.
        private static ServerSocket serverSocket = null;
        // The client socket.
        private static Socket clientSocket = null;
  
        // This server can accept up to maxClientsCount clients' connections.
        private static final int maxClientsCount = 5;
        private static final clientThread[] threads = new clientThread[maxClientsCount];
  
        public static void main(String args[]) throws MqttException, InterruptedException {
  
  
  
  
  
          // The default port number.
          int portNumber = 4544;
  
          //Open Server
          try {
            serverSocket = new ServerSocket(portNumber);
          } catch (IOException e) {
            System.out.println(e);
          }
          //
  
  
          //When server is listening
          System.out.println("Server is now listening at port 4544");
          while (true) {
            try {
              //Make connection 
  
              clientSocket = serverSocket.accept();
  
              System.out.println("Connected");
  
  
  
              int i = 0;
              //Find thread null to run the connection
              for (i = 0; i &lt; maxClientsCount; i++) {
                if (threads[i] == null) {
                  (threads[i] = new clientThread(clientSocket, threads)).start();
                  break;
                }
              }
              if (i == maxClientsCount) {
                PrintStream os = new PrintStream(clientSocket.getOutputStream());
                os.println("Server is now, please try again later");
                os.close();
                clientSocket.close();
              }
            } catch (IOException e) {
              System.out.println(e);
            }
          }
        }
      }
  
      //Thread control each Request
      class clientThread extends Thread {
  
  
        private Socket clientSocket = null;
        private final clientThread[] threads;
        private int maxClientsCount;
  
        public clientThread(Socket clientSocket, clientThread[] threads) {
          this.clientSocket = clientSocket;
          this.threads = threads;
          maxClientsCount = threads.length;
        }
  
        public void run() {
          int maxClientsCount = this.maxClientsCount;
          clientThread[] threads = this.threads;
  
  
          try {
              int identifier=0;
              //get id
               InputStream input = null;
               input = clientSocket.getInputStream();
  
  
               identifier=input.read();
  
               String topic="topic".concat(String.valueOf(identifier));
  
              //Subscribe
               try {
                  System.out.println("subscribing");
                  Subscribe receive=new Subscribe(topic);
  
  
              } catch (MqttException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              } catch (URISyntaxException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              } 
               // open image
               FileInputStream imgPath = new FileInputStream("image.jpg");
               BufferedImage bufferedImage = ImageIO.read(imgPath);
  
               Thread.sleep(1200);
               ByteArrayOutputStream baos = new ByteArrayOutputStream();
               ImageIO.write( bufferedImage, "jpg", baos );
               baos.flush();
               byte[] imageInByte = baos.toByteArray();
               baos.close();
  
  
  
            //SendImage
            DataOutputStream outToClient = new DataOutputStream(clientSocket.getOutputStream());        
            outToClient.write(imageInByte);
            System.out.println(outToClient.size());
  
            clientSocket.close();
  
          } catch (IOException e) {
          } catch (InterruptedException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          } 
        }
      }
  </code></pre>
  
  <p>'And this is my subscribe class</p>
  
  <pre><code>public class Subscribe implements MqttCallback {
  
      private final int qos = 1;
      static String topic=null;
  
      private MqttClient client;
  
      String subText = "abc";
      public Subscribe(String topic) throws MqttException, URISyntaxException {
          this.topic=topic;
  
          String host = "tcp://m14.cloudmqtt.com:19484"; 
          String username = "***";
          String password = "********";
          String clientId = MqttClient.generateClientId(); 
          MqttConnectOptions conOpt = new MqttConnectOptions(); 
          conOpt.setCleanSession(true);
          conOpt.setUserName(username);
          conOpt.setPassword(password.toCharArray());
  
  
  
          this.client = new MqttClient(host, clientId, new MemoryPersistence()); 
  
     ;
          this.client.setCallback(this);
  
          this.client.connect(conOpt);
          this.client.subscribe(topic,1);
          System.out.println("subscribe topic: " +this.topic);
      }
  
  
  
  
  
      /**
       * @see MqttCallback#connectionLost(Throwable)
       */
      public void connectionLost(Throwable cause) {
          System.out.println("Connection lost because: " + cause);
          System.exit(1);
      }
  
      /**
       * @see MqttCallback#deliveryComplete(IMqttDeliveryToken)
       */
      public void deliveryComplete(IMqttDeliveryToken token) { 
      }
  
      /**
       * @throws IOException 
       * @see MqttCallback#messageArrived(String, MqttMessage)
       */
  
      public void messageArrived(String topic, MqttMessage message) throws MqttException, IOException {
           System.out.println("1");
          subText = message.getPayload().toString();
  
          System.out.println("Received"+subText);
  
      }
  }
  </code></pre>
  
  <p>The topic in subscribe constructor is correct, however, the         this.client.setCallback(this) seem to not call the method messageArrived. So i can not receive anything.</p>
  
  <p>Anybody know about it?
  Thank you so much</p>