简体   繁体   中英

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. 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.

Server- http://pastebin.com/F7xzMdes

SeverMultiCLient - http://pastebin.com/HQM7PyGj

Client- 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. 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

Client's Mouse Coor http://pastebin.com/RnEGgBJm

Server's Mouse Coor http://pastebin.com/cBqfLHnf

The following 2 links contain the terminal sessions

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

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. Idk why those are there. http://i.imgur.com/HTPVzHU.png


This is the actual sendData() from Client

//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

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. This setup is the fromClient[0] contains a tag (Selections, MouseCoor, etc) and fromClient[1] contains the data to write.

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.

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() . 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) .

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.

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.

Thanks for the interesting puzzle!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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