Android Question [SOLVED] UDP riceive & send

Lello1964

Well-Known Member
Licensed User
Longtime User
I want build a small UDP server that receive a packed from a port and redirect to another IP and port.

I' tryed this code, it receive but cannot redeirect.

what did i do wrong?

Redirect Service:
#Region  Service Attributes
    #StartAtBoot: False
 
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim socketReceive, socketSend As UDPSocket
End Sub

Sub Service_Create
    socketReceive.Initialize("sockedReceive",9102,8000)
    socketSend.Initialize("sockedSend",9101,8000)
    End Sub

Sub Service_Start (StartingIntent As Intent)
    Service.StopAutomaticForeground 'Call this when the background task completes (if there is one)
End Sub

Sub Service_Destroy

End Sub

Sub sockedReceive_PacketArrived (PacketFromSender As UDPPacket)
    Log("Received from Sender")
    '...this is he one that you are listening to
 
    Dim InviaUDP As UDPPacket
    InviaUDP.Initialize(PacketFromSender.data,"192.168.1.249",9101)
    socketSend.Send(InviaUDP)
    Sleep(1000)
End Sub

Sub sockedSend_PacketArrived (PacketFromReceiver As UDPPacket)
    Log("Received from Receiver")
    '...this is he on    e that you are listening To
 
    Dim InviaUDP As UDPPacket
    InviaUDP.Initialize(PacketFromReceiver.data,"192.168.1.169",9102)
    socketReceive.Send(InviaUDP)
End Sub
 
Last edited:

Computersmith64

Well-Known Member
Licensed User
Longtime User
I want build a small UDP server that receive a packed from a port and redirect to another IP and port.

I' tryed this code, it receive but cannot redeirect.

what did i do wrong?

Redirect Service:
#Region  Service Attributes
    #StartAtBoot: False
 
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim socketSend, socketReceive As UDPSocket
End Sub

Sub Service_Create
    socketSend.Initialize("sockedReceive",9102,8000)
    socketReceive.Initialize("sockedSend",9101,8000)
End Sub

Sub Service_Start (StartingIntent As Intent)
    Service.StopAutomaticForeground 'Call this when the background task completes (if there is one)
End Sub

Sub Service_Destroy

End Sub

Sub sockedReceive_PacketArrived (Packet As UDPPacket)
    Log("Ricevuto")
    '...this is he one that you are listening to
    Dim InviaUDP As UDPPacket
    InviaUDP.Initialize(Packet.Data,"192.168.1.249",9102)
    socketSend.Send(InviaUDP)
End Sub

Sub sockedSend_PacketArrived (Packet As UDPPacket)
    '...this is he on    e that you are listening To
    Log("Ricevuto")
    Dim InviaUDP As UDPPacket
    InviaUDP.Initialize(Packet.Data,"192.168.1.169",9101)
    socketReceive.Send(InviaUDP)
End Sub
Where is the initial data you want to forward coming from? There's no UDPSocket.Send outside of the _PacketArrived subs in the code you provided. Regardless, it looks to me like your _PacketArrived subs are forwarding to the same port they're listening on, so if you're sending from socketReceive_PacketArrived & expecting it to be received in socketSend_PacketArrived (or vis-a-vis) that won't happen. You might want to rename your variables to make them less confusing... :)

- Colin.
 
Upvote 0

Lello1964

Well-Known Member
Licensed User
Longtime User
Data comes from a Pc Windows, i receive packet from IP 192.168.1.169 port 9102 and send received packet to cashdrawer IP 192.168.1.249 port 9101.

My app Android device IP is 192.168.1.242 i'm on same LAN.
 
Upvote 0

Lello1964

Well-Known Member
Licensed User
Longtime User
I've changed variables name.
changed names:
#Region  Service Attributes
    #StartAtBoot: False
 
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim socketReceive, socketSend As UDPSocket
End Sub

Sub Service_Create
    socketReceive.Initialize("sockedReceive",9102,8000)
    socketSend.Initialize("sockedSend",9101,8000)
    End Sub

Sub Service_Start (StartingIntent As Intent)
    Service.StopAutomaticForeground 'Call this when the background task completes (if there is one)
End Sub

Sub Service_Destroy

End Sub

