简体   繁体   English

服务器从客户端读取数据无效

[英]Server reading in data from client not working

EDIT--I have included some test classes. 编辑-我已经包括了一些测试课程。 Sadly with these test files, the server isn't writing any data to the files it receives from the client. 遗憾的是,对于这些测试文件,服务器没有将任何数据写入从客户端接收到的文件中。 Idk why either. 伊德克为什么要这样。 It's the best I can give atm. 这是我给atm最好的。 edit again- Also, I noticed that while testing this, the test text files will be read in by the Client and printed in the terminal. 再次编辑-另外,我注意到在进行测试时,客户端将读取测试文本文件并将其打印在终端中。 But if I add new text to the test text files, it still reads in the old data. 但是,如果我将新文本添加到测试文本文件中,它仍会读取旧数据。 Maybe it's cause it's in an eclipse dir, idk. 也许是因为它在eclipse目录中,idk。

Server- http://pastebin.com/F7xzMdes 服务器-http: //pastebin.com/F7xzMdes

SeverMultiCLient - http://pastebin.com/HQM7PyGj SeverMultiCLient- http: //pastebin.com/HQM7PyGj

Client- http://pastebin.com/hBSLZsus 客户端-http: //pastebin.com/hBSLZsus


The goal of this program is for the Client to write data to a file. 该程序的目标是使客户端将数据写入文件。 There are 2 files that the clients writes. 客户端写入2个文件。 The client will then read in each line of the first file and send it to the server. 然后,客户端将读取第一个文件的每一行,并将其发送到服务器。 The server will then write each line to its own file. 然后,服务器将每一行写入其自己的文件。 This is repeated again for the second file. 对第二个文件再次重复此操作。

What's working? 怎么了 :

The client writing all data to its files 客户端将所有数据写入其文件

The client reading in each line KIND OF(some small issues) before sending to server 客户端在发送到服务器之前在每一行中阅读(有些小问题)

The server is writing data (wrong data) 服务器正在写入数据(错误数据)

The server IS writing data to the correct file 服务器正在将数据写入正确的文件

What's not working?: 什么不起作用?:

The server is not writing the correct data to its files - This is the problem. 服务器未将正确的数据写入其文件-这是问题。 It's repeating the same line over and over again EDIT: The real problem is that the String it keeps getting from the Client is the same over and over again. 重复一遍又一遍地重复同一行编辑:真正的问题是,它不断从客户端获取的字符串是一遍又一遍的。

Files to Compare: 要比较的文件:

The following 2 Links should match (EXCEPT the first line) Notice Sever has one extra line and repeated data 以下2个链接应该匹配(第一行除外)注意服务器中多了一条额外的行并重复了数据

Client's Mouse Coor http://pastebin.com/RnEGgBJm 客户的鼠标器http://pastebin.com/RnEGgBJm

Server's Mouse Coor http://pastebin.com/cBqfLHnf 服务器的鼠标器http://pastebin.com/cBqfLHnf

The following 2 links contain the terminal sessions 以下2个链接包含终端会话

Each line the server READS in from the client. 服务器的每一行都从客户端读取。 This is what's written to the Server's file, this should match the client's terminal session http://pastebin.com/A4xqWGiu 这就是写入服务器文件的内容,应该与客户端的终端会话http://pastebin.com/A4xqWGiu匹配

Each line the client reads in from it's file right BEFORE sending to the server. 客户端在发送到服务器之前,从文件中读取每一行。 The same variable that is sent, is printed to its terminal. 发送的相同变量将打印到其终端。 Sorry for image. 图片很抱歉。 NOTICE the 2 nulls. 注意2个空值。 Idk why those are there. 知道为什么这些东西在那里。 http://i.imgur.com/HTPVzHU.png http://i.imgur.com/HTPVzHU.png


This is the actual sendData() from Client 这是来自客户端的实际sendData()

//This method will send data to the Client
    public void sendData(Object[] data){
        try {
            oos.writeObject(data);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

Code the client uses to SEND data to server. 客户端用于将数据发送到服务器的代码。 (The getProgramPath() IS returning the correct path btw) Btw, both sections of the code sends repeat data (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();
            }
        }
    }

Code Client uses to write files to hdd to then send 代码客户端用于将文件写入硬盘,然后发送

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();
        }
    }

}

Code the server uses TO READ data - Again it's writing to the correct file, just wrong data. 服务器使用代码读取数据-再次写入正确的文件,只是错误的数据。 So it will properly reach the Selections if and the MouseCoor if when needed. 因此,如果需要,它将正确到达“选择”和“ MouseCoor”。 This setup is the fromClient[0] contains a tag (Selections, MouseCoor, etc) and fromClient[1] contains the data to write. 此设置是fromClient [0]包含标签(选择,MouseCoor等),而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");
                    }
                }
            }

Code server uses to write file 代码服务器用于写入文件

//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();
        }
    }

}

I've compiled and tested the code you added to your question, it really was a weird thing. 我已经编译并测试了您添加到问题中的代码,这确实是一件奇怪的事情。 I was reproducing your error, yet the object prior to writing to the socket was changed. 我正在重现您的错误,但是写入套接字之前的对象已更改。

I then added my own string array to be written directly after your passed one and that worked fine. 然后,我添加了我自己的字符串数组,以在通过一个字符串数组后立即将其写入,并且效果很好。 This lead me to assume that the objectOutputStream does some kind of caching. 这使我假设objectOutputStream进行某种缓存。

I managed to fix it by switching the line; 我设法通过切换线路来修复它;

oos.writeObject(data);

With

oos.writeObject(new String[]{(String)data[0],(String)data[1]});

So I checked the docs to find anything that referenced this and I found the method reset() . 因此,我检查了文档以查找引用此内容的所有内容,然后找到了reset()方法。 This confirmed my suspiciouns that they were being efficient and not re-serialising/sending objects of an identical address. 这证实了我的怀疑,他们是有效的,没有重新序列化/发送具有相同地址的对象。 It also provides you with a cleaner fix. 它还为您提供了更干净的修复程序。 Just call oos.reset() after you call oos.writeObject(data) . 在调用oos.reset()之后,只需调用oos.reset() oos.writeObject(data)

In your Client.java class you should also fix your file reading loops as they currently miss the first line and read a line of null. 在Client.java类中,您还应该修复文件读取循环,因为它们当前错过了第一行并读取了空行。

String line;
while ((line = br.readLine()) != null) {
    selectionData[1] = line;
    System.out.println("Client Line2: " + selectionData[1]);
    sendData(selectionData);
}

In your MultiServerClient.java you should really create a system whereby the file stream isn't opened and closed prior to and after writing each line to the file, you should be able to open it once when the connection is opened and then close it after the connection is closed. 在MultiServerClient.java中,您应该真正创建一个系统,在该系统中,在将每一行写入文件之前和之后均不会打开和关闭文件流,应该能够在打开连接时将其打开一次,然后在连接之后将其关闭连接已关闭。

Thanks for the interesting puzzle! 感谢您的有趣难题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM