コード例 #1
0
  /**
   * Send a message to a specified receiver address.
   *
   * @param msg message string to send.
   * @param receiverAddress Address of the place to send it to.
   * @param receiverPort the port to send it to.
   * @throws IOException If there is trouble sending this message.
   */
  protected void sendMessage(
      byte[] msg, String receiverAddress, int receiverPort, boolean reconnect) throws IOException {
    // msg += "\r\n\r\n";
    // Via is not included in the request so silently drop the reply.
    if (receiverPort == -1) {
      if (LogWriter.needsLogging)
        LogWriter.logMessage(
            "DEBUG, UDPMessageChannel, sendMessage(),"
                + " The message is not sent: the receiverPort=-1");
      throw new IOException("Receiver port not set ");
    }

    try {
      DatagramConnection socket;
      boolean created = false;

      if (stack.udpFlag) {
        // Use the socket from the message processor (for firewall
        // support use the same socket as the message processor
        // socket -- feature request # 18 from java.net). This also
        // makes the whole thing run faster!
        socket = ((UDPMessageProcessor) messageProcessor).dc;

      } else {
        // bind to any interface and port.
        // format: datagram://address:port
        String url = "datagram://" + peerAddress + ":" + peerPort;
        socket = (DatagramConnection) Connector.open(url);
        created = true;
      }

      Datagram reply = socket.newDatagram(msg, msg.length);
      socket.send(reply);
      if (created) socket.close();
    } catch (IOException ex) {
      throw ex;
    } catch (Exception ex) {
      InternalErrorHandler.handleException(ex);
    }
  }
コード例 #2
0
  /**
   * Send a message to a specified receiver address.
   *
   * @param msg message string to send.
   * @param receiverAddress Address of the place to send it to.
   * @param receiverPort the port to send it to.
   * @param receiverProtocol protocol to use to send.
   * @param retry try if connection was not successful at fi
   * @throws IOException If there is trouble sending this message.
   */
  protected void sendMessage(
      byte[] msg, String receiverAddress, int receiverPort, String receiverProtocol, boolean retry)
      throws IOException {
    if (LogWriter.needsLogging)
      LogWriter.logMessage(
          "Sending message to ["
              + receiverAddress
              + ":"
              + receiverPort
              + "/"
              + receiverProtocol
              + "]\n"
              + new String(msg)
              + "  to be sent to  ");

    // msg += "\r\n\r\n";
    // Via is not included in the request so silently drop the reply.
    if (receiverPort == -1) {
      if (LogWriter.needsLogging)
        LogWriter.logMessage(
            "DEBUG, UDPMessageChannel, sendMessage(),"
                + " The message is not sent: the receiverPort=-1");
      throw new IOException("Receiver port not set ");
    }
    if (Utils.compareToIgnoreCase(receiverProtocol, "UDP") == 0) {

      try {
        DatagramConnection socket;
        Datagram reply;
        boolean created = false;

        if (stack.udpFlag) {
          // Use the socket from the message processor (for firewall
          // support use the same socket as the message processor
          // socket -- feature request # 18 from java.net). This also
          // makes the whole thing run faster!
          socket = ((UDPMessageProcessor) messageProcessor).dc;

          // ArnauVP: this has the problem that the datagram created from this (inbound) connection
          // doesn't have an address assigned. Let's do it.
          reply = socket.newDatagram(msg, msg.length);
          reply.setAddress("datagram://" + peerAddress + ":" + peerPort);

        } else {
          // bind to any interface and port.
          // format: datagram://address:port
          socket = stack.getNetworkLayer().createDatagramSocket(peerAddress, peerPort);
          reply = socket.newDatagram(msg, msg.length);
          created = true;
        }
        if (LogWriter.needsLogging)
          LogWriter.logMessage(
              LogWriter.TRACE_DEBUG,
              "UDPMessageChannel, sendMessage(),"
                  + " Sending message over UDP, using socket "
                  + socket
                  + " created "
                  + created
                  + " destination "
                  + reply.getAddress());
        socket.send(reply);
        if (created) socket.close();
      } catch (IOException ex) {
        throw ex;
      } catch (Exception ex) {
        InternalErrorHandler.handleException(ex);
      }

    } else {
      // Use TCP to talk back to the sender.
      SocketConnection outputSocket =
          stack.ioHandler.sendBytes(peerAddress, peerPort, "tcp", msg, retry);
      OutputStream myOutputStream = stack.ioHandler.getSocketOutputStream(outputSocket);
      myOutputStream.write(msg, 0, msg.length);
      myOutputStream.flush();
      // The socket is cached (don't close it!);
    }
  }