Re: List resources on local area network
Posted: Sun Oct 11, 2009 4:26 pm
And reverse DNS, to get the hostname from an IP address. Anyone know why Windows takes so long to do this kind of thing? It takes a good few seconds here...
Code: Select all
Procedure.l IntFromAddress (ip$)
ProcedureReturn inet_addr_ (@ip$)
EndProcedure
Procedure.s AddressFromInt (ip.l)
ProcedureReturn PeekS (inet_ntoa_ (ip))
EndProcedure
; Create WSA version number (damn, ugly!)...
high.b = 1: low.b = 1
wsaversion.w
PokeB (@wsaversion, high) ; Gotta poke major version number into low byte...
PokeB (@wsaversion + 1, low) ; ... and minor version number into high byte
If WSAStartup_ (wsaversion, wsa.WSAData) = #NOERROR
ip$ = "192.168.0.2" ; PC on local network
; ip$ = "212.58.226.79" ; BBC News site (shows "newslb308.telhc.bbc.co.uk" at time of writing)
ipaddr = IntFromAddress (ip$)
*host.hostent = gethostbyaddr_ (@ipaddr, 4, #AF_INET)
If *host
Debug ip$ + " = " + PeekS (*host\h_name)
Else
Debug "Error... parse results as per MSDN!"
EndIf
WSACleanup_ () ; Close Windows sockets stuff...
EndIf