简体   繁体   中英

How to communicate with a Virtual Machine in Java?

I made a little Java program to check if a pin code is correct or not. As you can see in the code, the program asks someone to enter his pin code, then it checks in a bdd.txt file (playing the database role) if the pin is correct or not.

Actually I want to go to the next step, I want to deploy this program on an Android Device.

At first, I need the bdd.txt file on a virtual machine (such as Ubuntu for example) so it won't be local anymore. That is more realistic. I have to keep in mind that in the future, the device will ask if the pin entered is good or not, and of course all the pins won't be on the device, so the all the checking process won't be local.

That is why my question is: how to communicate with a virtual machine in Java? My program is running on my windows computer, I have installed Ubuntu with VMware, how can I reach a file in my VM? (Web Services?) Is this possible? And if yes, how?

The code :

import java.io.*;
import java.util.*;

public class Main {

  // Fonction permettant d'accéder/lire notre BDD de pins (fichier .txt)
  static public boolean readPinsData(File dataFile, ArrayList<Integer> data) {
    boolean err = false;
    try {
      Scanner scanner = new Scanner(dataFile);
      String line;
      while (scanner.hasNext()) {
        line = scanner.nextLine();
        try {
          data.add(Integer.parseInt(line));
        } catch (NumberFormatException e) {
          e.printStackTrace();
          err = true;
        }
      }
      scanner.close();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
      err = true;
    }

    return err;
  }

  public static void main(String[] args) {

    // Définition int & booléen nécessaires ici
    System.out.println("-----------------------");
    System.out.println("APPLICATIONS BESOINS");
    System.out.println("-----------------------");
    Console console = System.console();

    //System.out.println(console == null);

    int pinSize = 0;
    int nbTry = 0;
    boolean authenticated = false;

    // On va demander à saisir un pin de 4 digits : tant que l'utilisateur
    // ne saisit pas 4 digits, on boucle
    do {
      do {

        char passwordArray[] = console.readPassword("Enter pin: "); // Demande
                                      // de
                                      // saisie
                                      // du
                                      // pin
        pinSize = passwordArray.length;

        if (pinSize != 4) { // On prévient l'utilisateur que le pin se
                  // compose de 4 chiffres, sinon Ok on passe
                  // à la vérif.
          System.out.println("Pin must be 4 digits");
        } else {
          System.out.println("Checking...");
        }

        ArrayList<Integer> pins = new ArrayList<Integer>(); // Nos pins
                                  // sont mis
                                  // dans un
                                  // Liste
        readPinsData(new File("bdd.txt"), pins); // On lit dans notre
                              // BDD de pins
        // System.out.println(pins);
        // System.out.println(passwordArray);

        String[] thePins = new String[pins.size()];
        for (int i = 0; i < thePins.length; i++) {
          thePins[i] = pins.get(i).toString();
        }

        String passEntered = String.valueOf(passwordArray);

        for (int i = 0; i < thePins.length; i++) { // Recherche si pin
                              // bon et on print
                              // :)
          if (passEntered.equals(thePins[i]) && pinSize == 4) {
            System.out.println(":)");
            authenticated = true;
            break;
          }
        }

      } while (pinSize != 4); // Si la on saisit un pin qui n'a pas 4
                  // chiffres, on ne peux valider la demande,
                  // donc le nombre d'Essais n'est pas déduit
      if (!authenticated && pinSize == 4) { // Dans ce cas, on incrémente
                          // le nombre d'essais car le
                          // pin saisit n'est pas dans
                          // notre BDD et on print :(
        System.out.println(":(");
        nbTry++;
      }
    } while (nbTry < 3 && !authenticated);
  }
}

The "database" :

1111
2222
3333
4444
5555
6666
7777
8888
9999

Assuming that the virtual machine has been configured with a network connection, it should have an IP address just like any other computer on a network. Just open a network connection like you would to any other computer.

Note that virtual machines' network connections are often configured for NAT, meaning that only the host computer has a direct connection to the VM. If you need to connect to it from somewhere other than the VM's host computer — such as an Android device that's not an emulator running on the host — you'll need to configure the VM for "bridged" networking, which gives it direct access to the LAN.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM