B4R Question Best way how to save IP address as Global - globalstore?

petr4ppc

Well-Known Member
Licensed User
Longtime User
Dear friends, please for help,

I get IP address:
B4X:
ip1() As Byte
Log ("Received IP:", ip1(0),".", ip1(1),".", ip1(2),".", ip1(3) )

And I want to save this IP xxx.xxx.xxx.xxx address as GLOBAL

I am trying
B4X:
    Dim firstipnumber As String = ip1(0)
    Log(GlobalStore.Slot0,"-", ip1(0)) 'get the value from IP1(0)
    Dim ipsaved as String
    Dim cbt(3) As Byte
    byteconvert.ArrayCopy2(GlobalStore.Slot0,0,cbt,0,3)
    ipsaved= byteconvert.StringFromBytes(cbt)
    Log("ipsaved=",ipsaved)
    If firstipnumber =ipsaved Then
            Log("****Same IP was received")
        Else
            Log("****It is differnet IP", "- and this value go to Global:",GlobalStore.Slot0)
            GlobalStore.Put(0,firstipnumber) 'store value in slot #0
    End If

this is functioned...but I dont know if that is good direction how to do it..
because IP address can be f.e. 192.145.3.5 or 192.168.137.15
and I must recognized if declaration is Dim cbt(3) As Byte or Dim cbt(2) As Byte or Dim cbt(1) As Byte

then I want to use this received IP for sending pockets via UDP:
B4X:
usocket.BeginPacket(ip, port)
usocket.Write("value1")
usocket.SendPacket


Please for advice what is the best way for saving IP address as GLOBAL and I think that later I will save it to eeprom.
Thank you very much
p4ppc
 
Last edited:

petr4ppc

Well-Known Member
Licensed User
Longtime User
Thank you for your answer,

I am trying possibilities. I did many dumb mistakes and I was on the bad way because I think after this days that I am not smart...

Yes, after some days playing with udp I must write that this is perfect functioned:

****Example1 - functioned
EDIT:

B4X:
Private ipudp() As Byte = Array As Byte(192, 168, 137, 214) ' this functioned
GlobalStore.Put(0,ip1)

dim port as Unit = 5000
usocket.BeginPacket(GlobalStore.slot0, port)
usocket.Write("Hallo1")
usocket.SendPacket  '----------------- functioned

but this is not functioned because of some my mistake...why please:


****Example2
B4X:
Private Sub usocket_PacketArrived (Data() As Byte, ip1() As Byte, port1 As UInt)
Log ("Received:",Data, " from IP:" , ip1(0),".", ip1(1),".", ip1(2),".", ip1(3) ) '-I see right address

If GlobalStore.Slot0=ip1 Then '-----------------NOT functioned
  dim port as Unit = 5000
  usocket.BeginPacket(GlobalStore.slot0, port)
  usocket.Write("Hallo1")
  usocket.SendPacket  '----------------- without IF - functioned
 else
  GlobalStore.Put(0,ip1)
End if

End sub

maybe this can be smartest solution of example2 ?
B4X:
Dim ip As String
dim ipad as String

ip= ip1(0)
ipad=JoinStrings(Array As String(ip))
ip= ip1(1)
ipad=JoinStrings(Array As String(ipad,".",ip))
ip= ip1(2)
ipad=JoinStrings(Array As String(ipad,".",ip))
ip= ip1(3)
ipad=JoinStrings(Array As String(ipad,".",ip))
Log("Ip address IS=",ipad)

ip= GlobalStore.Slot0(0)
ipad2=JoinStrings(Array As String(ip))
ip= GlobalStore.Slot0(1)
ipad2=JoinStrings(Array As String(ipad,".",ip))
ip= GlobalStore.Slot0(2)
ipad2=JoinStrings(Array As String(ipad,".",ip))
ip= GlobalStore.Slot0(3)
ipad2=JoinStrings(Array As String(ipad,".",ip))
Log("Ipaddress from Globalstore is=",ipad2)
If ipad=ipad2 then log("Functioned")

******************** Example3
and can you please give me advice ...How to save IP from JSON to Globalstore? And how to convert this received IP
for sending UDP?

If I receive IP from JSON, I get
B4X:
Sub JobDone (Job As JobResult)
Dim MaxSize As Int = 20
Dim buffer(MaxSize) As Byte
GetTextValueFromKey(jsontext, "ipfromjson", 0, buffer, MaxSize)
GlobalStore.Put(2,buffer)
Log("IP form json:",GlobalStore.Slot2) '----------------- Here I see right IP address

dim port as Unit = 5000
usocket.BeginPacket(GlobalStore.slot2, port)
usocket.Write("Hallo2")
usocket.SendPacket  '----------------- Not functioned because of my mistake
End sub

Thank you very much,
Best regards
p4ppc
 
Last edited:
Upvote 0

petr4ppc

Well-Known Member
Licensed User
Longtime User
Erel I hope that I am saying true,
but I had tried it many times, I think that here is something.....?

code edited:
B4X:
GetTextValueFromKey(jsontext, "ipfromjson", 0, buffer, MaxSize)
GlobalStore.Put(2,buffer)
Log("IP form json:",GlobalStore.Slot2)                            ' I get 192.168.117.162
Log("IP form json length=",GlobalStore.Slot2.length)        ' 15

' then trying to send data:

Dim port As UInt=5000
usocket.BeginPacket(GlobalStore.slot2, port)                              'it is not functioned....
usocket.Write("Hallo1")
usocket.SendPacket
delay(5000)

usocket.BeginPacket(Array As Byte(192,168,117,162),port)        'this functioned
usocket.Write("Hallo2")
usocket.SendPacket
delay(5000)

Dim ipforcertain() As Byte = Array As Byte(192, 168, 117, 162)
GlobalStore.Put(2,ipforcertain)
usocket.BeginPacket(GlobalStore.slot2, portik)
usocket.Write("Hallo3")
usocket.SendPacket  '----                                                          this functioned
delay(5000)
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
My mistake. The error is expected.

The ip address needs to be 4 bytes, not a string representation of these bytes.

You can parse the string:
B4X:
Dim IpString As String = "192.168.117.162"
Dim bc As ByteConverter
Dim ip(4) As Byte
Dim i As Int = 0
Dim bc As ByteConverter
For Each s() As Byte In bc.Split(IpString, ".")
   ip(i) = bc.StringFromBytes(s) 'bytes cannot be converted to numbers directly
   i = i + 1
Next
'test
For Each b As Byte In ip
   Log(b)
Next
 
Upvote 0

petr4ppc

Well-Known Member
Licensed User
Longtime User
Dear Mr.Erel,

thank you very much. As always yours advices are amazing.

I customized this code for my situation in rows:
B4X:
Dim i As Int = 1 ' was Dim i As Int = 0
and
B4X:
ipadresajakobyte(i-1) = bc.StringFromBytes(s) ' was ipadresajakobyte(i) = bc.StringFromBytes(s)
Because of test routine, now I see correct data of received IP instead of zeros

Thank you very much,
Best regards,
p4ppc
 
Upvote 0
Top