[英]How to communicate with a Virtual Machine in Java?
我制作了一個小的Java程序,要求人們輸入密碼。 輸入密碼后,它將讀入一個“ bdd.txt”文件,其中存儲了所有引腳,然后顯示:)(如果良好)和:(如果錯誤。到目前為止,很簡單的應用程序。
我想要做的是將“數據庫”文件移到計算機上的虛擬機(例如Ubuntu)中,然后執行相同的操作。 這樣,它將不再是本地的,因為該文件將不再位於我項目的根目錄下。
這是我的應用程序的外觀:
如您所見,該應用程序啟動,要求用戶輸入密碼。 如果這是一個好的選擇,則應用程序完成,否則,他還有2次嘗試,直到應用程序停止。
輸入圖釘后,我的程序會在“ bdd.txt”中檢查圖釘是否存在。 它扮演數據庫角色:
要了解我的需求,有必要將該程序吸收到需要安全的內容中。 我們不希望引腳數據庫與程序(或現實生活中的設備)位於同一位置。 因此,我們將其放在虛擬機上,並且必須在Eclipse中的Windows7 Java程序與VMWare Player的Ubuntu上的bdd.txt文件之間進行通信。
我的問題是怎么可能? 我如何更改代碼以使程序到達VM上的某些內容? 我應該使用一種特定的技術嗎? 我需要首先進行一些配置嗎?
這是我的代碼:
import java.io.*;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import javax.swing.*;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
import javax.swing.text.PlainDocument;
import java.awt.*;
import java.awt.event.*;
public class Main extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel container = new JPanel();
private JPasswordField p1 = new JPasswordField(4);
private JLabel label = new JLabel("Enter Pin: ");
private JButton b = new JButton("OK");
public Main() {
this.setTitle("NEEDS");
this.setSize(300, 500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
container.setBackground(Color.white);
container.setLayout(new BorderLayout());
container.add(p1);
JPanel top = new JPanel();
PlainDocument document =(PlainDocument)p1.getDocument();
b.addActionListener(new BoutonListener());
top.add(label);
top.add(p1);
p1.setEchoChar('*');
top.add(b);
document.setDocumentFilter(new DocumentFilter(){
@Override
public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
String string =fb.getDocument().getText(0, fb.getDocument().getLength())+text;
if(string.length() <= 4)
super.replace(fb, offset, length, text, attrs); //To change body of generated methods, choose Tools | Templates.
}
});
this.setContentPane(top);
this.setVisible(true);
}
class BoutonListener implements ActionListener {
private final AtomicInteger nbTry = new AtomicInteger(0);
ArrayList<Integer> pins = readPinsData(new File("bdd.txt"));
@SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent e) {
if (nbTry.get() > 2) {
JOptionPane.showMessageDialog(null,
"Pin blocked due to 3 wrong tries");
return;
}
final String passEntered=p1.getText().replaceAll("\u00A0", "");
if (passEntered.length() != 4) {
JOptionPane.showMessageDialog(null, "Pin must be 4 digits");
return;
}
//JOptionPane.showMessageDialog(null, "Checking...");
//System.out.println("Checking...");
SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
@Override
protected Void doInBackground() throws Exception {
boolean authenticated = false;
if (pins.contains(Integer.parseInt(passEntered))) {
JOptionPane.showMessageDialog(null, ":)");
authenticated = true;
}
if (!authenticated) {
JOptionPane.showMessageDialog(null, ":(");
nbTry.incrementAndGet();
}
return null;
}
};
worker.execute();
}
}
//Function to read/access my bdd.txt file
static public ArrayList<Integer> readPinsData(File dataFile) {
final ArrayList<Integer> data=new ArrayList<Integer>();
try {
BufferedReader reader = new BufferedReader(new FileReader(dataFile));
String line;
try {
while ((line = reader.readLine()) != null) {
try {
data.add(Integer.parseInt(line));
} catch (NumberFormatException e) {
e.printStackTrace();
System.err.printf("error parsing line '%s'\n", line);
}
}
} finally {
reader.close();
}
} catch (Exception e) {
e.printStackTrace();
System.err.println("error:"+e.getMessage());
}
return data;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Main();
}
});
}
}
有任何想法嗎 ? 謝謝,
弗洛朗。
共享文件夾當然可以工作,但是擁有VM似乎毫無意義,因為PIN文件也位於您的主機上,並且Java正在直接讀取它。
也許您需要客戶端/服務器架構?
您使用UI進行編程將是客戶端。 將為客戶端配置一種調用服務器的方式(IP地址和端口)。 客戶端無權訪問bdd.txt文件,但服務器具有訪問權。
在您的VM上,您還有另一個Java應用程序,即服務器。 您的服務器偵聽來自客戶端的請求。 該請求將包含用戶輸入的PIN。 然后,服務器根據文件中的PIN對其進行檢查,並以是或否做出響應。 您的客戶端從服務器收到是/否響應,並將結果報告給用戶。
在這里閱讀有關套接字編程的入門
您需要做兩件事:
讓您的應用程序從共享文件夾中讀取Pin文件。 這意味着更改此行:
ArrayList<Integer> pins = readPinsData(new File("bdd.txt"));
現在,此代碼正在從用戶所在的當前目錄中讀取文件bdd.txt,我認為這是可執行文件所在的目錄。相反,您希望它指向共享目錄中的pin文件。 為了使代碼盡可能靈活,啟動程序時,您可能希望將命令行文件的路徑作為命令行參數傳遞。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.