コード例 #1
0
  private static void requestLocalServerInformation(final ComputerLocal xcl) {

    //    int ownid = -1;
    //    for (int i = 0; i < Constants.getVecCl().size(); i++) {
    //      if (xcl.equals(Constants.getVecCl().get(i))) {
    //        ownid = i;
    //      }
    //    }

    for (ComputerLocal c : Constants.getVecCl()) {

      //
      // Send "HELLO" - message
      DatagramPacket dp =
          Utils.createDatagramPackage(
              Staticio.SERVER_INFO_REQUEST + xcl.generateInformationString(),
              xcl.getLocalLocation(),
              Utils.getBroadcastAddressLocalhost());
      Staticio.handleMessageIn(dp, c, null);
    }
  }
コード例 #2
0
  /**
   * Sends the String {@value #SERVER_INFO_REQUEST} contained by the static and final String {@link
   * #SERVER_INFO_REQUEST} to server with specified InetAdress and port.
   *
   * @param xadr the server's InetAddress,
   * @param xport the server's port.
   * @return the DatagramPacket that consists of the server's response. In case the operation
   *     failed, the return value is null.
   * @author Julius Huelsmann
   * @version %I%, %U%
   * @since 1.0
   */
  public static final DatagramPacket requestServerInformation(
      final InetAddress xadr, final int xport, final ComputerLocal xcl) {

    //
    // Threshold: if the instance of InetAddress is null, return null
    if (xadr == null) {
      return null;
    }

    DatagramSocket socket = null;
    try {

      // Send QUESTION:
      // Open socket, send datagramPackage and close the socket.
      socket = new DatagramSocket(xport);
      socket.setSoTimeout(Constants.socketTimeout);
      // Generate DatagramPacket and DatagramSocket for the transmission.
      // Generate information string and prepare DatagramPacket that will be
      // sent during the next step.
      final String infoString = Staticio.SERVER_INFO_REQUEST + xcl.generateInformationString();
      final String requestStatus =
          Staticio.sendStringServerinfoRequest(xadr, xport, xcl, socket, infoString);
      socket.close();
      socket = null; //
      //      View.print(requestStatus);

      socket = new DatagramSocket(xport);
      socket.setSoTimeout(Constants.socketTimeout);

      byte[] buf1 = new byte[Constants.BUFFERSIZE];

      DatagramPacket receivedData = new DatagramPacket(buf1, buf1.length);
      socket.receive(receivedData);

      //      final String messageIn =
      new String(receivedData.getData()).substring(0, receivedData.getLength());

      //
      // If the message's origin is not the current computer.
      if (!receivedData.getAddress().getHostAddress().equals(FetchServer.getAddress())) {

        View.networkout(
            FetchClient.class.getSimpleName(),
            receivedData.getAddress().getHostAddress(),
            xport,
            infoString,
            true);
        Staticio.handleMessageIn(receivedData, xcl, socket);
      }

      socket.close();
      socket = null;

      buf1 = null;
      return receivedData;
    } catch (IOException e) {
      System.out.println(e);
      try {

        if (socket != null) {

          socket.close();
          socket = null;
        }
      } catch (Exception i) {
        return null;
      }
      return null;
    }
  }