How to get your own IP in Blitz?

BlitzMax Forums/BlitzMax Programming/How to get your own IP in Blitz?

Tibit(Posted 2005) [#1]
How do I do it?

EDIT--
Sorry for being unclear, How do I do it in BlitzMax?
EDIT--


Here is some code that's unfortunatly does not work.






semar(Posted 2005) [#2]
Look in the code archive dude !

http://www.blitzbasic.com/codearcs/codearcs.php?code=139

Sergio.


Tibit(Posted 2005) [#3]
True I should check the code archive more often but I need it to work in BlitzMax. I have no problems at all whatsoever with this in Blitz3D.

I converted that codearchive example to BMax - Do you get your IP from it? Is it just my machine?

( btw it requires you to install the BNet module )

' Get the IP on this machine
ips=CountHostIPs("")
If Not ips End '; No countee, no IP

IP=HostIP(1)
DotIP$=DottedIP(ip)

Global octetList:TList = CreateList()

Type Toctet
	Field value
	Method New()
		octetList.Addlast(Self)
	EndMethod
End Type

Global octets[3]

Print "Source Integer IP:"+IP
Print "Sourse Padded  IP:"+DotIP$
Print
Print "=---Converting---="
Print

If ParseIP(DotIP)
	count=3
	
	For Local O:Toctet = EachIn octetList
		octets[count] = o.value
		Print "Octet "+count+" ="+octets(count)
		count=count-1
	Next
EndIf

byte4=octets[3] Shl 24
byte3=octets[2] Shl 16
byte2=octets[1] Shl 8
byte1=octets[0]
ip2=byte4 Or byte3 Or byte2 Or byte1

Print "Converted:" + DotIP +" back to :"+ ip2

WaitKey()
End

Function parseip(ip$)
	If Len(ip$)>6
		ip$=ip$
		lastperiod=1
		For t=1 To 4							'; four octets
			period=Instr(ip,".",lastperiod)		'; Separtated by 3 periods
			If period=0
				If t=4							'; If its the last octet Then we dont expect To find a period
					period=Len(ip)+1			'; stay within bounds
				Else	
					Return False
				EndIf
			EndIf
		
			If period>lastperiod
				octet = Int( Mid$(ip,lastperiod,period-lastperiod) )
				If octet>=0 And octet<=255
					o:Toctet=New Toctet
					o.value = octet
				Else
					Return False
				EndIf
			Else
				Return False
			EndIf
			lastperiod=period+1
		Next
		octet=0
		Return True
	Else
		Return False
	EndIf
End Function


This is the part which don't work for me?!
ips=CountHostIPs("")
If Not ips End '; No countee, no IP
There is ends.. A bug in BNet?


semar(Posted 2005) [#4]
btw it requires you to install the BNet module
Where do I find the BNet module ?


Tibit(Posted 2005) [#5]
Where do I find the BNet module ?

Here: BNet by Vertex

What is BNet?

A Module to BMax that mimics the network functions of original blitz.


Vertex(Posted 2005) [#6]
Hi!
HELLO
Source Integer IP:-1062731164
Sourse Padded IP:192.168.2.100

=---Converting---=

Octet 3 =192
Octet 2 =168
Octet 1 =2
Octet 0 =100
Converted:192.168.2.100 back to :-1073741824

Process terminated


This is my IP in my local area networtk(1 DSL-Router + 2 PCs).

You can get a IP in a LAN from a DHCP_Server dnynamical or can coose a static IP.

The internet IP you get from a Provider became the router(or a gateway?).

This works also with Blitz3D but not with BNet on your system?

cu olli


Tibit(Posted 2005) [#7]
This works also with Blitz3D but not with BNet on your system?

Yes! In Blitz3D I get the IP 192.168.0.100 but in BMax I get 0.0.0.0 IntIp 0

I guess we have a strange bug here =)

Vertex, Awesome work with the BNet!


Tibit(Posted 2005) [#8]
Do you think it could have to do with some of my firewall/router settings?
My ip is static btw. And the ip I want it primary the internal. So people in the network can connect, or if you have a unique ip ( not behind router ) - anyone can connect to you.


Vertex(Posted 2005) [#9]
Hi!
Can you test the following application?
http://www.sucox.art-fx.org/BlitzPastingv1/showupload.php?re_filename=iptest.rar

Enter as hostname nothing and press return.

Whats your hostname? Can you test CountHostIPs with your hostname?

cu olli


Tibit(Posted 2005) [#10]
My Hostname is : truplo
and the ip I get is 192.168.0.100. It also says "No aliases found"

When I go :
Count = CountHostIPs("truplo")
Somehow count is = 0 ?

----------------------------------------------------
Also noticed another strange thing which might help to solve the problem.

When I CountHostIP("www.google.com") in BMax I get:
119.119.119.46

But www.google.com in your program returns two Ips:
66.249.85.99
66.249.85.104


Vertex(Posted 2005) [#11]
Hi!
Can you test the following code?
Local Hostent:THostent, pHostent:Byte Ptr, pIP:Int Ptr
Local Index:Int

Hostent = New THostent
pHostent = w_gethostbyname("".ToCString())

If pHostent = Null Then
	Print "No IPs found"
	End
Else
	MemCopy Hostent, pHostent, SizeOf(Hostent)
EndIf

Repeat
	pIP = Hostent.ipAddrList[Index]
	If pIP = Null Then
		Exit
	Else
		Index :+ 1
		Print DottedIP(s_ntohl(Var pIP))
	EndIf
Forever


cu olli


Tibit(Posted 2005) [#12]
YES! Thanks! Works perfect!
I both get my own ip and I can get domain names ips.

I have no idea how to read that code ;)

Out of curiosity what was the problem?

I made a function of it, Vertext you own network programming =) You should really put this in the Code Archive for others. (does it require BNet?)
Function GetIP$( url$="" )'Default local ip

	Local Hostent:THostent, pHostent:Byte Ptr, pIP:Int Ptr
	Local Index:Int

	Hostent = New THostent
	pHostent = w_gethostbyname(url.ToCString())

	If pHostent = Null
		'DebugLog "No IPs found"
		Return 0
	Else
		MemCopy Hostent, pHostent, SizeOf(Hostent)
	EndIf

	pIP = Hostent.ipAddrList[Index]
	If pIP = Null 'fail
		Return 0
	Else
		Index :+ 1
		Return DottedIP(s_ntohl(Var pIP))
	EndIf

EndFunction

'Print GetIP() 'To Test



Vertex(Posted 2005) [#13]
Hi!
This requieres:
Type THostent
   Field bpName:Byte Ptr 
   Field bpAliases:Byte Ptr Ptr 
   Field shAddrtype:Short 
   Field shLength:Short 
   Field ipAddrList:Int Ptr Ptr 
End Type

Type TWSAData
   Field shVersion:Short
   Field shHighVersion:Short
   Field sDescription:String
   Field sSystemStatus:String
   Field shMaxSockets:Short
   Field shMaxUdpDg:Short
   Field bpVendorInfo:Byte Ptr
End Type

Extern "OS"
   Function w_gethostbyname:Byte Ptr(pName:Byte Ptr) = "gethostbyname@4"

   Function w_WSAStartup:Int(shVersion:Short, tWSA:Byte Ptr) = "WSAStartup@8"
   Function s_WSACleanup:Int() = "WSACleanup@0"
End Extern

Function s_WSAStartup(shVersion:Short, tWSA:TWSAData Var)
   Local bTemp:TBank, iReturn:Int, bChar:Byte, shIndex:Short
   Local sDescription:String, sSystemStatus:String
   
   bTemp = CreateBank(400)
   
   iReturn = w_WSAStartup(shVersion, BankBuf(bTemp))
   If iReturn <> 0 Then
      Return iReturn
   Else
      For shIndex = 4 To 260
         bChar = PeekByte(bTemp, shIndex)
         If bChar = 0 Then Exit
         sDescription = sDescription+Chr(bChar)
      Next
      
      For shIndex = 261 To 359
         bChar = PeekByte(bTemp, shIndex)
         If bChar = 0 Then Exit
         sSystemStatus = sSystemStatus+Chr(bChar)
      Next
   
      tWSA.shVersion     = PeekShort(bTemp, 0)
      tWSA.shHighVersion = PeekShort(bTemp, 2)
      tWSA.sDescription  = sDescription
      tWSA.sSystemStatus = sSystemStatus
      tWSA.shMaxSockets  = PeekShort(bTemp, 390)
      tWSA.shMaxUdpDg    = PeekShort(bTemp, 392)
      tWSA.bpVendorInfo  = Null

      Return iReturn
   EndIf
End Function


Just init this with:
Local tWSA:TWSAData = New TWSAData
s_WSAStartup(MAKESHORT(2, 0), tWSA)

And terminate it with:
s_WSACleanup


For Linux(and MacOSX?) you don't need WSAStartup and WSACleanup. And you must use this:
Function w_gethostbyname:Byte Ptr(pName:Byte Ptr) = "gethostbyname"



The init and cleanup where execute automaticly by BNet.
http://vertex.art-fx.org/bnet.zip <- new version with fixed bug

thx you!

cu olli


Tibit(Posted 2005) [#14]
Thanks! Great work.

Wounder if everything changes when Mark releases the official netowrk module..