Results 1 to 14 of 14
  1. #1
    DarkBlaze is offline Member
    Join Date
    Jul 2008
    Posts
    7
    Rep Power
    0

    Question passing info between server/client problem

    I am creating the board game diplomacy and I have run into a problem that I can't figure out. It might have to do with my limited knowledge of networking and threads but I will try to only post the relevant info.

    I have a Board class which creates all of the Territorys in the game. I have a Player class which creates of all the units for each player. The GameJudge class keeps track of all this information.

    When the game starts, the server creates the board and then assigns each client to be a different player. It then stores this information in a class called StartGameVariables and passes this to each client. When the client recieves this information it updates its respective variables in GameJudge.

    When its time to process the moves for each player's units, each client sends their player info to the server:

    Java Code:
    _output is an ObjectOutputStream
    player is the player for this client
    
    public void writeToHostGameJudge(Player player){
       try {
            _output.writeObject(player);
            _output.flush();
        }
        catch(SocketException e){
            System.out.println("Exited16");
        }
        catch (IOException e){
            System.out.println("Exited15");
        }
    }
    When the server recieves this info it updates each clients' player because it keeps track of what each client is doing with their units. Once it has received every clients' info, it then processes the moves and passes each client the now updated Board and the updated Players:

    Java Code:
    _numUpdatePlayer is an integer representing how many times this was called
    _numPlayers is an integer representing the number of clients
    
    public void updatePlayers(Player clientPlayer){
        for(int x=0; x<_gameJudge.getPlayers().length; x++){
            if(clientPlayer.Name().equals(_gameJudge.getPlayers()[x].Name())){
                _gameJudge.getPlayers()[x] = clientPlayer;
                _numUpdatePlayers++;
            }
        }
    
        if(_numUpdatePlayers == _numPlayers){
            _gameJudge.ProcessOrders();
            
            **Writes info back to clients here regarding Players and the Board**
    
        }
    }
    The problem comes when it is time to process the moves. The server knows all the moves for every unit because each client passed it thier unit's moves when it passed it's player to the server. The clients' units however don't interact because apparently they are refering to a different board.

    For example:
    player[1] is the server
    player[2] is a client

    player[1].getUnit(0).getCurrentTerritory() - it will display the name the of the territory unit 0 is in.

    So when I loop through every territory in the game and compare territory[x] to a unit's current territory, if they are equal it should output "WORKS":

    Java Code:
    for(int x=0; x<_board.getTerritory().length; x++){
        if(_board.getTerritory()[x].equals(player[1].getUnit(0).getCurrentTerritory()){
            System.out.println("WORKS");
        }
    }
    It works fine for player[1], but if i replace player[1] with player[2] is does not diplay anything even though player[2].getUnit(0).getCurrentTerritory() displays the name of the territory it is currently in. I don't understand how player[2]'s units can be refering to a different territory when I passed each client the same info that the server has.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    Not sure where server/client come in. I was thinking of HTTP communications but I don't see any of that here.

    I notice that you have used a lot of literal/constant values in indexing rather than variables. Eg [0] and [2] vs [firstOne]

    You'll have to show more code if anyone is to figure this out.

    To debug your code, add println() statements to it so you can trace flow and values.

  3. #3
    DarkBlaze is offline Member
    Join Date
    Jul 2008
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Not sure where server/client come in. I was thinking of HTTP communications but I don't see any of that here.

    I notice that you have used a lot of literal/constant values in indexing rather than variables. Eg [0] and [2] vs [firstOne]

    You'll have to show more code if anyone is to figure this out.

    To debug your code, add println() statements to it so you can trace flow and values.
    The server/client part is where the player that is the server passes/receives information to/from the clients that are players with sockets, output/input streams, a TCP/IP connection, and all that jazz. Unless I'm horribly mistaken that is a server/client relationship.

    I've traced the problem to what I have given. To make it simple, the problem is that the server creates and passes a Board value to all the clients. The clients enter orders for thier units and then pass that info back to the server. The server takes all the info and processes the orders, but the Board that the clients' units are refering when processing the orders seems to be a different Board than the one that the server has even though the server passed its Board to the clients and it remains unchanged till the orders are to be processed.

    I don't know what else I can show code wise since this is a really big program and the parts I have given are where it is giving me the problem, everything else works fine.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    The only thing I can suggest is:
    add println() statements
    Without seeming the code how can anyone know what your code for the equals() method does, for example.

    Good luck.

  5. #5
    DarkBlaze is offline Member
    Join Date
    Jul 2008
    Posts
    7
    Rep Power
    0

    Default

    Okay, to make things simple here's some questions that describe my problem:


    If I create an Object, "Board"(a class of variables and data), in the Server Class and then pass that "Board" to a Client, is it still the same Object in the Client?

    Also if an Object, "Unit", in the Client Class stores data from the "Board" that was passed to it from the Server Class, is that variable the same as the Server Class' "Board" data?

    If I need to clarify what I'm asking just let me know.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    Ok, you're writing and reading Objects using ObjectOutputStreams and ObjectInputStreams. I'll have to research it.
    an Object, "Unit", in the Client Class stores data
    Not sure what this means. Are you asking if a variable in the Client class is set to data(primitive or an object reference) from an object the client received from the server is the value the same?
    You could use println() on both sides to show the values.

    Here are a pair of programs to write and read Objects:
    Java Code:
    // WriteSerialized.java
    // Write a serialized object - can then read with ReadSerialized
    
    package Testing;
    
    import java.io.*;
    import java.awt.*; 
    import java.util.*;   
    
    public class WriteSerialized {
    
    	public static void main(String[] args) {
            Frame fr = new Frame();
            String fn = "WriteSerializedData.ser";   // This is the file we'll write to
    
            try {
                // Write a class out
                FileOutputStream f = new FileOutputStream(fn);
                ObjectOutput out = new ObjectOutputStream(f);
    
                WriteSerializedData wst = new WriteSerializedData();
                out.writeObject(wst);
                out.flush();
                out.close();
                System.out.println("Wrote " + fn + ", date=" + wst.dd);
    //     Wrote WriteSerializedData.ser, date=Tue Jul 22 20:55:07 CDT 2008
            }catch(Exception ex) {
                ex.printStackTrace();
            }
    
            System.exit(0);                 // Done
    	} // end main()
    } // end class
    
    // A class to write  ---------------------------------
    // Renaming this class's class file causes: java.lang.ClassNotFoundException
    // trying to read the .ser file
    class WriteSerializedData implements Serializable {
       java.util.Date dd =  new Date();
    }
    Java Code:
    // ReadSerialized.java
    // Read and display a serialized object
    // See WriteSerialized to write object
    
    package Testing;
    
    import java.io.*;
    import java.awt.*; 
    import java.util.*;   
    
    public class ReadSerialized {
    
    	public static void main(String[] args) {
            Frame fr = new Frame();
            String serFN = "";
    
            if (args.length == 0) {
                FileDialog fd = new FileDialog(fr, "Get serialized file",
                                        FileDialog.LOAD);
                fd.show();
                String fn = fd.getFile();
                if (fn == null || fn.equals("")) {
                    System.out.println("user cancelled");
                    System.exit(0);         //OK done    
                }
                serFN = fd.getDirectory() + fd.getFile();
            }else {
                // User passed a fn as arg
                serFN = args[0];
            }
            System.out.println("Trying to read: " + serFN);
    
            // Now try to read/Deserialize the file
            Object obj = null;            
            try {
                FileInputStream f2 = new FileInputStream(serFN);
                // Read into a byte buffer first
                byte[] buf = new byte[f2.available()];
                int nr = f2.read(buf); // read
                f2.close();
                // Now read the object from buf
                ByteArrayInputStream bais = new ByteArrayInputStream(buf);
                ObjectInputStream in = new ObjectInputStream(bais);
                obj = in.readObject();
                in.close();
            }catch(Exception ex) {
                System.err.println("Reading got " + ex);
                ex.printStackTrace();
                System.exit(0);             // DONE if error
            }
          // Get following error when class file not on classpath:
          //    Reading got java.lang.ClassNotFoundException: Testing.WriteSerializedData
    
            //--------------------------------------------------------
            // What have we found?
            System.out.println("Class is " + obj.getClass());
    
            if (obj instanceof Hashtable) {
                System.out.println((Hashtable)obj + "\n");
                // Show the keys with values
                Enumeration en = ((Hashtable)obj).keys();
                while (en.hasMoreElements()) {
                    Object obj2 = en.nextElement();
                    System.out.println(obj2 + " = " + ((Hashtable)obj).get(obj2));
                }
    
            }else if (obj instanceof Vector) {
                System.out.println((Vector)obj);
    
            }else if(obj instanceof WriteSerializedData) {
                System.out.println("dd=" + ((WriteSerializedData)obj).dd);    //  cast to type
                // output=dd=Tue Jul 22 20:45:50 CDT 2008
                //        dd=Tue Jul 22 20:55:07 CDT 2008
    
            }
            System.exit(0);                 // Done
    	} // end main()
    } // end class ReadSerialized
    Last edited by Norm; 07-23-2008 at 04:18 AM.

  7. #7
    DarkBlaze is offline Member
    Join Date
    Jul 2008
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Are you asking if a variable in the Client class is set to data(primitive or an object reference) from an object the client received from the server is the value the same?
    Yes, that is what I'm asking. The will be both primitive and object reference data being set.

    It doesn't seem like they are the same though because this is where the problem in my program comes from. I don't know why that would be the case though since I'm never changing its values.


    Quote Originally Posted by Norm View Post
    You could use println() on both sides to show the values.
    The problem with this is that I am running it from Eclipse and I can't get the Server values and the Client values to both show up in the console since I am running the program twice from the same computer, once as a server and the other as a client. When one writes values to the console it will overwrite values that the other writes. Last time I debugged server/client errors that I had, I had more than one computer running. If you know of a way around this I can try it.


    In regards to the code you posted, my program can already read and write Objects. Its just what happens after it reads and writes the Objects that gives me a problem. It seems like the Object that it passes has changed when I compare its data (like in the second question that I asked) even though I didn't modify anything about it.
    Last edited by DarkBlaze; 07-23-2008 at 05:53 AM.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    when I compare its data
    What method is used for this? I earlier asked what was in the equals() method.
    a way around this
    Run the server and the client from different windows.
    Another way is to trap output to System.out and write it to a file.
    I've a class that does that SaveStdOutput
    Its in a zip at: http://shellknob2007.googlepages.com...oolsSource.zip

  9. #9
    DarkBlaze is offline Member
    Join Date
    Jul 2008
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Run the server and the client from different windows.
    Didn't work because both consoles where linked and it still overwrote whatever was in it. I outputed the info using the classes that you gave me and it shows that they are indeed not the same.

    Quote Originally Posted by Norm View Post
    What method is used for this? I earlier asked what was in the equals() method.
    I have tried both the .equals() method and the == operator and they both agree that the data is not equal.

    I compared the data inside the Object "Unit" which stores some data from the Clients "Board" Object(passed to it from Server), with the "Board" Object of the Server (I pass "Unit" to the Server). The data that I compare is the data that the "Unit" Object has which should be the same as the "Board" Object of Server has since Server passed the same "Board" to the Client. I don't understand why they are different.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    Didn't work because both consoles where linked
    You must start them one at a time so that there are two of them.
    What OS are you on?

    So you don't have your own equals() method. I suggest that you write one. I suspect that because the two objects came from different sources, that they are not equal as per the Object.equals() method.

    When comparing two objects in your app, what do you consider equal? I suspect you want the contents of the two objects to be the same within some tolerance. If you want to check if two object references point to the same object then == would work.

    Here's how Object.equals() might work:
    AnObject a1 = new AnObject();
    AnObject a2 = a1;
    a2.equals(a1); // true

    The code for Object.equals() from the src.zip file in the JDK:
    public boolean equals(Object obj) {
    return (this == obj);
    }
    Last edited by Norm; 07-23-2008 at 06:54 PM.

  11. #11
    DarkBlaze is offline Member
    Join Date
    Jul 2008
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    So you don't have your own equals() method. I suggest that you write one. I suspect that because the two objects came from different sources, that they are not equal as per the Object.equals() method. When comparing two objects in your app, what do you consider equal?
    I created my own equals() method and instead of checking if the Objects are equal, it checks to see if the data inside the Objects are equal. This fixes the problem that I've been having and allows my program to work. YAY!!

    I don't get why this doesn't work though.
    ex):
    Object A = created within the Server Class.
    it has:
    A.data1 - variable which stores data
    A.data2 - variable which stores data

    Pass Object A to Client:
    Object B = Object A(that is passed from Server Class) in Client Class
    Object C = B.data1 in Client Class (C class is the same class as data1)

    Pass Object C to Server

    Both is Server Class:
    C.equals(A.data1)
    C == A.data1

    Both return false. I still don't understand why they aren't equal.

    Thanks for your help so far Norm, I appreciate it. I know that I can be a pain in the butt to deal with.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    Isn't that the same problem?
    Who's equals() are you using?
    There can be two objects that contain exactly the same data, but are not equal() if you are using the Object's equal method. That equal() means that the contents of the two object references(pointers) point to the SAME object, NOT to two objects that have the same content.

  13. #13
    DarkBlaze is offline Member
    Join Date
    Jul 2008
    Posts
    7
    Rep Power
    0

    Default

    Yes it is the same problem and I was trying to put it into some type of code. The equals() method is the Object equals() method.

    Okay I think I understand now. When you pass information to a Client it is not refering to the same Object anymore and that is what the .equals() method is checking. Hence why when I compare the two Objects in the example, even though they have the same data, they are not refering to the same Object. Correct?

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

Similar Threads

  1. how client know what kind of server
    By lemur in forum Networking
    Replies: 3
    Last Post: 05-31-2008, 07:11 AM
  2. client/server palindrom
    By Rgfirefly24 in forum New To Java
    Replies: 0
    Last Post: 04-28-2008, 02:19 AM
  3. Identify Client in Socket Client Server Application
    By masadjie in forum Networking
    Replies: 1
    Last Post: 12-20-2007, 09:18 AM
  4. client-server communication problem
    By revathi17 in forum New To Java
    Replies: 1
    Last Post: 08-09-2007, 02:21 PM
  5. Problems with client and server
    By Albert in forum Advanced Java
    Replies: 2
    Last Post: 07-02-2007, 06:07 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •