Not sure if anyone can guide me on this but it's worth a shot. I am making a server using sockets that spawns a game when it has two players. Most of it is working, but I'm trying to account for the scenario where a player in an existing game disconnects. If this happens, the program should wait until one other person connects and insert them into that game. Or, if there's someone waiting in queue, it should grab and insert them into the game.
The way I have the server setup is like this:
While (b == false)
socket s = serversocket.accept();
Add this socket to an arrayList.
If the arrayList size is 2, spawn a game.
Reset arrayList size to 0.
While(b == true);
Now I'll explain. B is a static boolean. I made it static because the game class needs access to it. In the server I initialize it to false, so within the while true infinite loop this sub loop will always run and create games while b is false.
However, in the game class, whenever a disconnect happens, I have a loop that waits until the server arrayList size is more than 0 (there is one socket waiting). Once there is, I call serverclass.b = true; to stop the server loop until I am able to grab that socket from a get method at the server. Once I have it, I call serverclass.b = false.
However, it's simply not working and is just adding the socket to the server arrayList as if I haven't stopped it.
Is there an easier way to achieve something like this?
Runs your game class in a separate thread?
If so: if your variable is shared amongst more threads, then reading and writing to this variable by more threads gives no guarantee that both threads see the same value. If you want such changes to be visisble for all threads, you should use synchronization.
Try to make your boolean b volatile. Volatile variables ensure that changes to them are propagated to other threads.
Yeah the game is spawned and ran as a thread, which calls serverclass.b = true in an attempt to lock the server until it can get the next available socket. The server itself isn't threaded, it just runs via the while(true) loop until closed.
Thanks that did help a lot, making it volatile. Follow up question though. If I am running a .jar version of just the server on a machine, and a .jar version of just the game on other machines, will setting the volatile value from the game thread (on a client machine) register it as changed in the server, running on a different machine?
Sorry. Guess I was a bit unclear there. I was able to fix that problem but now I have another one. When a user in a game disconnects, I'm sending a message to that output stream telling them about it, and notifying them when a new user is found.
The problem is, if both users close their game Window, it has already sent this notification to the output stream. So, when there are now 0 active users and someone connects as user 1 for a new game, they are receiving the notification about a user disconnect from the other game. Is there any way I can not send the message to the output stream unless only one of the game users disconnects?
John Reacher wrote:The problem is, if both users close their game Window, it has already sent this notification to the output stream. So, when there are now 0 active users and someone connects as user 1 for a new game, they are receiving the notification about a user disconnect from the other game. Is there any way I can not send the message to the output stream unless only one of the game users disconnects?
Well, remember that we've only seen a top-level summary of your programs. However I would say that if you think you're sending a message to Connection A but it's actually received by Connection B then there's a bug in your code. Or your design.