PDA

View Full Version : How to get all computers/IP's on a lan automatically?



Michael Hartlef
20-04-2008, 07:44
Hi folks,

thanks to the sample Eros posted here long time ago I know now how to send stuff of the LAN. I want to implement multiplayer other the LAN in Airdogs. What is uncomfortable it that you have to enter the IP of your counterpart. In games like Age of Empire, when someone creates a lan game as a server, the others can see it automatically and can log in. I guess for this you need to retrieve the IP's of all connected computers and then send them the info about your game. So my question is, how to retrieve the IP's of all connected computers?

Cheers
Michael

ErosOlmi
20-04-2008, 09:43
Michael,

here an example that scan a subnet starting from your local IP.
Script assumes that your computer have just one IP and one NIC (network interface card)
It also assumes that your IP config is masked 255.255.255.0
If all ok, it scan all the 255 possible IP and determine their MAC address.

In your case, you need a function similar to this script but you need to define your own protocol using your own port (I suggest UDT ports) to comunicate between server and possible clients.
The first thing to for the server is to open a communication UDT port and remain in listen mode.
The client when starts, must scan the IP in the subnet and if found a computer send a command (you cna invent whateven here) to the port the server is listening.
If they exchange the expected data than they can start to exchange other info to agree to some common ports to use during ingame operations.

... other stuff will follow but this can give you a go.

Eros


'---------
' Modules
'---
uses "inet"
uses "console"

'---------
' Constants
'---
%MaxIp = 255

%Pos_IP = 1
%Pos_Results = 2
%Pos_Mac = 3

'---------
' Variables
'---
dim Counter as long '---Used to count IPs
dim sIP as string '---Used to form full IP

dim sResults(3, %MaxIp) as string '---Store results

'---------
' Script
'---
'---Determine first IP. ATTENTION, you can have more than one IP configured
' both in the same NIC or in different NIC
dim MyFirstIP as string = INET_GetIp(1)
console_writeline "My first IP is: " & MyFirstIP

'---Determine your lan assuming subnet mask is 255.255.255.0
dim sSubNet as string '---Store initial subnet
dim lPos as long

'---Search the position of the 3rd "."
lPos = instr(MyFirstIP, ".", 3)
'---If found, extract the subnet part (assuming mask is 255.255.255.0)
if lPos then
sSubNet = left$(MyFirstIP, lPos)
console_writeline "My SubNet is: " & sSubNet
else
'---Something wrong
console_writeline "Something wrong with subnet/ip numbers. Check data."
console_waitkey
stop
end if
'---

for Counter = 1 to %MaxIp
sIP = sSubNet & Counter

sResults(%Pos_IP , Counter) = sIP
sResults(%Pos_Results , Counter) = INET_Ping(sIP, 100, "abcdefghijklmnopqrstu")
sResults(%Pos_Mac , Counter) = INET_GetRemoteMACAddress(sIP)

Console_writeline _
format$(Counter, "000") & " " & _
using$("\ \", sResults(%Pos_IP, Counter)) & _
using$("\ \", parse$(sResults(%Pos_Results, Counter), $tab, 3)) & _
sResults(%Pos_Mac, Counter) & _
""

next

'---Here you can do what you need with IP and mac address pair.

console_waitkey

Michael Hartlef
20-04-2008, 14:05
Thanks Eros, I'll give this a try.