# FreeBSD java socket close problem



## kingspeech (Apr 22, 2011)

Hello everybody,

I have a problem about xSocket close() method in FreeBSD. Despite of the fact that I search it in Google I could not find any satisfying solution. Let me explain the problem a little bit detailed.

I have a code that open ports and listen connection. I use the xSocket for this. Especially this is for multiplayer game. When user wants to set up a game I assign the ports for the user and user can create a game and wait for another players. When player closes his web browser or goes out from the game if the online player size in the created game is smaller than 1 I close the ports by applying the ondisconnect method. This method clear the objects and makes game port close. Despite of the fact that the code says the port is closed it is not exactly closed. After the first game creation if another user wants to create a new game it gets an error that the ports is already in use. (java.net.BindException). The stack trace is below;


```
INFO: server (0.0.0.0:20051) has been shutdown
Apr 22, 2011 9:20:09 AM org.xsocket.connection.IoAcceptor <init>
WARNING: could not bind server to 0.0.0.0/0.0.0.0:20051. Reason: java.net.BindException: Address already in use
        at sun.nio.ch.Net.bind(Native Method)
        at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:119)
        at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:59)
        at org.xsocket.connection.IoAcceptor.<init>(IoAcceptor.java:117)
        at org.xsocket.connection.IoAcceptor.<init>(IoAcceptor.java:94)
        at org.xsocket.connection.IoProvider.createAcceptor(IoProvider.java:453)
        at org.xsocket.connection.Server.<init>(Server.java:492)
        at org.xsocket.connection.Server.<init>(Server.java:169)
        at           .engine.communication.Game.closeGame(Game.java:246)
        at           .engine.communication.Game.disconnect(Game.java:209)
        at           .engine.communication.Lounge.disconnect(Lounge.java:238)
        at           .engine.Engine.disconnect(Engine.java:102)
        at .engine.communication.gameSocketDataHandler.onDisconnect(gameSocketDataHandler.java:235)
        at org.xsocket.connection.HandlerAdapter.performOnDisconnect(HandlerAdapter.java:334)
        at org.xsocket.connection.HandlerAdapter.access$300(HandlerAdapter.java:42)
        at org.xsocket.connection.HandlerAdapter$PerformOnDisconnectTask.run(HandlerAdapter.java:317)
        at org.xsocket.SerializedTaskQueue.performPendingTasks(SerializedTaskQueue.java:161)
        at org.xsocket.SerializedTaskQueue.access$100(SerializedTaskQueue.java:40)
        at org.xsocket.SerializedTaskQueue$MultithreadedTaskProcessor.run(SerializedTaskQueue.java:189)
        at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:885)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907)
        at java.lang.Thread.run(Thread.java:619)
```

As it can be seen in the trace the exception is thrown in the code line 246 of Game class.The code of this method is below;

```
public void closeGame() {
    try {

        server.close();
        System.out.println("Opened?"+server.isOpen());
        LoungeManager.removeGame(loungeId, gameId);
    } catch (Exception ex) {
        String err = "Game.closeGame:" + ex.getMessage();
        err += logGame.exceptionTrace(ex);
        logGame.appendLog(err, Severity.FATAL);
    }
}
```

Thanks for your help and again thanks the site stackoverflow. The question can be said a little bit long but I want myself to be understandable. I am waiting your advice.

kingspeech


----------

