UDP: Finding Hosts

Blitz3D Forums/Blitz3D Programming/UDP: Finding Hosts

Nathaniel(Posted 2009) [#1]
I've been playing around with networking and have successfully implemented UDP into an action game of mine. To join the host, you have to type in the IP address of the host computer.

So I was wondering:
Is there a way to find a remote host without knowing the host's IP address or name? There doesn't seem to be an answer in these forums. The only method I can think of is blindly sending messages to every possible IP address my router will allow.


Bobysait(Posted 2009) [#2]
You need a static server with a database to store player's Ips

So, you 'll need some php codes to communicate between blitz3d and the database.


AJ00200(Posted 2009) [#3]
Yes, thats the only way.


Nate the Great(Posted 2009) [#4]
So, you 'll need some php codes to communicate between blitz3d and the database.
So, you 'll need some php codes to communicate between blitz3d and the database.



theoretically no you dont need php. You must have a main server and hardcode its ip into your game, then the game can make a request of all of the server names and their ip's and the number of player etc... on the server side, when someone hosts a server, the main server's ip is again hardcoded into it. It tells the server its ip and its name and the server can then give all of the clients its ip and name so they can join. once they join that server, they can disconect from the main server as it is no longer needed.

What I have always wondered is how some games seem to not need a server and can simply find other people on the same local network. I never understood how they did it though.


RifRaf(Posted 2009) [#5]
You need a static server with a database to store player's Ips
So, you 'll need some php codes to communicate between blitz3d and the database.


You must have a main server and hardcode its ip into your game


well you dont have to do this a specific way, there are many options. While I think a php database connection would be the cleanest way, its not the only way.

I dont know php at all, so I didnt use it for TinyTanks. I used BlitzGET from the archives to manage a flatfile system on my webhost(ftp). Server lists are downloaded from there and dislpayed in the client. The webhost does get its list from php entry that someone made for me, whereas a server admin can add his server to the list.

I also made a pingbot that will download the list and ping each server over and over so it can remove anyone from the "active" list that is not online, and reactivate them when it gets a good ping.

As far as hardcoding any addresses into the client, I would perhaps hardcode a no-ip address so you can reroute it at any time without a client redistribution.


I could go on for pages but the basic notion is there are many ways.


Nathaniel(Posted 2009) [#6]
Thanks for all the help!

I should have specified that I'm asking about LAN networking, where the host might be any computer with any IP.

What I have always wondered is how some games seem to not need a server and can simply find other people on the same local network. I never understood how they did it though.
This is where I'm confused. Nearly all modern PC multiplayer games seem to be able to locate remote systems without a problem, without the help of a main host.


_PJ_(Posted 2009) [#7]
I think a lot of multiplayer games use a form of specialised lobby server such as Gamespy or the Valve gameservers.
A host sets up their game and the details are sent to the lobby server where clients can browse to find the game they want.

The suggestions above generally involve providing your own server to use as a lobbying one.

Otherwise, there's no way a host's IP can be 'stored' for the clients to browse, without, perhaps, a client scanning every possible IP ( !!! ) in the vain hope of stumbling upon a server running the game!

_______________________________________________________________

In a LAN environment, scanning local IP ranges is more feasible, and beyond Blitz' native capability, Perhaps, some libraries etc. may be able to detect particular port connections and these ports can be specific to your game.


Nathaniel(Posted 2009) [#8]
Thanks again for all the help. Surprisingly, sending messages to all the IPs the router allows is the answer (if I don't want to use the main server concept):

Server:
n = CountHostIPs("")
local_ip = HostIP(1)
Print "Local IP Address: " + DottedIP(local_ip)

Print "Establishing UDP Stream..."
UDP_Stream = CreateUDPStream(8080)
If UDP_Stream Then Print "-Successful"
If Not UDP_Stream Then Print "-Failed":Delay 2000:End

Print "Waiting for contact..."
While Not KeyDown(1)
	message_ip = RecvUDPMsg(UDP_Stream)
	If message_ip
		Print "Found Client at: "+DottedIP(message_ip)
		WriteString(UDP_Stream,"1")
		SendUDPMsg(UDP_Stream,message_ip,8080)
	EndIf
Wend
CloseUDPStream(UDP_Stream)
End

Client:
n = CountHostIPs("")
local_ip = HostIP(1)
Print "Local IP Address: " + DottedIP(local_ip)

Print "Establishing UDP Stream..."
UDP_Stream = CreateUDPStream(8080)
If UDP_Stream Then Print "-Successful"
If Not UDP_Stream Then Print "-Failed":Delay 2000:End

Print "Searching for Computers..."
Local remote_ip$
For i = 100 To 199;Send a message to all the IPs the Router will allow
	If i <> Right(DottedIP(local_ip),3);Makes sure we aren't sending a message to ourselves
		WriteString(UDP_Stream,"1")
		remote_ip = DotToInt("192.168.1."+Str(i))
		SendUDPMsg(UDP_Stream,remote_ip,8080)
	EndIf
Next

While Not KeyDown(1)
	message_ip = RecvUDPMsg(UDP_Stream)
	If message_ip
		Print "Found Host at: "+DottedIP(message_ip)
	EndIf
Wend
CloseUDPStream(UDP_Stream)
End

Function DotToInt%(ip$)
	off1=Instr(ip$,".")	  :ip1=Left$(ip$,off1-1)
	off2=Instr(ip$,".",off1+1):ip2=Mid$(ip$,off1+1,off2-off1-1)
	off3=Instr(ip$,".",off2+1):ip3=Mid$(ip$,off2+1,off3-off2-1)
	off4=Instr(ip$," ",off3+1):ip4=Mid$(ip$,off3+1,off4-off3-1)
	Return ip1 Shl 24 + ip2 Shl 16 + ip3 Shl 8 + ip4
End Function

Note: The Server has to be running first


Bobysait(Posted 2009) [#9]
theoretically no you dont need php. You must have a main server and hardcode its ip into your game, then the game can make a request of all of the server names and their ip's and the number of player etc...


well you dont have to do this a specific way, there are many options. While I think a php database connection would be the cleanest way



It's also the more convenient way.
What we should know is :
+> Many servers does not allow access from other application than a web browser ( like "Free" ) , so you won't be able to send any request without a direct access. Else, the server can be a personal-computer, but, do a lot of people owns such a dedicated machine ? For sure, no. So there is many way to achieve IPs to local machine, but most of them are not reliable with most of the games. ( that are, Blitz3d games, and not industrial games... so they are not intended to make a great commercial benefits ... and of course, dedicated machine are expensive )

Eventually, you can use an iRC channel... but it's not a secured way.
An other way is to use DirectPlay ( that is the same way as using database+server, except, the server is the blitzbasic one's ) . But, it's not a very flexible way...


xtremegamr(Posted 2009) [#10]
Thanks again for all the help. Surprisingly, sending messages to all the IPs the router allows is the answer (if I don't want to use the main server concept):

Erm...that'll only work on small LAN's. If you do that on the Internet (or a big LAN), it'll take FOREVER.

An other way is to use DirectPlay ( that is the same way as using database+server, except, the server is the blitzbasic one's ) . But, it's not a very flexible way...

I wouldn't recommend DirectPlay; you're better off creating your own master server or using a specialized game lobby system (ex. GameSpy).


SytzeZ(Posted 2009) [#11]
It's not that hard to create a simple PHP script, I can post some code if you need to.
There are a lot of free hosts that support PHP and MySQL (I use 000webhost.com, it's good IMO)