简体   繁体   中英

XStream not sent via sockets

I used already working code for save/load game for sending a player state via sockets. And I encountered a problem that game save is correct, but server is not receiving client's player state.

Here is the base code that is tested and working:

    int retval = fc.showSaveDialog(givenComponent);
    if (retval == JFileChooser.APPROVE_OPTION) {
        File file = fc.getSelectedFile();
        try {
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
            XStream xs = new XStream();
            GameSave gs = new GameSave();
            ArrayList<PlayerSerialize> listps = new ArrayList<PlayerSerialize>();
            for (Player tempplayer : Players.players) {
                PlayerSerialize ps = new PlayerSerialize();
                ps.getPlayerData(tempplayer);
                listps.add(ps);
            }
            gs.playersSerialize = listps;
            gs.gamedate = Dateutils.gamedate;
            String s = xs.toXML(gs);
            bw.write(s);
            bw.close();
        } catch (IOException ex) {
            Logger.getLogger(DialogMainField.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

Here is the client side code that is not sending anything to server:

    XStream xs = new XStream();
    GameSave gs = new GameSave();
    ArrayList<PlayerSerialize> listps = new ArrayList<PlayerSerialize>();

    PlayerSerialize ps = new PlayerSerialize();
    ps.getPlayerData(Players.players.get(1));
    listps.add(ps);
    gs.playersSerialize = listps;
    gs.gamedate = Dateutils.gamedate;
    String s = xs.toXML(gs);
    out.println("clientplayertoserver");
    out.println(s);

Here is the server side just in case:

            if (strIn.contains("clientplayertoserver")) {
                strIn = in.readLine();
                XStream xs = new XStream();
                GameSave gs = (GameSave) xs.fromXML(strIn);
                Players.players.get(1).getPlayerSerializeData(gs.playersSerialize.get(0));
            }

I need some kind of clue because I'm stuck investigating the problem. Are there any XStream limitations? Or the error is in the working with sockets? The same code is working in one place and is not working in another - I greatly thank in advance for any help with this weird situation.

Well, you are doing two different things here:

1) Saving the data to a file, which is ok.

2) Sending data via a socket. You seem to assume that all your data (the XStream serialized object) is actually in one line. This will usually not be the case. Even if you configure XStream to serialize all data without identation, you still cannot be sure you won't have linebreaks in the serialized data (your variables).

So solve your issue, you should separate your concerns here. 1st serialize / deserialize your objects to String and back (that seems to be working for you. 2nd send this data to a medium, like a file (which you already have) or to a server.

For sending string data to a server, you'll need some kind of protocol. Either you can reuse an existing protocol, like HTTP (POST request to a server), Web Service, Rest Call or whatever else your server is running.

If you want to implement your own protocol (as you have tried above), you must ensure that the server knows what to expect and how to treat it properly. Usually you should split your request in a header and a payload section or something like that. Include in your header what you want to do (eg save player state) and the meta information of that (eg how many bytes payload you are sending). After the header, send the payload. The server must now read the header 1st (like everything until the first newline), parse the header to understand what is going on (eg save player state, 543 bytes data) and act on it (read the data, transform it to a string, deserialize the XStream object and store it in a local database or whatever the server should do with that).

So and after all this information, please adapt your question. As you have seen you do not really have a question about XStream, but about how to send some data from client to a custom server.

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