next up previous contents
Next: Example Up: Connector object Previous: Connector object   Contents

Examples

Set the connector:

  try {
    // create instance of Connector
    Connector conn = Connector.createInstance(localhost,60201);
    // instantiate and set the listener
    MyListener listener = new MyListener();
    conn.setListener(listener,null);
    // start the listener thread
    conn.startThread();
  } catch (Exception e) {
    // code to handle exception
  } catch (IOException e) {
    // code to handle exception
  } catch (UnknownHostException e) {
    // code to handle exception
  }
Send a message:
  Message m = new Message("my command", "my 1st parameter");
  m.addParameter("my 2nd parameter");
  m.addParameter(3);
  try {
    Connector.sendMessage(m);
  } catch (Exception e) {
    // code to handle exception
  }
Receive messages:
public class MyListener implements MessageListener {

  public void processIncomingMessage(Message m, Object o) {
    System.out.println("command: "+m.getCommand());
    m.resetParCount();
    while(m.hasMorePars()) {
      String p=m.getNextParameter();
      System.out.println("parameter: "+p);      
    }
  }
}

NOTE! it the connector is started with the ability of listening for incoming messages it starts a background thread, it is mandatory to stop the backgraound thread before stopping the execution of the application otherwise the application will not stop.



2005-07-12