B4J Question UDP Send Reply

aaronk

Well-Known Member
Licensed User
Longtime User
Hi,

I am using a UDP socket and I am getting the ASCII data from the remote connection and I am able to send the data back to the UDP device that sent the message.

However, it only seems to work locally.

I have been told that I should be able to grab the Remote EndPoint, from the socket I receive data on, save it, then use it as the remote endpoint to send your reply back to without having to worry about opening ports in the customers router.

Been told the following is how it's done in .Net but not sure how to do it in B4J..

initiate an asynchronous “BeginReceive”:
B4X:
result = XepUdpClient.BeginReceive(NewAsyncCallback(AddressOfHandleXepUdpReceive),Nothing)

When UDP data arrives, the callback, HandleXepUdpReceive is called and it performs an “EndReceive”:
B4X:
receiveBytes = XepUdpClient.EndReceive(null, UdpRemoteIpEndPoint)

the parameter UdpRemoteIpEndPoint is defined as:
B4X:
DimUdpRemoteIpEndPointAsIPEndPoint=Nothing

IPEndPoint is a class in .net. That parameter, the remote endpoint is kept until I reply. It is simply the remote IP address and port that sent the data to me. I stuff the endpoint into a struct:
B4X:
XepSendData.EP = UdpRemoteIpEndPoint

(That struct also contains the data to be sent in other members. I’m just showing you the remote endpoint (EP) member above)
And I call my UDB Send function (RmtUdpClient is my local endpoint – my local IP address and port):
B4X:
DimlXepUdpClientAsUdpClient= XepSendData.RmtUdpClient
  lXepUdpClient.Send(XepSendData.IOBuff, XepSendData.NumBytesInBuff, XepSendData.EP)

When it goes out of a home’s router to the Internet, the router “remembers” it for several minutes so that when the reply comes back, the router automatically forwards it to the device on the network.

Anyone know how to do this in B4J ?
 

aaronk

Well-Known Member
Licensed User
Longtime User
Hi,

I did this in the PacketArrived sub:

B4X:
Dim Sender_IP As String = Packet.HostAddress

And it returns the WAN IP of the device that sent the UDP message to my B4J app.

B4X:
Sub Initialize
RMSockUDP.Initialize("RMSockUDP",8881,8000)
End Sub

Sub RMSockUDP_PacketArrived (Packet As UDPPacket)
    Dim msg As String
    msg = BytesToString(Packet.Data, Packet.Offset, Packet.Length, "UTF8")
   
    Log("UDP: " & msg)

    Dim Sender_IP As String = Packet.HostAddress
   
    Log(Sender_IP) ' this logs the IP of the sender

    Dim Packet1 As UDPPacket
      Dim data() As Byte
    Dim msg As String = "hello"
          data = msg.GetBytes("UTF8")
          Packet1.Initialize(data, Sender_IP, RMSockUDP.Port)
          RMSockUDP.Send(Packet1)
   
End Sub

The above code works if the device is a local device connected to my network.

But if a device that sends the UDP message to my B4J app and is on the outside of my network my B4J app gets the message but when my B4J app sends the reply the device doesn't get the reply.

I was told that it should even without opening ports in the router as the router will remember the connection for several minutes and will route it to the device on the network.
 
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
I log both they return the same value:

B4X:
Sub RMSockUDP_PacketArrived (Packet As UDPPacket)
    Log(RMSockUDP.Port) ' logs 8881
    Log(Packet.Port) ' logs 8881
End Sub

however I got it to work.

The command I was sending to my device was wrong and now that I am sending the correct value it's working.

Thanks for you help.
 
Upvote 0
Top