GetIPbyName

Hier könnt Ihr gute, von Euch geschriebene Codes posten. Sie müssen auf jeden Fall funktionieren und sollten möglichst effizient, elegant und beispielhaft oder einfach nur cool sein.
Benutzeravatar
jpd
Beiträge: 380
Registriert: 14.02.2005 10:33

GetIPbyName

Beitrag von jpd »

Hi,

habe heute rumgespielt ...

und denke das es für diesen bereich passt.

Ciao
jpd

Code: Alles auswählen

;http://www.purebasic.fr/german/archive/viewtopic.php?t=834#5330


Procedure.s Uni2Ansi2(*Unicode.l) 
  Size.l = WideCharToMultiByte_(#CP_ACP, 0, *Unicode, -1, #Null, #Null, #Null, #Null) 
  ansi.s=Space(Size) 
  WideCharToMultiByte_(#CP_ACP, 0, *Unicode, -1, @ansi, Size, #Null, #Null) 
  ProcedureReturn ansi 
EndProcedure 

Structure host_info
  computername.s
  ip_adress.s[8]  ;number of ip adress on the computer
  error.s
EndStructure
Global NewList host_info.host_info()

Procedure.s GetIPbyName (NameIP.s) 
  
  Protected AdressNumber.l
  AddElement( host_info())
  host_info()\computername=NameIP
  
 
    CompilerIf #PB_Compiler_Unicode 
      pHostinfo = gethostbyname_(Uni2Ansi(@NameIP))
    CompilerElse
      pHostinfo = gethostbyname_(NameIP)
    CompilerEndIf
    If pHostinfo = 0 
      
      host_info()\error=  "Unable To resolve domain name" 
    Else 
      CopyMemory (pHostinfo, hostinfo.HOSTENT, SizeOf(HOSTENT)) 
      If hostinfo\h_addrtype <> #AF_INET 
        
        host_info()\error=  "A non-IP address was returned"
      Else 
        
        While PeekL(hostinfo\h_addr_list+AdressNumber*4) 
          ipAddress = PeekL(hostinfo\h_addr_list+AdressNumber*4) 
         
         host_info()\ip_adress[AdressNumber]=StrU(PeekB(ipAddress),0)+"."+StrU(PeekB(ipAddress+1),0)+"."+StrU(PeekB(ipAddress+2),0)+"."+StrU(PeekB(ipAddress+3),0) 
          AdressNumber+1 
        Wend 
      EndIf 
    EndIf 
  ;If InitNetwork() 
 
EndProcedure 
If InitNetwork() 
  GetIPbyName ("jpd-pc")
Else 
  MessageRequester("Info",  "Network can't be initialized")
  End
EndIf 
  
ResetList ( host_info())

While NextElement(host_info())
  Debug  host_info()\computername
  If host_info()\error=""
    For x=0 To 7
      If host_info()\ip_adress[x] <>""
        Debug host_info()\ip_adress[x]
      EndIf   
    Next
  Else
    Debug "Error: "+  host_info()\error
  EndIf   
Wend 

ClearList( host_info())
Zuletzt geändert von jpd am 20.10.2007 00:42, insgesamt 1-mal geändert.
PB 5.10 Windows 7 x64
Benutzeravatar
HeX0R
Beiträge: 3054
Registriert: 10.09.2004 09:59
Computerausstattung: AMD Ryzen 7 5800X
96Gig Ram
NVIDIA GEFORCE RTX 3060TI/8Gig
Win11 64Bit
G19 Tastatur
2x 24" + 1x27" Monitore
Glorious O Wireless Maus
PB 3.x-PB 6.x
Oculus Quest 2 + 3
Kontaktdaten:

Beitrag von HeX0R »

Benutzeravatar
jpd
Beiträge: 380
Registriert: 14.02.2005 10:33

Beitrag von jpd »

Hi HeXor,

wenn dass nicht jemand anderes stört..

ist kein problem quellangabe eingetragen!
PB 5.10 Windows 7 x64
Antworten