|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

07-22-2008, 11:15 PM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 7
|
|
|
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:
_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:
_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":
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.
|
|

07-23-2008, 12:07 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
|
|
|
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.
|
|

07-23-2008, 12:29 AM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 7
|
|
Originally Posted by Norm
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.
|
|

07-23-2008, 02:02 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
|
|
The only thing I can suggest is:
Without seeming the code how can anyone know what your code for the equals() method does, for example.
Good luck.
|
|

07-23-2008, 04:40 AM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 7
|
|
|
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.
|
|

07-23-2008, 05:31 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
|
|
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:
// 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();
}
// 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 06:18 AM.
|
|

07-23-2008, 06:21 AM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 7
|
|
Originally Posted by Norm
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.
Originally Posted by Norm
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 07:53 AM.
|
|

07-23-2008, 05:33 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
|
|
What method is used for this? I earlier asked what was in the equals() method.
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
|
|

07-23-2008, 08:31 PM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 7
|
|
Originally Posted by Norm
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.
Originally Posted by Norm
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.
|
|

07-23-2008, 08:44 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
|
|
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 08:54 PM.
|
|

07-23-2008, 09:54 PM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 7
|
|
Originally Posted by Norm
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.
|
|

07-24-2008, 12:45 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
|
|
|
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.
|
|

07-24-2008, 01:58 AM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 7
|
|
|
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?
|
|

07-24-2008, 05:14 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
|
|
|
Yes, that seems right.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|