问题
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.
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.
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?
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();
}
}
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;
}
}
回答1:
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:
- User inputs a number
- Client sends user's number to server
- Client receives response from server, and displays it
Server side:
- 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
I have written some simple code to show you, excluding UI stuff:
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:
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;
}
来源:https://stackoverflow.com/questions/22683811/how-to-access-a-file-in-client-server-architecture-using-sockets-in-java