Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Reset ServerSocket  RSS feed

 
Drew Lane
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to learn how to do Socket programming, so I wrote a little game of network tic tac toe.
It's going pretty good, but I'm not sure what do do when the game is over.
Right now, I only allow two players to connect to the ServerSocket at a time. When the game is finished, I want to allow another game to start immediately. I figured I could just create another instance of the server when the game was over, but I keep getting this error:
java.net.BindException: Address in use: JVM_Bind
I assume this means that the original instance of the server is still using that address. So, how do I release the server? I'm using server.close() but is there something else I need to do?
There is probably a better way to architect this program, but it still seems like I should be able to close the ServerSocket and start a new one, no?
Hope this is enough info...
Thanks,
Drew
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

When the game is finished, I want to allow another game to start immediately.
...
There is probably a better way to architect this program, but it still seems like I should be able to close the ServerSocket and start a new one, no?

You really don't need to close and rebind the ServerSocket. The normal way to process ServerSocket requests is to spawn a new thread to deal with each client. Since you are only allowing one game at a time you don't necessarily have to run the game in a separte thread, but it usually helps to decouple the plumbing from the work. You should redesign it so that there is no need to recreate the ServerSocket and if you run the game in a separate thread then it becomes easy to allow more games to take place concurrently.
 
Drew Lane
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Michael Morris:

You should redesign it so that there is no need to recreate the ServerSocket and if you run the game in a separate thread then it becomes easy to allow more games to take place concurrently.

I'm sure that you are right.
However, what is the reason I can't rebind the ServerSocket? It's really got me puzzled.
I do have some threads running. If one of these threads was still running somehow, would that cause the error I've been getting?
Regards,
Drew
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!