Exemplo n.º 1
0
  private void init() throws IOException {
    messageBuffer = new ArrayList();
    receiveBuffer = ByteBuffer.allocate(CAPACITY);
    sendBuffer = ByteBuffer.allocate(CAPACITY);
    buf = new byte[CAPACITY];

    channel = DatagramChannel.open();
    channel.configureBlocking(false);

    InetAddress host = null;
    if (getAddress() != null) {
      host = InetAddress.getByAddress(getAddress().getBytes());
    }
    channel.socket().bind(new InetSocketAddress(host, getPort()));
    channel.socket().setReceiveBufferSize(CAPACITY);

    certifier = new MessageCertifier(this);
    extractor = new MessageExtractor(this);
  }
Exemplo n.º 2
0
 static void pinging() throws IOException {
   Socket mySocket;
   if (!inetAddresses.isEmpty()) {
     bcid =
         ((InterfaceAddress) inetAddresses.get(0))
             .getBroadcast()
             .getAddress(); // gets the last ip address in subnet
     netaddress(
         ((InterfaceAddress) inetAddresses.get(0))
             .getNetworkPrefixLength(), // creats the mask field
         ((InterfaceAddress) inetAddresses.get(0))
             .getAddress()
             .getAddress() // creats the myip field
         );
     byte[] pingip = netid;
     for (; ; ) {
       InetAddress ping = InetAddress.getByAddress(pingip);
       //   boolean reachable=ping.isReachable(5000);     // This command is doing the same work as
       // the ping command in windows
       try {
         Process ps = Runtime.getRuntime().exec("ping.exe " + ping);
         BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));
         StringBuilder sb = new StringBuilder();
         String line;
         while ((line = br.readLine()) != null) {
           sb.append(line);
           sb.append("\n");
         }
         y = sb.toString();
         System.out.print(sb.toString());
       } catch (IOException e) {
         e.printStackTrace();
       }
       if (!contains(y, substring)) {
         System.out.println("The ip address " + ping + "is reachable");
         for (int port = 0; port < 65536; port++) {
           try {
             mySocket = new Socket(ping, port);
             System.out.println("The port" + port + "on ip:" + ping + "is free.");
             mySocket.close();
           } catch (IOException c) {
             System.out.println("The port" + port + "on ip:" + ping + "is closed.");
           }
         }
       } else
         System.out.println(
             "The pinged ip addres is unreachable, probably it is not up in your subnet");
       pingip[3]++;
       if (pingip[3] == (byte) 255) pingip[2]++;
       if (pingip[2]
           == (byte)
               255) // here we ping ips in our subnet from the first ip address to the last one
       pingip[1]++;
       if (pingip[1] == (byte) 255) pingip[0]++;
       if (pingip[0]
           == (byte)
               127) // beacuase the ip addresses that are not in class A have at least 2 bytes of
                    // net ids
       break;
       if (pingip[0] == bcid[0]
           & pingip[1] == bcid[1]
           & pingip[2] == bcid[2]
           & pingip[3] == bcid[3]) break;
     }
   } else System.out.println("null pointer exception");
 }
  /** {@inheritDoc} */
  @Override
  public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
    super.readExternal(in);

    addr = InetAddress.getByAddress(U.readByteArray(in));
  }