问题描述
我需要读取放置在我的计算机上的虚拟机上的bdd.txt文件。我在Java做了一个客户端/服务器系统。我的Server.java是在我的VM(Ubuntu)与一个数据库(在同一文件夹中的bdd.txt文件),我的Client.java是在我的Windows 7。
I need to read a "bdd.txt" file placed on a Virtual Machine on my computer. I made a client/server system in Java. My Server.java is on my VM (Ubuntu) with a "database" (the bdd.txt file in the same folder), and my Client.java is on my Windows 7.
到目前为止,我已经将我的代码拆分为两个不同的文件(服务器/客户端),我建立了我的Windows 7和我的VMware Player的Ubuntu之间的连接。当我在我的VM上启动我的服务器,它侦听端口号x,然后我回到我的Windows并运行我的客户端。它要求建立连接,然后,回到我的VM,我打印一条消息连接已经建立,我的应用程序正在运行。所以现在我可以在他们之间沟通。我刚刚使用socket = new Socket(我的VM的IP地址,portNumber);它的工作原理。但现在,我不知道如何适应我的代码,以达到我在我的虚拟机上移动的bdd.txt文件。
So far I have split my code into 2 different files (Server/Client) and I made the connexion between my Windows 7 and my VMware Player's Ubuntu. When I start my server on my VM, it listens on a port number x, then I go back on my Windows and run my client. It asks to make the connexion and then, back on my VM, I print a message "The connexion is made" and my app is running. So now I can communicate between them. I have just used socket = new Socket("my VM ip address",portNumber); and it works. But now, I have no idea how to adapt my code to reach my bdd.txt file I moved on my VM.
我现在如何读取bdd .txt文件,有权访问pin码?
为什么我的新客户端()
从未在我的程序中调用?
How can I now read the bdd.txt file, to have access to the pin codes ?Why is my new Client()
never called in my program?
is Client.java:
Here is Client.java :
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;
public class Client {
public static void main(String[] args) throws IOException {
int pinSize = 0;
//set up server communication
Socket clientSocket = new Socket(InetAddress.getLocalHost(),1234);
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
PrintWriter out = new PrintWriter(clientSocket.getOutputStream());
Scanner scanner = new Scanner(System.in);
System.out.println("Enter pin : ");
String password = scanner.next();
pinSize = password.length();
//send PIN to server
out.println(password);
if (pinSize != 4) {
System.out.println("Pin must be 4 digits");
} else {
System.out.println("Checking...");
}
out.flush();
//get response from server
String response = in.readLine();
System.out.println(response);
in.close();
out.close();
clientSocket.close();
}
}
这里是Server.java bdd.txt):
Here is Server.java (in the same folder as bdd.txt):
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
private static ServerSocket server;
public static void main(String[] args) throws Exception {
server = new ServerSocket(1234);
Socket socket = server.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream());
//Listen for client requests:
String request;
while ((request = in.readLine()) != null) {
//check PIN, send result
boolean pinCorrect = checkPin(request);
out.println(pinCorrect ? "yes" : "no");
out.flush();
}
out.close();
in.close();
socket.close();
}
/**
* Check if PIN is in bdd.txt
* @throws IOException
*/
private static boolean checkPin(String pin) throws IOException {
boolean result = false;
File file = new File("bdd.txt");
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String line;
while ((line = in.readLine()) != null) {
result |= (line.equals(pin));
}
in.close();
return result;
}
}
推荐答案
以下是应该发生的情况:
Here's what should happen:
客户端:
- 用户输入一个号码
- 客户端将用户号码发送到服务器
- 客户端从服务器接收响应并显示
服务器端:
- 服务器侦听客户端连接
- 服务器从客户端接收号码
- 服务器检查文件
bbd.txt
- 如果文件中存在数字,则返回
yes
else returnno
- Server listens for client connection
- Server receives number from client
- Server checks number against file
bbd.txt
- If number exists in file, return
yes
else returnno
我写了一些简单的代码,不包括UI内容:
I have written some simple code to show you, excluding UI stuff:
Client.java:
Client.java:
public static void main(String[] args) {
//set up server communication
Socket clientSocket = new Socket("ip.address",1234);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream());
//send PIN to server
out.println("9.8.7.6");
out.flush;
//get response from server
String response = in.readLine();
System.out.println(response);
in.close();
out.close();
clientSocket.close();
}
Server.java:
Server.java:
public static void main(String[] args) throws Exception {
//Set up client communication
ServerSocket server = new ServerSocket(1234);
Socket socket = server.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream());
//Listen for client requests:
String request;
while ((request = in.readLine()) != null) {
//check PIN, send result
boolean pinCorrect = checkPin(request);
out.println(pinCorrect ? "yes" : "no");
out.flush();
}
out.close();
in.close();
socket.close();
}
/**
* Check if PIN is in bdd.txt
*/
private static boolean checkPin(String pin) {
boolean result = false;
File file = new File("bdd.txt");
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String line;
while ((line = in.readLine()) != null) {
result |= (line.equals(pin));
}
in.close();
return result;
}
这篇关于如何使用Java中的Sockets访问Client-Server体系结构中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!