Android Question How to send multiple integer variables from udp socket

MMA004

New Member
hi everyone
i wanna to send array of integer from udp socket but i can how do it!
hoever im trying to do this but it dose not work well

here my sample code:
code:
Private Sub Label1_Click
    Dim hum=200,temp=320 As Int
    Dim int() As Int = Array As Int(temp,hum)
    Dim byte() As Byte = convert.IntsToBytes(int)
    
    'UdpP.Initialize("data".GetBytes("ASCII"), "192.168.4.1",8888)
    UdpP.Initialize(byte,"192.168.4.1",8888)
    Socket.Send(UdpP)
    
    ToastMessageShow("Sending...",False)
End Sub

I don't get an error with this code, but I get empty data in the receiving section
However, the comment line works fine

thanks a lot for any help
 

drgottjr

Expert
Licensed User
Longtime User
i see global warming has reached your part of the world (hum 200, temp 320). almost as hot as here.
your code is more or less correct. what i don't see is where you initialize your udp socket. also you
need to take into account the size of the data buffer. also, it's risky to name your variables the way you
do ("dim int() as int"). also, never use toast as a debugging tool. use the log.

anyway, i retrofitted a little broadcast routine i have to accomodate your int array. i send this way:


B4X:
    Dim hum=200,temp=320 As Int
    Dim intarray() As Int = Array As Int(temp,hum)
    Dim data() As Byte = bc.IntsToBytes(intarray)
    Log("data size: " & data.Length)
    udp.Initialize("udp", 0, data.length)
    
    Packet.Initialize(data, broadcast, 51049)
    Log("sending...")
    udp.Send(Packet)

on the receiving end, it receives thus:
' don't forget to initialize the receiving socket with the same size buffer

B4X:
    Log("pkt rcvd." & P.Data.Length & " bytes")
  
    Dim mydata() As Int = bc.IntsFromBytes(P.Data)
    Dim i As Int
    For i = 0 To mydata.Length - 1
        Log(mydata(i))   
    Next
    Log("done with this packet")


and in the receiver's log:
broadcast: 192.168.12.255
pkt rcvd.8 bytes
320
200
done with this packet
 
Upvote 0

MMA004

New Member
i see global warming has reached your part of the world (hum 200, temp 320). almost as hot as here.
your code is more or less correct. what i don't see is where you initialize your udp socket. also you
need to take into account the size of the data buffer. also, it's risky to name your variables the way you
do ("dim int() as int"). also, never use toast as a debugging tool. use the log.

anyway, i retrofitted a little broadcast routine i have to accomodate your int array. i send this way:


B4X:
    Dim hum=200,temp=320 As Int
    Dim intarray() As Int = Array As Int(temp,hum)
    Dim data() As Byte = bc.IntsToBytes(intarray)
    Log("data size: " & data.Length)
    udp.Initialize("udp", 0, data.length)
   
    Packet.Initialize(data, broadcast, 51049)
    Log("sending...")
    udp.Send(Packet)

on the receiving end, it receives thus:
' don't forget to initialize the receiving socket with the same size buffer

B4X:
    Log("pkt rcvd." & P.Data.Length & " bytes")
 
    Dim mydata() As Int = bc.IntsFromBytes(P.Data)
    Dim i As Int
    For i = 0 To mydata.Length - 1
        Log(mydata(i))  
    Next
    Log("done with this packet")


and in the receiver's log:
broadcast: 192.168.12.255
pkt rcvd.8 bytes
320
200
done with this packet
thank you for the guidance
Oh yes, sorry, I set the temp and hum variables for testing?

I set the size to 1024 in the udp initialization, which I think shouldn't be a problem

And it should be mentioned that my receiving part is esp8266 board and ArduinoIDE
However, I still receive an empty inbox?
 
Upvote 0
Top