繁体   English   中英

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

[英]Server reading in data from client not working

编辑-我已经包括了一些测试课程。 遗憾的是,对于这些测试文件,服务器没有将任何数据写入从客户端接收到的文件中。 伊德克为什么要这样。 这是我给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();
        }
    }

}

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

}

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

然后,我添加了我自己的字符串数组,以在通过一个字符串数组后立即将其写入,并且效果很好。 这使我假设objectOutputStream进行某种缓存。

我设法通过切换线路来修复它;

oos.writeObject(data);

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

因此,我检查了文档以查找引用此内容的所有内容,然后找到了reset()方法。 这证实了我的怀疑,他们是有效的,没有重新序列化/发送具有相同地址的对象。 它还为您提供了更干净的修复程序。 在调用oos.reset()之后,只需调用oos.reset() oos.writeObject(data)

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

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

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

感谢您的有趣难题!

暂无
暂无

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

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