Author Topic: How to get server addresses?  (Read 4077 times)

Shifty Geezer

  • Full Member
  • ***
  • Thank You
  • -Given: 7
  • -Receive: 9
  • Posts: 226
    • View Profile
How to get server addresses?
« on: June 27, 2016, 06:11:42 AM »
I have a simple peer-to-peer 2-player networked game. If a player creates a network game, a server is started on their device. Other players can see a list of servers to join and can connect using serverList.internalAddress. How do I get that address for a server running on the device so that the player who starts the network game can immediately connect to it without having to select from a list of available servers?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to get server addresses?
« Reply #1 on: June 27, 2016, 07:01:46 AM »
When using a UDP lobby, you can get a list of servers via the UDP Lobby Client. What you do with the list is up to you. You can connect to the first server on the list if you like. You don't need to present the list to the user.

Shifty Geezer

  • Full Member
  • ***
  • Thank You
  • -Given: 7
  • -Receive: 9
  • Posts: 226
    • View Profile
Re: How to get server addresses?
« Reply #2 on: June 27, 2016, 07:33:59 AM »
Sure, that's what I do currently - if there's only one server, I connect to it. But if there are more than one server on the network and someone creates a new server, I want to connect straight to that one. Do I have to look it up in the lobby list?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to get server addresses?
« Reply #3 on: June 27, 2016, 06:07:59 PM »
Your code is what decides when to display the server list. Same code can just check -- is there 1 server present? If so, connect to it. Is there more than 1? Show the list. Are there none? Create a new one.

Shifty Geezer

  • Full Member
  • ***
  • Thank You
  • -Given: 7
  • -Receive: 9
  • Posts: 226
    • View Profile
Re: How to get server addresses?
« Reply #4 on: June 28, 2016, 03:43:04 AM »
What I want is if there's more than one server, connect to the local server without showing the list. Is that possible? The examples all seem to be about selecting a server, which seems a little redundant in a mobile game where you'd rather have a 'create network game' button. What I have used is connecting to the first server in the list when one is created, but that won't work if there's more than one server on the network. Or does the first server in the list always point to a local server if there is one?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to get server addresses?
« Reply #5 on: June 28, 2016, 09:19:49 AM »
You don't need to start a local server at all. As I said earlier... this is 100% your code here. You keep asking if it's possible -- of course it is. It's your code, do what you want. Don't start a local server until you had a chance to check the list of local servers.

Shifty Geezer

  • Full Member
  • ***
  • Thank You
  • -Given: 7
  • -Receive: 9
  • Posts: 226
    • View Profile
Re: How to get server addresses?
« Reply #6 on: June 28, 2016, 01:06:18 PM »
Maybe you're confused over what's happening? The game is a two player football game played across the LAN. To play a game, one player creates a server on the mobile device, then joins, and then the other player can join. If more than one player on the network (eg. school  router) creates a server, players can select from a list of available games - one per server. However, the servers are ephemeral, coming and going as people create a game and then switch off their phone or swap to some other app. I *have* to have a server created on the device hosting the game because any other server on some random mobile device on the network might disappear any moment.

eg.
iPad 1 creates a network game. This creates a server, Server1. Only one server on the network. iPad 1 joins Server1.
iPad 2 creates a network game. This creates a server, Server2. Two servers on the network. How do I get iPad2 to join Server2 without having to manually select it in a list?
iPad 3 joins a network game. They are presented a list of Server1, Server2 in the lobby. They pick their choice and join that server .

So, whenever I create a new network game, I need to know how to connect to that particular server. The only way I know how to connect to that server is via the lobby. So again I ask, is that the only way to get the server details of a server running on the local device?


cmifwdll

  • Global Moderator
  • Sr. Member
  • *****
  • Thank You
  • -Given: 0
  • -Receive: 149
  • Posts: 285
  • TNet Alchemist
    • View Profile
Re: How to get server addresses?
« Reply #7 on: June 28, 2016, 09:26:59 PM »
Just join 127.0.0.1. You can get the port from TNServerInstance.listeningPort.

Shifty Geezer

  • Full Member
  • ***
  • Thank You
  • -Given: 7
  • -Receive: 9
  • Posts: 226
    • View Profile
Re: How to get server addresses?
« Reply #8 on: June 29, 2016, 05:21:41 AM »
Thanks, that worked.