Cleaning Up - Sockets left bound

Hello,

I’m very new to both Python and BGE.
I have managed to get some network UDP based comms working. However it was after several hours of confusion.
The problem was that every time I stopped BGE to do more coding the socket which was bound for receiving messages was not being released in the OS. It was only being released after closing down Blender.
I’m on Win 7 64bit.
Now I’m not sure if this is a BGE issue, in that BGE has a clean up process not picking up on this.
OR
If its something I am meant to do.

If it is something I am meant to do, i.e. call the sock.close() method can someone please enlighten me as to where and how I should correctly detect when BGE is being stopped so I can add the appropriate code there.

If its a BGE issue any idea how to work around this?

If I recall correctly, Blender has a hardwire setup so that hitting ‘esc’ stops the game. I think there is a flag that turns off that behavior but then you have to add your own logic to detect a key press and exit the game. In that logic that detects the key press, you do your cleanup, then exit.

It has been a while since I added my custom cleanup code so some of those details might be incorrect.

Thanks Kastoria.
Yes I was suspecting it may have something like that, but my digging came up empty.

Anyone got something a little exact for me to follow?

Indeed hitting ESC is quite intrusive. To cleanup you’d have to attach to an object so that del is called. It also depends a bit if your socket is sending or receiving… have a look at this code:

That’s how you attach an thread and make sure it gets cleaned up.

I got a chance to look into this for the details. If there is keyboard sensor bound to ‘esc’ then BGE does not automatically exit when you press that key.

Here is one way to enable cleanup code.

  • Add an Empty to your scene.
  • Give the Empty a Keyboard sensor bound to ‘esc’
  • Given the Empty a Python controller triggered by the keyboard sensor
  • Add you cleanup code to this controller and manually call bge.logic.endGame() once the cleanup has happened.

Wow thanks guys. Two great replies. And a HTTP server too… now that can cross off a few tasks and points on my list for the network comms I was looking to implement!

How do I mark this thread as solved?

I think you have to go into advance mode then there is an option there.