Results 1 to 10 of 10
Thread: Networked Card Game.
- 07-20-2011, 08:02 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 6
- Rep Power
- 0
Networked Card Game.
I've just finished writing all the game logic for an electronic version of a game a friend of mine and I used to play. The game logic is 100% complete and it does exactly what I want. The problem I have is that I have no idea where to start with implementing the networking aspect of the program. I've done a bunch of reading about sockets and client-server setups, but I can't wrap my head around how to make that do what I want. The specific problem I have is how to synchronize the GUI. I have a bunch of JToggleButtons that I used to build the basic playing field. There are 15 flippable cards, what I need to do is make sure that when 1 player flips a card on his screen, that same card flips on the other players screen. That's the main issue. I'm hoping figuring that out will help me in how to send and implement the other pieces of data I have (that include the player's hand, a simple chat, and cards revealed from the other player's hand), but one step at a time. If anyone could provide any insight or point me to useful information I'd greatly appreciate it.
- 07-20-2011, 08:27 PM #2
I'd start with a simple client / server set of programs and see how they work. There are many examples here on the forum if you Search for say ServerSocket.
Then design a protocol for how the players and the server will communicate.
- 07-21-2011, 07:51 PM #3
Member
- Join Date
- Jul 2011
- Posts
- 6
- Rep Power
- 0
Thanks for the advice, Norm. I've managed to work out most of the basics. I just want to get confirmation on one thing. I think I will need to start anew thread to handle listening on the socket while the rest of the program goes about it's business, as the data I need to send is pushed from the source PC to the destination PC instead of requested, correct? Also can I send more than 1 type of data on a single open connection? This program sends a String and several custom objects with absolutely no pattern as to how and when. All the examples I've seen have been a client making a request and then getting a response of a single data type.
- 07-21-2011, 08:15 PM #4
Yes, you'll want the communication stuff on its own thread.
You can mix data types on an ad hoc basis by using a protocol of sending a short message saying what the next type is. Something like: NxtMsg=String String NxtMsg=int int ...
Where NxtMsg=String is a representation and not the actual data sent. You could use something like an enum to define a bunch of single bytes that would tell the receiver how to read the following data. You'd need to use a class like ObjectOutputStream and ObjectInputStream that have different methods for reading different data types.Last edited by Norm; 07-21-2011 at 08:18 PM. Reason: Correct class name
- 07-23-2011, 05:59 PM #5
Member
- Join Date
- Jul 2011
- Posts
- 6
- Rep Power
- 0
Thanks again Norm.
Okay, now I'm looking at a situation like this:
Server starts, initializes the game and listens for a client connection(works)
Client starts, connects to the server (works)
Server sends initial data to the client(fails)
As best I can figure I'm doing something wrong with ObjectOutputStream.
ObjectOutputStream out = null;
...
out = new ObjectOutputStream(clntSkt.getOutputStream());
out.writeObject(<A Deck Object>);
out.writeObject(<A Hand Object>);
out.writeObject(<A Hand Object>);
Nothing gives me any issues until the out.writeObject call. Any idea where I'm going wrong.
On the client side it does the following:
try {
clntSkt = new Socket("mypc", 3797);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
in = new ObjectInputStream(clntSkt.getInputStream());
} catch (IOException e) {
System.out.println("Your input stream is pooched.");
}
//Draw game based on parameters passed from server
try {
d=(Deck)in.readObject();
pyr=(Hand)in.readObject();
p=(Hand)in.readObject();
GUI.startGUI(d, pyr, p);
} catch (IOException e) {
} catch (ClassNotFoundException e) {
- 07-23-2011, 06:03 PM #6
Try flushing the output after writing
What does this mean? Errors with messages? Please post any error messages.Nothing gives me any issues until
Always have a printStackTrace in all of your catch blocks. You want to know exactly where and why there was an exception.
- 07-23-2011, 06:14 PM #7
Member
- Join Date
- Jul 2011
- Posts
- 6
- Rep Power
- 0
Good call on the printStackTrace. Turns out one of my objects wasn't serializable, but I didn't notice it because it wasn't being used directly.
- 07-25-2011, 09:28 PM #8
Member
- Join Date
- Jul 2011
- Posts
- 6
- Rep Power
- 0
I am on the cusp of completing this project. I'd like to thank you for all your assistance. I have just one last issue, that's simply an annoyance rather than a flaw in the program. I set up a simple chat embedded in the program. It works great except for one problem; The chat history doesn't auto scroll when it receives a message from the other computer. It auto scrolls just fine when it updates the messages from the local machine (assuming it's already at the very bottom of the scroll window). Anyone know how to fix this?
- 07-26-2011, 12:14 AM #9
That's strange. You are saying that the source of the data to be displayed determines if the history scrolls.chat history doesn't auto scroll when it receives a message from the other computer. It auto scrolls just fine when it updates the messages from the local machine
No ideas.
- 07-26-2011, 12:22 AM #10
Member
- Join Date
- Jul 2011
- Posts
- 6
- Rep Power
- 0
Similar Threads
-
Creating a Card Game
By Kidd91 in forum New To JavaReplies: 1Last Post: 12-24-2010, 02:25 PM -
Card Game
By abby0910 in forum New To JavaReplies: 1Last Post: 07-24-2010, 12:38 AM -
please help me with this card game
By noobinoo in forum New To JavaReplies: 13Last Post: 03-28-2010, 02:07 PM -
card game Rummy
By javafox in forum New To JavaReplies: 4Last Post: 03-14-2009, 03:53 PM -
A Online Card Game
By GonzaloP in forum NetworkingReplies: 0Last Post: 12-28-2008, 06:37 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks