Author Topic: Stopping a server seems to 'lock' its port.  (Read 4417 times)

Shifty Geezer

  • Full Member
  • ***
  • Thank You
  • -Given: 7
  • -Receive: 9
  • Posts: 226
    • View Profile
Stopping a server seems to 'lock' its port.
« on: July 01, 2016, 04:50:51 AM »
When I Stop a server on a device and then Start a new one, I get a "The Resquested address is not valid in its context" error on the client when trying to join. I've found this is because the specified port isn't being used.

To create a server, I use (fixed):
    TNServerInstance.Start(5127, serverUdpPort, lobby.remotePort, "server.dat", TNServerInstance.Type.Udp

To connect on the client, I use:
    ServerList.Entry ent = serverList[0];
    print ("Button Press Join "+serverList.Count+"   "+ent.internalAddress+"    "+ent.externalAddress);
    TNManager.Connect(ent.internalAddress, ent.internalAddress);

...and the server address is listed as 192.168.56.1:5127 and it connects to just fine.
I then stop the server and start a new instance on the first device the same way. This time trying to join, its address is given as 192.168.56.1:0 and connection fails.

« Last Edit: July 01, 2016, 02:06:00 PM by Shifty Geezer »

cmifwdll

  • Global Moderator
  • Sr. Member
  • *****
  • Thank You
  • -Given: 0
  • -Receive: 149
  • Posts: 285
  • TNet Alchemist
    • View Profile
Re: Stopping a server seems to 'lock' its port.
« Reply #1 on: July 01, 2016, 06:42:28 AM »
I'm not sure how you're even able to connect with that code. Must be some considerable changes to TNManager in TNet3.
Regardless, I looked over the patch notes and it seems TNServerInstance still exists in TNet3, so you should use that if you want your clients to host servers.

Creating a server:
TNServerInstance.Start(5127);

Joining the server on the client that called TNServerInstance.Start:
while(!TNServerInstance.isListening) yield return null; // (or some other garbage code, just need to wait for TNServerInstance to initialize)
TNManager.Connect("127.0.0.1", 5127);

Stopping the server:
TNServerInstance.Stop();

Joining the server from the serverlist:
What you have works, though you're passing ent.internalAddress for both parameters (not an issue if every client is on the same LAN).

Shifty Geezer

  • Full Member
  • ***
  • Thank You
  • -Given: 7
  • -Receive: 9
  • Posts: 226
    • View Profile
Re: Stopping a server seems to 'lock' its port.
« Reply #2 on: July 01, 2016, 07:45:15 AM »
Sorry, the above is missing code. I missed the server creation part. Pressing a Create Network Game button calls this:
  1. If(!TNServerInstance.isActive){
  2.         serverUdpPort = Random.Range(10000, 50000);
  3.         if(TNServerInstance.Start(5127, serverUdpPort, lobby.remotePort, "server.dat", TNServerInstance.Type.Udp)){
  4.                 TNServerInstance.serverName = "Footy_Server";
  5.                 lbl_NetMessage.enabled = false;
  6.                 twn_netName.PlayForward ();
  7.         }
  8. }
This opens a dialogue to choose Server name and optional password, which then calls the above Connect() to connect.

The Connect() has a callback to join the channel.
  1. void OnNetworkConnect(bool success, string message){
  2.         bool home = Random.value < 0.5f;
  3.         GameDetails.instance.SetDifficulty (1);
  4.         GameDetails.instance.SetGameDetails (MATCHTYPE.NETWORKED, true, home, home, !home,"CLUB_001","CLUB_001");
  5.         If(TNServerInstance.isActive){
  6.                 TNManager.JoinChannel (1,"MatchLauncher", false, 2, netPassword);
  7.         }else{
  8.                 print ("Channels stuff "+success+"   "+message);
  9.                 TNManager.GetChannelList(OnChannelListRefresh);
  10.         }
  11. }
  12.  
I use the internalAddress precisely because it's LAN only. I only use the TNManager.Connect("127.0.0.1:5127"); when joining an already existing local server.

Importantly, TNServerInstance.Start(5127, serverUdpPort, lobby.remotePort, "server.dat", TNServerInstance.Type.Udp) starts the server and connects on the local device. It should be using port 5127, but the serverList entry has it as port 0.

cmifwdll

  • Global Moderator
  • Sr. Member
  • *****
  • Thank You
  • -Given: 0
  • -Receive: 149
  • Posts: 285
  • TNet Alchemist
    • View Profile
Re: Stopping a server seems to 'lock' its port.
« Reply #3 on: July 01, 2016, 08:58:12 AM »
How are you stopping the server?
Should be TNServerInstance.Stop().

Actually, that shouldn't even be causing your problem. The lobbylink uses mGameServer.tcpPort when it adds itself to the lobby server, and mGameServer.tcpPort should never be 0 unless mGameServer.Stop() is called in the span of microseconds. I can't find any cases where you wouldn't be getting an error printed to console though, so check that.

I'm stumped, hopefully Aren or someone else will be able to help.

Shifty Geezer

  • Full Member
  • ***
  • Thank You
  • -Given: 7
  • -Receive: 9
  • Posts: 226
    • View Profile
Re: Stopping a server seems to 'lock' its port.
« Reply #4 on: July 01, 2016, 09:49:07 AM »
Yep, TNServerInstance.Stop() called on a button press.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Stopping a server seems to 'lock' its port.
« Reply #5 on: July 02, 2016, 12:23:30 AM »
Try it with the built-in example that comes with TNet. Does the same issue occur? Does it occur on other platforms? Do you call MakePrivate() on the server anywhere? Need more details here.

Shifty Geezer

  • Full Member
  • ***
  • Thank You
  • -Given: 7
  • -Receive: 9
  • Posts: 226
    • View Profile
Re: Stopping a server seems to 'lock' its port.
« Reply #6 on: July 02, 2016, 10:09:29 AM »
I get the same error with the TNet example. Windows 7 64 bit

1) Start a LAN server. It autojoins
2) Disconnect. The server is listed in the Server List as port 5127.
3) Stop the server
4) Start a LAN server. It autojoins
5) Disconnect. The server is listed in the Server List as port 0.
6) Try to join (press the button). "The request address is not valid in this context";

Same on Android (Nexus 7), although the error at step 6 is simply "unable to join".

Tnet 2 works correctly. From the example menu, create a server and it's on port 5127. Stop it and start a new one and it's still on port 5127.
« Last Edit: July 02, 2016, 10:14:48 AM by Shifty Geezer »

Shifty Geezer

  • Full Member
  • ***
  • Thank You
  • -Given: 7
  • -Receive: 9
  • Posts: 226
    • View Profile
Re: Stopping a server seems to 'lock' its port.
« Reply #7 on: July 04, 2016, 03:41:00 PM »
Also tried another Android device on another network, and the problem persists with the TNet 3 example. Really need this sorted as it's stopping product release and holding up other development on another networked app.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Stopping a server seems to 'lock' its port.
« Reply #8 on: July 04, 2016, 09:49:11 PM »
It seems I forgot to set
  1. mListenerPort = 0;
... in the TNet.GameServer class' Stop() function. Just add that at the end and it'll work as expected.

Shifty Geezer

  • Full Member
  • ***
  • Thank You
  • -Given: 7
  • -Receive: 9
  • Posts: 226
    • View Profile
Re: Stopping a server seems to 'lock' its port.
« Reply #9 on: July 05, 2016, 04:03:51 AM »
Yep, fixed. Thanks!