2

编辑——我已经包括了一些测试类。遗憾的是,对于这些测试文件,服务器没有将任何数据写入它从客户端接收的文件中。我也不知道为什么。这是我能给atm的最好的。再次编辑-另外,我注意到在测试时,客户端将读取测试文本文件并在终端中打印。但是如果我在测试文本文件中添加新文本,它仍然会读入旧数据。也许是因为它在 Eclipse 目录中,idk。

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

SeverMultiCLient - http://pastebin.com/HQM7PyGj

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


该程序的目标是让客户端将数据写入文件。客户端写入了 2 个文件。然后客户端将读取第一个文件的每一行并将其发送到服务器。然后服务器会将每一行写入自己的文件。这对第二个文件再次重复。

什么在起作用?:

客户端将所有数据写入其文件

客户端在发送到服务器之前读取每一行类型(一些小问题)

服务器正在写入数据(错误数据)

服务器正在将数据写入正确的文件

什么不工作?:

服务器没有将正确的数据写入其文件 - 这就是问题所在。它一遍又一遍地重复同一行编辑:真正的问题是它不断从客户端获取的字符串一遍又一遍。

要比较的文件:

以下 2 个链接应该匹配(除了第一行) 注意服务器有一个额外的行和重复的数据

客户的鼠标坐标http://pastebin.com/RnEGgBJm

服务器的鼠标坐标http://pastebin.com/cBqfLHnf

以下 2 个链接包含终端会话

服务器从客户端读取的每一行。这是写入服务器文件的内容,这应该与客户端的终端会话http://pastebin.com/A4xqWGiu匹配

客户端在发送到服务器之前从其文件中读取的每一行。发送的相同变量将打印到其终端。对不起图片。注意 2 个空值。不知道为什么那些在那里。 http://i.imgur.com/HTPVzHU.png


这是来自客户端的实际 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();
        }
    }

}

服务器使用读取数据的代码 - 它再次写入正确的文件,只是错误的数据。因此,如果需要,它将正确到达 Selections if 和 MouseCoor。此设置是 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");
                    }
                }
            }

代码服务器用于写入文件

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

}
4

1 回答 1

1

我已经编译并测试了您添加到问题中的代码,这确实是一件奇怪的事情。我正在重现您的错误,但写入套接字之前的对象已更改。

然后我添加了我自己的字符串数组,在你通过后直接写入,效果很好。这让我假设 objectOutputStream 做了某种缓存。

我设法通过换线来解决它;

oos.writeObject(data);

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

因此,我检查了文档以找到任何引用此内容的内容,并找到了方法reset()。这证实了我的怀疑,即它们效率很高,并且不会重新序列化/发送相同地址的对象。它还为您提供更清洁的修复。oos.reset()打完电话就打oos.writeObject(data)

在您的 Client.java 类中,您还应该修复文件读取循环,因为它们当前错过了第一行并读取了一行 null。

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

在您的 MultiServerClient.java 中,您应该真正创建一个系统,在该系统中,在将每一行写入文件之前和之后不会打开和关闭文件流,您应该能够在打开连接时打开它一次,然后在之后关闭它连接关闭。

感谢有趣的谜题!

于 2013-08-07T18:54:03.920 回答