Sub sockedReceive_PacketArrived (PacketFromSender As UDPPacket)
    Log("Received from Sender")
    '...this is he one that you are listening to
 
    Dim InviaUDP As UDPPacket
    InviaUDP.Initialize(PacketFromSender.data,"192.168.1.249",9101)
    socketSend.Send(InviaUDP)
    Sleep(1000)
End Sub

Sub sockedSend_PacketArrived (PacketFromReceiver As UDPPacket)
    Log("Received from Receiver")
    '...this is he on    e that you are listening To
 
    Dim InviaUDP As UDPPacket
    InviaUDP.Initialize(PacketFromReceiver.data,"192.168.1.169",9102)
    socketReceive.Send(InviaUDP)
End Sub
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
Data comes from a Pc Windows, i receive packet from IP 192.168.1.169 port 9102 and send received packet to cashdrawer IP 192.168.1.249 port 9101.

My app Android device IP is 192.168.1.242 i'm on same LAN.
You're sending the data to 192.168.1.249:9102
InviaUDP.Initialize(Packet.Data,"192.168.1.249",9102)

Is the same app running on the cashdrawer, or is this code just sitting on an Android device "between" Windows & your cashdrawer? If it's the latter, do you really need to use 2 UDP Sockets? Why not just use 1. The port you initialize the socket on is the one that it listens on, but I think you can send to any other port on the same socket:
B4X:
Sub Service_Create
    udpSocket.Initialize("udpSocket",9102,8000)
  
End Sub

Sub updSocket_PacketArrived (Packet As UDPPacket)
    Log("Ricevuto")
    Dim InviaUDP As UDPPacket
    InviaUDP.Initialize(Packet.Data,"192.168.1.249",9101)
    socketSend.Send(InviaUDP)
End Sub

- Colin.
 
Upvote 0

Lello1964

Well-Known Member
Licensed User
Longtime User
I have a 2 'solution, I have tried to use only one sock but cashDrawer does not receive data or the data is not the same as the one sent.


If I send from the PC directly to CashDrawer, it works.
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
Try this. I couldn't test it.

B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
 
    Private FromPC As UDPSocket
    Private ToRT As UDPSocket
    Private PC_Address As String = "192.168.1.100"
    Private PC_Port As Int
    Private RT_Address As String = "192.168.1.249"
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
 
    FromPC.Initialize("FromPC",9102,4096)
    ToRT.Initialize("ToRT",10080,4096)
End Sub

Private Sub FromPC_PacketArrived (Packet As UDPPacket)
    Dim D() As Byte = Packet.Data
    Dim Pck As UDPPacket
    PC_Port=Packet.Port
 
    Log("Data receved from PC : " & D.Length)
    Pck.Initialize(D,RT_Address,9102)
    ToRT.Send(Pck)
End Sub

Private Sub ToRT_PacketArrived (Packet As UDPPacket)
    Dim D() As Byte = Packet.Data
    Dim Pck As UDPPacket
 
    Log("Data receved from RT : " & D.Length)
    Pck.Initialize(D,PC_Address,PC_Port)
    FromPC.Send(Pck)
End Sub
 
