How to get IP address for connection opened with host name?
How to get IP address for connection opened with host name?
Hi
If I use OpenNetworkConnection() with a host name and not an IP Address is it possible to retrieve the IP address after the connection is made and if so how?
Thanks,
Simon
If I use OpenNetworkConnection() with a host name and not an IP Address is it possible to retrieve the IP address after the connection is made and if so how?
Thanks,
Simon
Simon White
dCipher Computing
dCipher Computing
Re: How to get IP address for connection opened with host na
Rhello my friend !
Yes it is possible.
Try this out :
Bye, have fun my friend !
Yes it is possible.
Try this out :
Code: Select all
InitNetwork()
hostnom.s="www.purebasic.com"
hostip.s=""
hostport.s = ""
cnx = OpenNetworkConnection(hostnom,80,#PB_Network_TCP|#PB_Network_IPv4)
socket = ConnectionID(cnx)
name.SOCKADDR_IN
namelen.i = SizeOf(SOCKADDR_IN)
Debug getpeername_(socket,@name.SOCKADDR_IN,@namelen)
hostip = IPString(name\sin_addr,#PB_Network_IPv4)
hostport = Str(name\sin_port)
Debug "Resultats : "
Debug "Port : " + hostport
Debug "IP : " + hostip
Re: How to get IP address for connection opened with host na
Hi
I should have mentioned that I am using Linux.
Simon
I should have mentioned that I am using Linux.
Simon
Simon White
dCipher Computing
dCipher Computing
Re: How to get IP address for connection opened with host na
Np my friend, here is your answer for linux :
http://man7.org/linux/man-pages/man2/getpeername.2.html
Have fun !
http://man7.org/linux/man-pages/man2/getpeername.2.html
Have fun !
Re: How to get IP address for connection opened with host na
This comes back with the wrong IP address so I assume that I have not configured the structures correctly. The IP address should be 64.191.45.45
Code: Select all
InitNetwork()
hostnom.s="bvddev20.vnkconsultinggroup.com"
hostip.s=""
hostport.s = ""
cnx = OpenNetworkConnection(hostnom,30000,#PB_Network_TCP|#PB_Network_IPv4)
socket = ConnectionID(cnx)
Structure in_addr
s_addr.l
EndStructure
Structure sockaddr_in
sin_family.w
sin_port.c
sin_addr.in_addr
sin_zero.s[8]
EndStructure
name.SOCKADDR_IN
namelen.i = SizeOf(SOCKADDR_IN)
Debug getpeername_(socket,@name.SOCKADDR_IN,@namelen)
hostip = IPString(name\sin_addr,#PB_Network_IPv4)
hostport = Str(name\sin_port)
Debug "Resultats : "
Debug "Port : " + hostport
Debug "IP : " + hostip
Simon White
dCipher Computing
dCipher Computing
Re: How to get IP address for connection opened with host na
You forgot \s_addr and you have to swap the port bytes.
Code: Select all
Structure in_addr
s_addr.l
EndStructure
Structure sockaddr_in
sin_family.w
sin_port.u
sin_addr.in_addr
sin_zero.a[8]
EndStructure
Define namelen.i, cnx
Define name.SOCKADDR_IN
InitNetwork()
cnx = OpenNetworkConnection("bvddev20.vnkconsultinggroup.com", 30000, #PB_Network_TCP|#PB_Network_IPv4)
socket = ConnectionID(cnx)
namelen = SizeOf(SOCKADDR_IN)
If getpeername_(socket, @name, @namelen) = 0
Debug IPString(name\sin_addr\s_addr, #PB_Network_IPv4) + ":" + Str(ntohs_(name\sin_port))
EndIf
Last edited by infratec on Thu May 21, 2020 10:22 pm, edited 1 time in total.
Re: How to get IP address for connection opened with host na
And this works in windows and linux:
Something is wrong with the windows ntohs_() call 
Also the windows sockaddr_in structure is not usable in this case.
Code: Select all
EnableExplicit
Structure in_addr
s_addr.l
EndStructure
Structure sockaddr_in_structure
sin_family.w
sin_port.u
sin_addr.in_addr
sin_zero.a[8]
EndStructure
Procedure.u ntohs(uword.u)
ProcedureReturn uword << 8 | uword >> 8
EndProcedure
Define.i namelen, cnx, socket
Define name.sockaddr_in_structure
InitNetwork()
cnx = OpenNetworkConnection("bvddev20.vnkconsultinggroup.com", 30000, #PB_Network_TCP|#PB_Network_IPv4)
socket = ConnectionID(cnx)
namelen = SizeOf(sockaddr_in_structure)
If getpeername_(socket, @name, @namelen) = 0
Debug IPString(name\sin_addr\s_addr, #PB_Network_IPv4) + ":" + Str(ntohs(name\sin_port))
EndIf

Also the windows sockaddr_in structure is not usable in this case.
Re: How to get IP address for connection opened with host na
Thank-you I missed that.
Simon
Simon
infratec wrote:You forgot \s_addr and you have to swap the port bytes.
Code: Select all
Structure in_addr s_addr.l EndStructure Structure sockaddr_in sin_family.w sin_port.u sin_addr.in_addr sin_zero.a[8] EndStructure Define namelen.i, cnx Define name.SOCKADDR_IN InitNetwork() cnx = OpenNetworkConnection("bvddev20.vnkconsultinggroup.com", 30000, #PB_Network_TCP|#PB_Network_IPv4) socket = ConnectionID(cnx) namelen = SizeOf(SOCKADDR_IN) If getpeername_(socket, @name, @namelen) = 0 Debug IPString(name\sin_addr\s_addr, #PB_Network_IPv4) + ":" + Str(ntohs_(name\sin_port)) EndIf
Simon White
dCipher Computing
dCipher Computing
Re: How to get IP address for connection opened with host na
Works with macOS too.
First time save that
First time save that

My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive