简体   繁体   English

如何与Java中的虚拟机通信?

[英]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. 我编写了一个小Java程序来检查密码是否正确。 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. 正如您在代码中看到的那样,该程序要求某人输入他的密码,然后它会检查bdd.txt文件(扮演数据库角色)是否正确。

Actually I want to go to the next step, I want to deploy this program on an Android Device. 实际上,我想转到下一步,我想将此程序部署在Android设备上。

At first, I need the bdd.txt file on a virtual machine (such as Ubuntu for example) so it won't be local anymore. 首先,我需要虚拟机(例如Ubuntu)上的bdd.txt文件,因此它不再是本地的。 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? 这就是为什么我的问题是:如何与Java中的虚拟机通信? My program is running on my windows computer, I have installed Ubuntu with VMware, how can I reach a file in my VM? 我的程序在Windows计算机上运行,​​我已经在VMware上安装了Ubuntu,如何在VM中访问文件? (Web Services?) Is this possible? (Web服务?)这可能吗? 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. 假设虚拟机已配置了网络连接,则它应该具有IP地址,就像网络上的任何其他计算机一样。 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. 请注意,虚拟机的网络连接通常是为NAT配置的,这意味着只有主机可以直接连接到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. 如果您需要从VM主机以外的其他位置(例如,不是在主机上运行的模拟器的Android设备)连接到该主机,则需要为VM配置“桥接”网络,从而可以直接访问局域网。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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