Java 服务器从客户端读取数据不工作
编辑——我已经包括了一些测试类。遗憾的是,对于这些测试文件,服务器没有将任何数据写入它从客户端接收到的文件。我也不知道为什么。这是我能给的最好的自动取款机了。再次编辑-另外,我注意到在测试时,客户端将读入测试文本文件并在终端中打印。但是如果我向测试文本文件添加新文本,它仍然会读取旧数据。可能是因为它在eclipse目录中,idk 服务器- 多客户 客户-Java 服务器从客户端读取数据不工作,java,sockets,client-server,Java,Sockets,Client Server,编辑——我已经包括了一些测试类。遗憾的是,对于这些测试文件,服务器没有将任何数据写入它从客户端接收到的文件。我也不知道为什么。这是我能给的最好的自动取款机了。再次编辑-另外,我注意到在测试时,客户端将读入测试文本文件并在终端中打印。但是如果我向测试文本文件添加新文本,它仍然会读取旧数据。可能是因为它在eclipse目录中,idk 服务器- 多客户 客户- 该程序的目标是让客户端将数据写入文件。客户端写入的文件有2个。然后,客户机将读入第一个文件的每一行并将其发送到服务器。然后服务器将每一行写入
该程序的目标是让客户端将数据写入文件。客户端写入的文件有2个。然后,客户机将读入第一个文件的每一行并将其发送到服务器。然后服务器将每一行写入自己的文件。对于第二个文件,将再次重复此操作 什么起作用了 客户端将所有数据写入其文件 在发送到服务器之前,客户端读取每一行的类型(一些小问题) 服务器正在写入数据(错误数据) 服务器正在将数据写入正确的文件 什么不起作用 服务器没有将正确的数据写入其文件-这就是问题所在。它一次又一次地重复同一行编辑:真正的问题是它不断从客户机获取的字符串一次又一次地相同 要比较的文件: 以下两个链接应该匹配(第一行除外),注意服务器有一个额外的行和重复的数据 客户的鼠标库 服务器的鼠标库 以下2个链接包含终端会话 服务器从客户端读入的每一行。这是写入服务器文件的内容,应该与客户端的终端会话相匹配 客户端在发送到服务器之前从其文件中读取的每一行。发送的同一变量将打印到其终端。对不起,这张照片。请注意2个空值。Idk为什么那些在那里。
这是来自客户端的实际sendData()
//This method will send data to the Client
public void sendData(Object[] data){
try {
oos.writeObject(data);
} catch (IOException e) {
e.printStackTrace();
}
}
客户端用于向服务器发送数据的代码。(getProgramPath()正在返回正确的路径)顺便说一句,代码的两部分都发送重复数据
private static void sendSavedData(){
try {
System.out.println("CLIENT SEND SAVED DATA: " + getProgramPath() + savedDataFileName + ext);
} catch (IOException e1) {
e1.printStackTrace();
}
//Now send selection data to the server
Object[] selectionData = new Object[2];
selectionData[0] = "Selections";
//selectionData[1] = allGraphs;
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(getProgramPath() + savedDataFileName + ext));
String line = br.readLine();
while (line != null) {
line = br.readLine();
selectionData[1] = line;
System.out.println("Client Line: " + line);
sendData(selectionData);
}
}
catch(Exception e){
e.printStackTrace();
}
finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//Now send selection mouse coor data to the server
selectionData = new Object[2];
selectionData[0] = "MouseCoor";
selectionData[1] = "Test here 1";
br = null;
try {
br = new BufferedReader(new FileReader(getProgramPath() + savedDataFileNameMouse + ext));
String line = br.readLine();
while (line != null) {
line = br.readLine();
selectionData[1] = line;
System.out.println("Client Line: " + selectionData[1]);
sendData(selectionData);
}
}
catch(Exception e){
e.printStackTrace();
}
finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
客户端用于将文件写入硬盘,然后发送的代码
public static void writeSavedFile(String line, int typeOfData){
if(typeOfData == 0){
FileWriter fstream;
try {
fstream = new FileWriter(getProgramPath() + savedDataFileName + ext, true); //Prepare to append (the "true") to the file
BufferedWriter out = new BufferedWriter(fstream); //More prep
out.write(line); //Write data
out.newLine(); //Write a new line
out.close(); //Close the file
} catch (Exception e) {
e.printStackTrace();
}
}
else if(typeOfData == 1){
FileWriter fstream;
try {
fstream = new FileWriter(getProgramPath() + savedDataFileNameMouse + ext , true); //Prepare to append (the "true") to the file
BufferedWriter out = new BufferedWriter(fstream); //More prep
out.write(line); //Write data
out.newLine(); //Write a new line
out.close(); //Close the file
} catch (Exception e) {
e.printStackTrace();
}
}
}
服务器用来读取数据的代码——同样,它正在写入正确的文件,只是错误的数据。因此,如果需要,它将正确到达选项if和鼠标C。此设置是指fromClient[0]包含标记(选择、鼠标指针等),fromClient[1]包含要写入的数据
while(true){
try{
if((fromClient = (Object[]) ois.readObject()) != null){
//Determine what data this is
String tag = (String)fromClient[0]; //Getting the tag
if(tag.equals("ID")){
Server.addClient(serverID, (String)fromClient[1]);
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
Date date = new Date();
savedDataFileName = "Server Data - Selections - " + (String)fromClient[1] + " - " + dateFormat.format(date); //The new outputfile name
writeSavedFile("Server Data - Selections - " + (String)fromClient[1] + " - " + dateFormat.format(date),0);
//Now write the mouse coor file
savedDataFileNameMouse = "Server Data - Mouse Coor - " + (String)fromClient[1]; //The default naming for the outputfile
//The file will be saved with the date and time
savedDataFileNameMouse = "Server Data - Mouse Coor - " + (String)fromClient[1] + " - " + dateFormat.format(date); //The new outputfile name
writeSavedFile(savedDataFileNameMouse, 1);
}
else if(tag.equals("Selections")){
System.out.println("Server Line: " + (String)fromClient[1]);
writeSavedFile((String)fromClient[1], 0);
}
else if(tag.equals("MouseCoor")){
System.out.println("Server Line: " + (String)fromClient[1]);
writeSavedFile((String)fromClient[1], 1);
}
else{
System.out.println("WARNING - UNKNOWN DATA RECEIVED");
}
}
}
服务器用于写入文件的代码
//Write saved data: 0 = Selections 1 = Mouse Coor
public static void writeSavedFile(String line, int typeOfData){
if(typeOfData == 0){
FileWriter fstream;
try {
fstream = new FileWriter(Server.getProgramPath() + savedDataFileName + ext, true); //Prepare to append (the "true") to the file
BufferedWriter out = new BufferedWriter(fstream); //More prep
out.write(line); //Write data
out.newLine(); //Write a new line
out.flush();
out.close(); //Close the file
fstream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
else if(typeOfData == 1){
FileWriter fstream;
try {
fstream = new FileWriter(Server.getProgramPath() + savedDataFileNameMouse + ext , true); //Prepare to append (the "true") to the file
BufferedWriter out = new BufferedWriter(fstream); //More prep
out.write(line); //Write data
out.newLine(); //Write a new line
out.close(); //Close the file
fstream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
我已经编译并测试了您添加到问题中的代码,这真是一件奇怪的事情。我正在复制您的错误,但写入套接字之前的对象已更改 然后,我添加了自己的字符串数组,在您通过一个后直接编写,效果很好。这导致我假设objectOutputStream执行某种缓存 我设法通过切换线路来修复它
oos.writeObject(data);
与
所以我检查了文档,找到了任何引用这个的东西,我找到了这个方法。这证实了我的怀疑,即它们是高效的,没有重新序列化/发送相同地址的对象。它还为您提供了更干净的修复。调用oos.writeObject(数据)
后,只需调用oos.reset()
在Client.java类中,您还应该修复文件读取循环,因为它们当前错过了第一行并读取了一行null
String line;
while ((line = br.readLine()) != null) {
selectionData[1] = line;
System.out.println("Client Line2: " + selectionData[1]);
sendData(selectionData);
}
在MultiServerClient.java中,您应该真正创建一个系统,在将每一行写入文件之前和之后都不会打开和关闭文件流,您应该能够在连接打开时打开它一次,然后在连接关闭后关闭它
谢谢你有趣的拼图 我仍在检查您的代码,但要让您走上正轨,需要做的一件事是在读取(或写入,如果您愿意的话)每行时执行
System.out.println
,以及执行println
的方法。这样你就可以看到什么时候有重复的行,在哪里。谢谢!服务器终端包含从客户端读取的内容,即写入文件的相同字符串。客户端终端包含从其文件中读取的字符串,该字符串与发送到服务器的字符串相同。假设您的Server
类中有处理实际i/o的其他方法。在那些fromClient
可能无法重新读取新数据的系统中是否存在问题?我已经阅读了您提供的代码至少3次,没有找到一个bump.Nope,这一行--System.out.println(“服务器行:”+(String)fromClient[1]);--是服务器终端会话中重复的内容,该会话也会打印到服务器文件中。因此,我认为客户端发送或服务器接收时有问题。我真的认为您应该在服务器端的读取操作接收数据的地方重新检查或添加一个println
。否则,恐怕我会被难倒。一旦你有足够的rep:\ for(inti=0;i<100000000;i++){System.out.println(“谢谢!!”)}哈哈!哎呀!还有,我为什么要让fstream保持打开状态?只是想让你知道。它打开一次写入,然后在20分钟内不会再获取数据。我是否应该在开始时保持打开状态,然后在结束时关闭它?啊,在示例代码中,它对正在读取的文件的每一行都这样做。除非您需要释放资源,否则如果没有其他内容访问该文件,则没有任何实际理由关闭该流。谢谢!很高兴知道:)
String line;
while ((line = br.readLine()) != null) {
selectionData[1] = line;
System.out.println("Client Line2: " + selectionData[1]);
sendData(selectionData);
}