Finding Server in local network

BlitzMax Forums/BlitzMax Beginners Area/Finding Server in local network

explosive(Posted 2010) [#1]
Hello altogether,

I'm wondering about the best way to find computers in a local network via a router.

Basically I have two computers. Now when the second one starts, I like to access files on my first one. Connecting via the TCP-Commands provided by Blitz ist not a problem at all, I just have to specify the IP-address of the computer. Now that i'm running a router with DHCP (and I don't really want to change that...) I somehow have to find the address given to the computers.

The setup looks like this:
Machine 1: Ubuntu 10.10/openSuse 11.3 Linux
Machine 2: Mac OS X 10.5
Right now I have the server running under linux on port 48620 (looked to be quite uncommon). As I said before connection works perfectly if I directly connect to the address of that computer
So starting the client under MacOS I've tried the following script:
Local tcpsocket:TSocket=CreateTCPSocket()
For x=170 To 180
	Local ip=192*256*256*256+168*256*256+0*256+x
	Print DottedIP(ip)
	If ConnectSocket(tcpsocket,ip,48620)
		Print "server running!"
		stream=CreateSocketStream(tcpsocket)
		Print ReadLine(stream)
		Exit
	Else
		Print "next try..."
	EndIf
	Print "waiting"
	Delay 200
Next


The programme freezes at "ConnectSocket()" when no server could be found. Likely that the router tries to find the computer in the network but I'm not sure. Any idea?

And by the way: is there a better way to find the local ip-address that using "ifconfig"? Something that's directly implemented in BlitzMax?

Many thanks in advance
puzzled Simon


skidracer(Posted 2010) [#2]
Use HostIP to look up another computer's IP:
Print DottedIP(HostIp("localhost"))
Print DottedIP(HostIp("google.com"))
Print DottedIP(HostIp("mylinuxbox"))


Last edited 2010


xlsior(Posted 2010) [#3]
For the HostIP to work, you either need to have a DNS server that the names get registered to (many of the home network routers do have this functionality built-in, but that's not always the case), or the individual computers have to actively advertise themselves on the network (which may or may not be the case depending on the network settings on the individual client computers. Especially if computer thinks it's a public network, it may not broadecast its presense all over the place by default)

Alternatively:

Alternatively, you can look at the IP address of your own computer, and then attempt to connect to the other addresses in the same subnet.
(e.g. you have 192.168.1.17, then try successively to connect to PC's at 192.168.1.1 through 192.168.1.254)

There's also the broadcast IP, typically at .255 (192.168.1.255 in the example above)

If you send a message to the broadcast address, it will be seen by ALL computers in the local LAN. If you have your client listen for incoming connections on a specific port, it should receive the message. If this message includes some identifier of your program and the sender/server IP, you can then have the client in turn use that info to establish a connection to the server.

However, for this to work properly, you really should use something like Brucey's inet module, which allows you to see the netmask associated with your IP. *most* of the time the broadcast address is .255, but if your network is subdivided into smaller chunks or you only got a partial block from your ISP, then you will need the netmask to calculate the actual broadcast address instead of just assuming it's .255

Last edited 2010


explosive(Posted 2010) [#4]
Hi!

I fiddled around a bit. The results using TCP or UDP are very unsatisfying. I had a look at the GameNet-Module and I think it works perfectly for finding computers in the net. I mainly used this tutorial: http://www.blitzbasic.com/codearcs/codearcs.php?code=1640 .

If you're looking for something similar, just write a loop and scan all ips in your subnet. For good results set the local timeout_ms-variable to something like 10 (Millisecs). This is fairly enough for me in a 300MBit WLAN.

Many thanks anyway.
Simon