Last edited:
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
Same problem, i'll receive data from Pc but not from RT.
:(
Perhaps then you have to listen in broadcast because you don't know on which port RT responds
 
Last edited:
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
Try this

B4X:
FromPC.Initialize("FromPC",9102,4096)
FromPC.Listen
ToRT.Initialize("ToRT",10080,4096)
ToRT.Listen
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
then you have to listen in broadcast because you don't know on which port

Try this

B4X:
FromPC.Initialize("FromPC",9102,4096)
FromPC.Listen
ToRT.Initialize("ToRT",10080,4096)
ToRT.Listen
There is no listen method for UDP Sockets. Initializing the socket causes it to start listening on the port you specify. There is also no way to "listen in broadcast" on a UDP socket. You can send broadcast packets, but you still have to know what port to listen on if you want to receive them & I think it's unlikely that either of his devices are sending broadcasts.
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
There is no listen method for UDP Sockets. Initializing the socket causes it to start listening on the port you specify. There is also no way to "listen in broadcast" on a UDP socket. You can send broadcast packets, but you still have to know what port to listen on if you want to receive them & I think it's unlikely that either of his devices are sending broadcasts.
Unfortunately I'm not in front of the PC and so I'm going to memory.
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
Try this. I couldn't test it.

B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
 
    Private FromPC As UDPSocket
    Private ToRT As UDPSocket
   
 PC_Address As String = "192.168.1.100"
    Private PC_Port As Int
    Private RT_Address As String = "192.168.1.249"
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
 
    FromPC.Initialize("FromPC",9102,4096)
   
End Sub

Private Sub FromPC_PacketArrived (Packet As UDPPacket)
    Dim D() As Byte = Packet.Data
    Dim Pck As UDPPacket
    PC_Port=Packet.Port
 
    Log("Data receved from PC : " & D.Length)
    Pck.Initialize(D,RT_Address,9102)

    IF ToRT.IsInitialized=False then ToRT.Initialize("ToRT",PC_Port)
    ToRT.Send(Pck)
End Sub

Private Sub ToRT_PacketArrived (Packet As UDPPacket)
    Dim D() As Byte = Packet.Data
    Dim Pck As UDPPacket
 
    Log("Data receved from RT : " & D.Length)
    Pck.Initialize(D,PC_Address,PC_Port)
    FromPC.Send(Pck)
End Sub
 
Upvote 0

Lello1964

Well-Known Member
Licensed User
Longtime User
B4X:
 Dim Pck As UDPPacket
 
    Log("Data receved from RT : " & D.Length)
    Pck.Initialize(D,PC_Address,PC_Port)

PC_port is not right, destination port is't same sender PC.
 
Upvote 0

Lello1964

Well-Known Member
Licensed User
Longtime User
Unfortunately I'm not in front of the PC and so I'm going to memory.
Cash Drawr will answer only after received packed from PC.

I doesn't know why doens't work. If i change PC sender Ip to Cash Drawer IP with same port, it work.
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
We have little information and no way to prove it.
Do a sniffer and see the ports it uses, then if you can catch the packets (if they are not encrypted) it can help
 
Upvote 0

Lello1964

Well-Known Member
Licensed User
Longtime User
Solved ..

Packet sended must have same lenght that received...
Solution:
#Region  Service Attributes
    #StartAtBoot: False
    
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim socketReceive     As UDPSocket
    Dim socketSend         As UDPSocket
End Sub

Sub Service_Create
    socketReceive.Initialize("sockedReceive",9102,256)
    socketSend.Initialize("sockedSend",9101,256)
    
End Sub

Sub Service_Start (StartingIntent As Intent)
    Service.StopAutomaticForeground 'Call this when the background task completes (if there is one)
End Sub

Sub Service_Destroy

End Sub

Sub sockedReceive_PacketArrived (PacketFromSender As UDPPacket)
    Log("Received from Sender")
    '...this is he one that you are listening to
 
    Dim msg As String
    msg = BytesToString(PacketFromSender.Data, PacketFromSender.Offset, PacketFromSender.Length, "ASCII")
    Dim b() As Byte=  msg.GetBytes("ASCII")
    Dim InviaUDP As UDPPacket   
    InviaUDP.Initialize(B,"192.168.1.249",9101)
    socketSend.Send(InviaUDP)
End Sub

Sub sockedSend_PacketArrived (PacketFromSender As UDPPacket)
    Log("Received from Receiver")
    '...this is he on    e that you are listening To
    Dim msg As String
    msg = BytesToString(PacketFromSender.Data, PacketFromSender.Offset, PacketFromSender.Length, "ASCII")
    Dim b() As Byte=  msg.GetBytes("ASCII")
    Dim InviaUDP As UDPPacket
    InviaUDP.Initialize(B,"192.168.1.169",9102)
    socketReceive.Send(InviaUDP)
End Sub
 
Upvote 0

Lello1964

Well-Known Member
Licensed User
Longtime User
use :

use this method to read the received packet:
Sub sockedSend_PacketArrived (PacketFromSender As UDPPacket)
    Log("Received from Receiver")
    '...this is he on    e that you are listening To

    Dim msg As String
    msg = [U][B]BytesToString(PacketFromSender.Data, PacketFromSender.Offset, PacketFromSender.Length, "ASCII")[/B][/U]
    Dim b() As Byte=  msg.GetBytes("ASCII")

    Dim InviaUDP As UDPPacket
    InviaUDP.Initialize(B,"192.168.1.169",9102)

    socketReceive.Send(InviaUDP)
End Sub

msg = BytesToString(PacketFromSender.Data, PacketFromSender.Offset, PacketFromSender.Length, "ASCII")
 
Upvote 0
Top