如何使用Java中的套接字访问客户机-服务器体系结构中的文件?
我需要读取一个“bdd.txt”文件放在我的计算机上的虚拟机上。我用Java制作了一个客户机/服务器系统。我的Server.java在我的VM(Ubuntu)上,有一个“数据库”(同一文件夹中的bdd.txt文件),我的Client.java在我的Windows 7上 到目前为止,我已经将我的代码分为两个不同的文件(服务器/客户端),并在Windows7和VMware Player的Ubuntu之间建立了连接。当我在虚拟机上启动服务器时,它会监听端口号为x的端口,然后我返回Windows并运行客户端。它要求建立连接,然后,在我的虚拟机上,我打印一条消息“连接已建立”,我的应用程序正在运行。现在我可以和他们交流了。我刚刚使用了socket=newsocket(“我的虚拟机ip地址”,端口号);它是有效的。但是现在,我不知道如何调整代码以达到我在虚拟机上移动的bdd.txt文件 我现在如何读取bdd.txt文件才能访问pin码? 为什么我的如何使用Java中的套接字访问客户机-服务器体系结构中的文件?,java,sockets,client-server,Java,Sockets,Client Server,我需要读取一个“bdd.txt”文件放在我的计算机上的虚拟机上。我用Java制作了一个客户机/服务器系统。我的Server.java在我的VM(Ubuntu)上,有一个“数据库”(同一文件夹中的bdd.txt文件),我的Client.java在我的Windows 7上 到目前为止,我已经将我的代码分为两个不同的文件(服务器/客户端),并在Windows7和VMware Player的Ubuntu之间建立了连接。当我在虚拟机上启动服务器时,它会监听端口号为x的端口,然后我返回Windows并运行客
new Client()
从未在我的程序中调用过?
下面是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位于同一文件夹中):
你的问题中有很多不相关的东西,很难看出你的程序是如何工作的 下面是应该发生的事情: 客户端:
- 用户输入一个数字
- 客户端将用户号码发送到服务器
- 客户端从服务器接收响应,并显示它
- 服务器侦听客户端连接
- 服务器从客户端接收号码
- 服务器根据文件
bbd.txt
- 如果文件中存在编号,则返回
else返回yes
no
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;
}
我们不需要截图或100码的Swing代码来回答一个关于套接字的简单问题。我想最好能理解我正在尝试做什么。不必那样说,给我一些帮助怎么样?学习一下sockets的基本文档,这对你很有帮助。问一个不需要每个人都涉过不相干领域的问题怎么样?试着找出你真正想问的是什么?如何提高获得答案的概率?这不是你的个人支持台。我们是志愿者。我建议你照此行事。这是为了你自己的利益。@Sathesh,我有谷歌“用Java编写的基于文件的web服务器”,但到目前为止没有什么帮助。谢谢NickJ,但这样我就不再使用文件了?由于我的“数据库”是一个文件,我是否应该将请求更改为文件?我已更新了答案,以包含checkPin()的代码。那是你读文件的地方。如果用户在文件中输入PIN,则返回true,否则返回false。我还没有检查它,您需要在编译它之前添加异常处理。谢谢,只有一个问题,在Client.java BufferedReader和PrintWriter中都使用socket.getIn/OutputStream。这是“socket”还是“clientSocket”与前面的声明相同?我尝试了此解决方案,但无法使其正常工作。很抱歉,Client.java中应该是clientSocket
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;
}