Captive Imagination
September 10, 2010, 04:38:21 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Check out the teaser site for the first official game by Captive Imagination: http://www.galaxiesbeyond.com
 
   Home   Help Search Calendar Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: Need to close channels for a rebind?  (Read 742 times)
0 Members and 1 Guest are viewing this topic.
elias4444
Full Member
***
Offline Offline

Posts: 110


View Profile
« on: February 27, 2006, 12:22:57 PM »

Just one thing after another with me... but as long as it's helping:  Tongue

I noticed that even if you call shutdown for the server and clients, it doesn't close any of the channels or ports. So, if you don't completely shut down the JVM before trying to restart a server instance, you get something like this:

Code:
Unable to bind to UDP port: 27995, trying: 27996.
java.net.BindException: Address already in use: bind
at sun.nio.ch.Net.bind(Native Method)
at sun.nio.ch.DatagramChannelImpl.bind(Unknown Source)
at sun.nio.ch.DatagramSocketAdaptor.bind(Unknown Source)
at com.captiveimagination.jgn.UDPMessageServer.init(UDPMessageServer.java:49)
at com.captiveimagination.jgn.UDPMessageServer.<init>(UDPMessageServer.java:33)
at jgnnetworkers.DGServer.<init>(DGServer.java:60)
at mainmenu.MainMenu.startServerSystem(MainMenu.java:827)
at mainmenu.MainMenu.begin(MainMenu.java:609)
at mpgame.MPGame.exittomainmenu(MPGame.java:2726)
at mpgame.MPGame.mousecheck(MPGame.java:2588)
at mpgame.MPGame.gameloop(MPGame.java:567)
at mpgame.MPGame.begin(MPGame.java:245)
at mainmenu.MainMenu.begin(MainMenu.java:619)
at mainmenu.MainMenu.splashscreen(MainMenu.java:442)
at mainmenu.MainMenu.main(MainMenu.java:1645)
Unable to bind to UDP port: 27996, trying: 27997.

I remember having this same problem with my code (as the TCP standard coupled with some funny Java ideas keeps the channel open). I just had to check if the channels were already initialized on server startup, and if they were, tell it to use the ones that were already there.
Logged
darkfrog
Administrator
Inspired Imagination
*****
Offline Offline

Posts: 2650


View Profile
« Reply #1 on: February 27, 2006, 01:48:37 PM »

Well, the problem there is that you also have to shut down the UDPMessageServer and TCPMessageServer associated with each client and server.  I don't have the shutdown of the client and server kill the servers also because you might have something else that's using the same servers that you want to keep running.  If it would be beneficial I could have shutdown receive a boolean to say if it should also shutdown the servers?

darkfrog
Logged
elias4444
Full Member
***
Offline Offline

Posts: 110


View Profile
« Reply #2 on: February 27, 2006, 03:09:51 PM »

Actually, I haven't tried this yet, but I figured maybe I could just pause the server, clear it out, and keep using the same channels in order to fix this issue on my end. A little non-intuitive, but it might work.

Logged
darkfrog
Administrator
Inspired Imagination
*****
Offline Offline

Posts: 2650


View Profile
« Reply #3 on: February 27, 2006, 03:13:14 PM »

Let me know what you come up with.  If you need some code changes to get it working more intuitively just let me know.

darkfrog
Logged
elias4444
Full Member
***
Offline Offline

Posts: 110


View Profile
« Reply #4 on: February 27, 2006, 05:26:06 PM »

Well, this was a tricky one, but I got it working (I think). Basically, you can't shutdown the server or the clients, you just have to leave them running, and then come up with a method to reinitialize your gamestate before going back to them. With the server, I put in a boolean variable that when set will pause the thread and then reinitialize all the counters and things once it's flipped back. You can't stop the thread though! Because if you do, you can't restart it, and you need to use the same instance of the server AND client. With the client, you can just call the "connect" method again to connect again.
Logged
darkfrog
Administrator
Inspired Imagination
*****
Offline Offline

Posts: 2650


View Profile
« Reply #5 on: February 27, 2006, 09:44:38 PM »

Right, that's one of the rules of Java. Once a thread's run has exited it can never be restarted.

Was this entirely a code change on your side or did you have to make some changes to JGN?  If so, could you post those changes for me to take a look at?

darkfrog
Logged
elias4444
Full Member
***
Offline Offline

Posts: 110


View Profile
« Reply #6 on: February 28, 2006, 09:42:46 AM »

I was able to tweak it on my side (although I'm sure I've made some changes to your code, I don't think they were needed for the final solution). Java just simply doesn't close the channels until the JVM is completely exited.
Logged
darkfrog
Administrator
Inspired Imagination
*****
Offline Offline

Posts: 2650


View Profile
« Reply #7 on: February 28, 2006, 10:16:41 AM »

Hmmm....that's probably an error on my part.  I'll try to do some testing with that when I get chance.  I'm sure there's a way to release the channel, I'm still pretty new to NIO though.

darkfrog
Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Page created in 0.188 seconds with 22 queries.