Tasharen Entertainment Forum

Support => TNet 3 Support => Topic started by: OriginalExtraCrispy on February 22, 2013, 12:40:12 PM

Title: Closing ports
Post by: OriginalExtraCrispy on February 22, 2013, 12:40:12 PM
I'm very, very new to networking code, so my apologies in advance if this is a ridiculous question.

Using the standalone server code, I made a scene with a button that starts the server and a button that stops it. When I press the start server button, the ports are opened. (server.isActive is true). Debug log confirms it. When I hit stop server, it stops -- server.Stop(). The debug log shows the tcp and udp ports as 0.

But when I hit start server again, it claims the server is still active and says the ports are still open. I'm not sure what I'm doing wrong.

Start Server:

  1. private GameServer server = new GameServer();
  2.  
  3. Transform goTCPPort = transform.parent.FindChild("TCP Port");
  4. int tcpPort = int.Parse (goTCPPort.FindChild ("Label").GetComponent<UILabel>().text);
  5.                
  6. Transform goUDPPort = transform.parent.FindChild("UDP Port");
  7. int udpPort = int.Parse (goUDPPort.FindChild ("Label").GetComponent<UILabel>().text);
  8.  
  9.                
  10. if(!server.isActive)
  11. {
  12.         server.name = "My server";
  13.         server.Start(tcpPort, udpPort);
  14.         server.LoadFrom("server.dat");
  15.         Debug.Log("Server is active: " + server.isActive + " " + tcpPort + " " + udpPort);
  16. }
  17. else
  18. {
  19.         Debug.Log("The ports are already open" + " " + tcpPort + " " + udpPort);
  20.         return;
  21. }

Stop Server:

  1. server.SaveTo("server.dat");
  2. server.Stop ();
  3.  
  4. Debug.Log("Server is active: " + server.isActive + " " + server.tcpPort + " " + server.udpPort);
  5. return;

Thanks for any help you can provide.
Title: Re: Closing ports
Post by: OriginalExtraCrispy on February 22, 2013, 02:15:45 PM
An update. A friend showed me how to use
  1. netstat -an |find /i "listening"
from http://www.petri.co.il/quickly_find_local_open_ports.htm to find all your open ports.

When I run it after pressing the Stop Server button, it still shows that my computer is listening for the TCP port, 5127, so server.Stop() isn't freeing the ports. Should I simply run Application.Quit() after? Or is there a way to keep the application running while freeing the ports?
Title: Re: Closing ports
Post by: ArenMook on February 22, 2013, 11:48:19 PM
Off the top of my head I don't see anything wrong with your code. Clicking Start in the provided example starts the server, and using netstat I can see the port open. Hitting Stop stops it, and doing netstat reveals that the port is no longer on the list either.
Title: Re: Closing ports
Post by: OriginalExtraCrispy on February 23, 2013, 12:18:09 AM
Weird. I've tried multiple things, including testing a completed build, and I always see the port open unless I exit the program and Unity entirely. I guess I'll keep looking.
Title: Re: Closing ports
Post by: OriginalExtraCrispy on February 23, 2013, 02:16:00 PM
Discovered the problem. I was opening up another instance of the server instead of using the correct one. Changed the code to use the singleton pattern to prevent that. Thanks for looking into it for me.