OK...some success!
It has worked for me. I ran my program on my laptop, and then a second or two later on my Android phone. In the following description, an "Instance" is a networked game on a specific port. My process is:
1. Listen for existing Instances (for 3 seconds) and record them
2. Check my game name doesn't exist. If it doesn't, create instance (HostNetwork())
3. Listen for Instances again to see what I find.
This is what happens at each step:
Laptop (runs first)
1. Get 1 message every second (Message No. + Message String)
. 10001 : App Control Network
. 10002 : App Control Network
2. Instance Created (Game9872)
3. My game name broadcast once a second
. 10003 : Game9872
. 10004 : Game9872
. 10005 : Game9872
Phone (runs second)
1. Get 1 message every second (Message No. + Message String)
. 10001 : Game9872
. 10002 : Game9872
2. Instance Created (Game1041)
3. Both game names broadcast once a second
. 10003 : Game9872
. 10004 : Game1041
. 10005 : Game9872
. 10006 : Game1041
. 10007 : Game9872
. 10008 : Game1041
--------------
I'm getting the full game name, but it is only short.
Once you have this name, you should be able to JoinNetwork() by name on the Local network. On a Remote network, you'd have to do something clever like put the port number in the name.
If you are likely to have more than one Instance on the network, you'd have to do something like have a "GetPort" message to record port numbers in use on the network, so you don't use an existing one.
My code for checking for Instances:
` ************************************************
function getNetworks()
` *** @H Gets a list of networks and connected status
` ************************************************
debugWriteHead("Getting Networks")
time# = g.time + 3
bl = createBroadcastListener(45631)
while g.time < time#
bm = getBroadcastMessage(bl)
if bm > 0
debugWrite("Broadcast Message : " + str(bm) + " : " + getNetworkMessageString(bm))
deleteNetworkMessage(bm)
endif
updateGame()
endwhile
deleteBroadcastListener(bl)
endfunction
My code for hosting a network:
` ************************************************
function createHost(name$, clientName$)
` *** @H host a Network of Given Name
` ************************************************
debugWriteHead("Creating Host : " + name$ + " : Client Name : " + clientName$)
`*** Start by getting availale networks
getNetworks()
success = 0
found = 0
for n = 1 to gNetworkSystem.count
`*** Does name exist?
if arrNetwork[n].name = name$
found = 1
`*** Is it me?
if arrNetwork[n].host = 1
`*** Did I lose connectivity?
if arrNetwork[n].connected <> 1
arrNetwork[n].id = hostNetwork(name$, arrNetwork[n].clientName, arrNetwork[n].port)
debugWrite("Network recovered (success)")
success = 1
endif
debugWrite("This network already active (success)")
success = 1
endif
debugWrite("Network name already taken (fail)")
displayMessage("Network name already taken",0,4)
endif
next n
` Not found, add it
if found = 0
debugWrite("New Network Name requested")
inc gNetworkSystem.count, 1
dim arrNetwork[gNetworkSystem.count] as tNetwork
arrNetwork[gNetworkSystem.count].name = name$
arrNetwork[gNetworkSystem.count].clientName = clientName$
arrNetwork[gNetworkSystem.count].port = gNetworkSystem.basePort + gNetworkSystem.count
arrNetwork[gNetworkSystem.count].id = hostNetwork(arrNetwork[gNetworkSystem.count].name,arrNetwork[gNetworkSystem.count].clientName,arrNetwork[gNetworkSystem.count].port)
endif