• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

While loop for Server

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,

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 (true)
{
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?

Much appreciated.



 
Ranch Hand
Posts: 159
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
John Reacher
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
John Reacher
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 26770
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No. The variables in the server's JVM are completely different from the variables in the client's JVM and there is no connection between them.
 
John Reacher
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh ok. Do you have any suggestions as to how I could get around that without completely redesigning the program?
 
Paul Clapham
Sheriff
Posts: 26770
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you mean by "get around that"? I'm trying to imagine what you think is happening, or failing to happen, but I don't understand at all.
 
John Reacher
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

Much appreciated again.
 
John Reacher
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry I should clarify. When user 1 disconnects user 2 gets a notification message about the disconnect and vice versa.
 
Paul Clapham
Sheriff
Posts: 26770
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic