Code example for DatagramSocket

Methods: getLocalPort, send

0
  /** 
   * Sends a single broadcast discovery request. 
   */ 
  private void sendProbe() { 
    DatagramPacket packet = makeRequestPacket(DESIRED_SERVICE,
        mSocket.getLocalPort());
    try { 
      mSocket.send(packet);
    } catch (IOException e) {
      Log.e(LOG_TAG, "Exception sending broadcast probe", e);
      return; 
    } 
  } 
 
  /** 
   * Immediately stops the receiver thread, and cancels the probe timer. 
   */ 
  public void stop() { 
    if (mSocket != null) {
      mSocket.close();
    }