Author Topic: First Run works, any after fails with error Only one usage of each socket addres  (Read 2709 times)

morty346

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 11
    • View Profile
I have a monobehvaiour called NetworkManager that starts my local server, starts a channel and handles the TNET callbacks and so forth
I have been using it for awhile, and now all of a sudden I only can run my server once before I get told my socket is in use

I have to log off my computer or restart it for the socket to no longer be in use, otherwise any sequential starts will not work



The Error

  1. [TNet] Udp.Start: Only one usage of each socket address (protocol/network address/port) is normally permitted.
  2.  
  3. UnityEngine.Debug:LogError(Object)
  4. TNet.UdpProtocol:Start(Int32) (at Assets/TNet/Common/TNUdpProtocol.cs:136)
  5. TNet.UdpLobbyServer:Start(Int32) (at Assets/TNet/Server/TNUdpLobbyServer.cs:49)
  6. TNServerInstance:StartLocal(Int32, Int32, String, Int32, Type) (at Assets/TNet/Client/TNServerInstance.cs:179)
  7. TNServerInstance:Start(Int32, Int32, Int32, String, Type) (at Assets/TNet/Client/TNServerInstance.cs:149)
  8. NetworkManager:Start() (at Assets/Scripts/NetworkManager.cs:22)
  9.  


The init code


   
  1.         TNManager.StartUDP(Random.Range(10000, 50000));
  2.  
  3.                 int udpPort = Random.Range(10000, 40000);
  4.                 TNLobbyClient lobby = GetComponent<TNLobbyClient>();
  5.  
  6.                 TNServerInstance.Type type = (lobby is TNUdpLobbyClient) ?
  7.                         TNServerInstance.Type.Udp : TNServerInstance.Type.Tcp;
  8.                 TNServerInstance.Start(serverTcpPort, udpPort, lobby.remotePort, "server.dat", type);
  9.  
  10.         TNManager.CreateChannel("clientChannel", true, 100, "");
  11.  


The shutdown code

  1.  
  2. void OnDisable()
  3.     {
  4.         Debug.Log("Closing Server");
  5.         TNServerInstance.Stop();
  6.  
  7.         TNManager.Disconnect();
  8.  
  9.         Debug.Log(TNServerInstance.isActive);
  10.     }

I see the Closing Server log and the isActive bool shows as false everytime


What gives?


morty346

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 11
    • View Profile
I should also add I am doing this from within the editor

If I run wire shark and check for things happening on the UDP port I can see the server is still polling every few seconds

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
What's your OS? If it worked before and doesn't work now, the obvious question is -- what changed? Did you install some update?

P.S. You never connect to your server in your code. You seem to start the server, then jump straight to creating a channel. Where is the part that connects to it?

staylor

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
We've also seen lobby client threads lingering and broadcasting on next play, but I think it might have been when we stopped the editor and didn't gracefully shut down networking.

ArenMook, perhaps it might be useful for TNServerInstance (and other TNet MonoBehaviours) to provide automatic clean up inside OnApplicationQuit()?

Finally, this might be a long shot, but I wonder whether LobbyServerLink's SendThread() is never exiting the loop on line 99:
  1. while (!mShutdown)
  2.  
... because of a compiler optimisation. Since mShutdown is written to by another thread, it should probably be declared volatile:
  1. protected volatile bool mShutdown = false;
  2.  

morty346, you could always ensure the lobby thread is shut down by joining with it. This isn't ideal because it'll block the calling thread, but it might help with testing. TNLobbyLink.cs ln 55, add mThread.Join():
  1. if (!mShutdown)
  2. {
  3.         mShutdown = true;
  4.         mThread.Join();
  5.         ... etc
  6.  

Cheers,
Scott

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
TNServerInstance closes the connection in its OnDestroy(), which should be called when the app shuts down. At least on my end I've never noticed any lingering connections.

P.S. In regards to the original thread, TNManager.Disconnect() should happen prior to TNServerInstance.Stop().