Author Topic: Connecting to the local server we've just created  (Read 3053 times)

troglodescu

  • Guest
Connecting to the local server we've just created
« on: May 29, 2013, 03:52:29 PM »
I'm using the ExampleMenu.cs as template for all my network functionality. Of course some things must be changed and I don't know how, like the following:
- After calling TNServerInstance.Start(serverTcpPort, udpPort, "server.dat", lobbyPort), and after TNServerInstance.isActive returns true, I want to automaticaly connect to the local server that was just created but I don't know what address to use.

Also, I would generaly need a way to allways know the IP address when pausing the game for instance, so other players can connect manually to that address.

I currently use this which works fine on everything except iOS:

  1.  try
  2.         {
  3.  
  4.             string sHostName = System.Net.Dns.GetHostName();
  5.             System.Net.IPHostEntry ipE = System.Net.Dns.GetHostEntry(sHostName);
  6.             System.Net.IPAddress[] IpA = ipE.AddressList;
  7.  
  8.             labelIP.text = "Your I.P.: " + IpA[0].ToString();
  9.         }
  10.         catch (System.Exception e)
  11.         {
  12.             Debug.LogWarning("tried to read IP but smth failed " + e.Message);
  13.             labelIP.text = "Offline";
  14.         }

I'm sure there's a better and safer way to get the IP (Unity/Tnet wrapper functions) address regardless of the device or OS.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Connecting to the local server we've just created
« Reply #1 on: May 29, 2013, 06:18:47 PM »
Use "TNet.Tools.localAddress" as the address when connecting to yourself, or simply "127.0.0.1".

troglodescu

  • Guest
Re: Connecting to the local server we've just created
« Reply #2 on: May 31, 2013, 10:54:37 AM »
Thanks for the answer.

I have another minor issue concerning TNet:

if( TNManager.isConnected == false ) it seems that you cannot use Send("RFC_Name", aPlayer) (because it is not connected, that player is of course the Host), only use Send("RFC_Name", Target.Host or Target.All, etc). So the Send function that uses the Target enum works and the one who uses Player as target doesn't. Is it supposed to be like this ? Now I have to write two different codes for online mode and offline:

  1. if (TNManager.isConnected)
  2.         {
  3.             tno.Send(2, TNManager.GetPlayer(aPlayerID), positionToSpawnAt, rotationToSpawnAt);
  4.         }
  5.         else
  6.         {
  7.             tno.Send(2, Target.Host, positionToSpawnAt, rotationToSpawnAt);
  8.         }
I'd rather just use one line of code which I expected to work since aPlayerID is the only player around, thus should just send it to itself
  1. tno.Send(2, TNManager.GetPlayer(aPlayerID), positionToSpawnAt, rotationToSpawnAt);


ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Connecting to the local server we've just created
« Reply #3 on: May 31, 2013, 01:46:13 PM »
Hmm. Well, my question is why are you trying to send a packet to a player when you are not actually connected? If you want to send a packet to the host, you should be doing just that. If you want to host a packet to a specific player... that implies that you should be connected by that point, as otherwise there are no players to choose from to begin with.

troglodescu

  • Guest
Re: Connecting to the local server we've just created
« Reply #4 on: May 31, 2013, 02:39:04 PM »
Yeah, I guess you're right.

My logic was the following:

Step 1: Send request to host to see where to spawn. The RFC contains the Player ID so the server knows where to send back the answer.

Step 2: Host receives request from the player that started step 1 (if not connected, it is one and the same) and server decides where that player should spawn, so send back to that player the spawn RFC

Step 3: ???

Step 4: Profit :D

I was thinking that even though I'm not connected, I could still juggle with TNet's mechanics and there are still players (only 1) and sending RFCs to that player would behave the same connected or not.

troglodescu

  • Guest
Re: Connecting to the local server we've just created
« Reply #5 on: May 31, 2013, 02:41:51 PM »
While I have your appreciated attention, there's another issue that bugs me about TNet:

If there's any error in the call of an RFC, it allways returns the very general and non-helpful exception Exception has been thrown by the target of an invocation. so it is very difficult to track the runtime errors :(

Is there a way to make these errors more explicit ?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Connecting to the local server we've just created
« Reply #6 on: June 01, 2013, 01:30:46 AM »
In the editor you should get more than just that exception. It actually tells you what function you are calling and what arguments it expects to have.