[英]How to access a file in Client-Server architecture using Sockets in Java?
I need to read a "bdd.txt" file placed on a Virtual Machine on my computer.我需要读取放置在我计算机上的虚拟机上的“bdd.txt”文件。 I made a client/server system in Java.我用 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.我的 Server.java 在我的 VM (Ubuntu) 上,有一个“数据库”(同一文件夹中的 bdd.txt 文件),而我的 Client.java 在我的 Windows 7 上。
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.到目前为止,我已将代码拆分为 2 个不同的文件(服务器/客户端),并在 Windows 7 和 VMware Player 的 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.当我在我的 VM 上启动我的服务器时,它会侦听端口号 x,然后我回到我的 Windows 并运行我的客户端。 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.它要求建立连接,然后返回我的 VM,我打印一条消息“连接已建立”并且我的应用程序正在运行。 So now I can communicate between them.所以现在我可以在他们之间进行交流了。 I have just used socket = new Socket("my VM ip address",portNumber);我刚刚使用了 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.但是现在,我不知道如何调整我的代码以访问我在 VM 上移动的 bdd.txt 文件。
How can I now read the bdd.txt file, to have access to the pin codes ?我现在如何读取 bdd.txt 文件以访问密码? Why is my new Client()
never called in my program?为什么我的new Client()
从未在我的程序中调用?
Here is Client.java :这是 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();
}
}
Here is Server.java (in the same folder as bdd.txt):这是 Server.java(与 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;
}
}
There's a lot of irrelevant stuff in you question, it's hard to see how your program works.你的问题中有很多不相关的东西,很难看出你的程序是如何工作的。
Here's what should happen:这是应该发生的事情:
Client side:客户端:
Server side:服务器端:
bbd.txt
服务器根据文件bbd.txt
检查数字yes
else return no
如果文件中存在数字,则返回yes
否则返回no
I have written some simple code to show you, excluding UI stuff:我已经写了一些简单的代码给你看,不包括 UI 的东西:
Client.java:客户端.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:服务器.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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.