Question about two way communication using UDP

JimAvanti

Member
Licensed User
Longtime User
I am new to network communication and I am writing a program on an Android device that will send command strings to a computer running a VB.net application and based on what happens the computer will send back a response to the Android. I chose to use UDP rather than TCP because I thought it would be simpler and less prone to errors if a connection couldn’t be made or when the connection is lost. I open a UDP port in VB.net and setup a separate thread to listen for incoming commands and handle them. I have Basic4Android simply open a UDP port and send commands which VB.Net receives. All this is working perfectly, but I have no idea how to send status back to the Android. The vb.net listener gathers the ip address from the sending Android so I could use that address to send an acknowledgement back to the phone, but I don’t know how I should go about receiving the acknowledgement on the phone since connections on the phone change so often. Can a UDP server be setup on the phone to run for a small amount of time after a command is sent to wait for the result? Anyone have any ideas as to the best way to accomplish this?

Android Code for UDP:

Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("Alarm_Control_1")
If FirstTime Then
UDPSocket1.Initialize("UDP", 0, 8000)
End If
End Sub

Sub SendCommand(Command As String)
UDPSocket1.Initialize("UDP", 0, 8000)
Dim Packet As UDPPacket
Dim data() As Byte
data = Command.GetBytes("UTF8")
Packet.Initialize(data, ip_address, 8000)
UDPSocket1.Send(Packet)
Label_Sent.text = Command
End Sub
 

MiniDemonic

Member
Licensed User
Longtime User
You can refer to my custom push notifications tutorial here: http://www.b4x.com/forum/basic4andr...ustom-push-notifications-server-included.html

I do not program in vb.net so I can't help you with that part. But to receive UDP packets you can use the following code:
B4X:
Sub UDP_PacketArrived (Packet As UDPPacket)
    Dim msg As String
    msg = BytesToString(Packet.Data, Packet.Offset, Packet.Length, "UTF8")
    msgbox(msg, "message from server")
End Sub

The great thing with UDP is that you do not need to actively wait for a packet and you do not need to ping the server to keep a connection alive, because UDP is connectionless.

But I would recommend using a service if you want to receive packets when the app is in the background.
 
Upvote 0

JimAvanti

Member
Licensed User
Longtime User
I did try that "UDP_PacketArrived "code but couldn't get it to work. Am I missing port declarations or a way to call the routine? :BangHead:

I also briefly looked at your push tutorial. The tutorial seems to require a server running at a given IP address to store the most recent Android IP. Is this correct? I have a program running and listening on my PC already and it works good for receiving commands from my Android. When the PC receives a command I try sending back a response to the IP address it received the command from, but I am not having much luck. If I finally do get this to work my next step would be to have the Android recieve responses even if it doesn't send a command first. For this, the PC would always need to know the current ip address of the phone, and a service would have to be running on the phone at all times. I think this is probably what you are doing in your push tutorial.



You can refer to my custom push notifications tutorial here: http://www.b4x.com/forum/basic4andr...ustom-push-notifications-server-included.html

I do not program in vb.net so I can't help you with that part. But to receive UDP packets you can use the following code:
B4X:
Sub UDP_PacketArrived (Packet As UDPPacket)
    Dim msg As String
    msg = BytesToString(Packet.Data, Packet.Offset, Packet.Length, "UTF8")
    msgbox(msg, "message from server")
End Sub

The great thing with UDP is that you do not need to actively wait for a packet and you do not need to ping the server to keep a connection alive, because UDP is connectionless.

But I would recommend using a service if you want to receive packets when the app is in the background.
 
Upvote 0

JimAvanti

Member
Licensed User
Longtime User
MiniDemonic,
I installed your Demonicpush server on my computer and set the router to forward the UDP port 4444 to the computer running it. I installed your Android push (getnotif) service. I removed all the "Local" code so that it is always communicating to the server. It has no problem sending the phone's IP address to the server, but I still never get anything back to the phone (which is the same problem i am having with my app). I have a notification "Network Test" and it always says "waiting for server...
You have a "Send Text As Notification" button on the server software along with a text box. I am assuming if I type text in the box and press the button it should send the text back to the last received IP address from the phone. Is that a correct assumption?
Could my problem be incomming UDP ports are blocked on my Verizon phone? I could understand having issues with being connected through wifi and not having the ports forwarded on the router, but I never get data back to the phone even when driving using 3G.
My application used UDP port 8000 to send packets to the server and 8001 for receiving data back. When in the same house with the server I have UDP port 8000 forwarded to the server and UDP port 8001 forwarded to my phone's IP address.

Thanks,
Jim



You can refer to my custom push notifications tutorial here: http://www.b4x.com/forum/basic4andr...ustom-push-notifications-server-included.html

I do not program in vb.net so I can't help you with that part. But to receive UDP packets you can use the following code:
B4X:
Sub UDP_PacketArrived (Packet As UDPPacket)
    Dim msg As String
    msg = BytesToString(Packet.Data, Packet.Offset, Packet.Length, "UTF8")
    msgbox(msg, "message from server")
End Sub

The great thing with UDP is that you do not need to actively wait for a packet and you do not need to ping the server to keep a connection alive, because UDP is connectionless.

But I would recommend using a service if you want to receive packets when the app is in the background.
 
Upvote 0

JimAvanti

Member
Licensed User
Longtime User
I will work on some simple code tomorrow and post. Is there a place I could also upload a working .Net (4.0) UDP Server/Client test PC (Windows) software? I want the phone to work like the client software. The server and client software are actually both Server and client (The server listens for incoming UDP packets in a seperate processor thread and sends back results, while the client listens for results in the background while taking user commands and sending them to the server). They can both be run on the same computer for test purposes using IP address 127.0.1.1 or can run in two different locations.

Jim

Can you post your complete code?
 
Upvote 0

JimAvanti

Member
Licensed User
Longtime User
These links are my the Server/Client .net example applications for Windows. They require .net 4.0 to be installed. The client is an example of what I am trying to do on my Android. Both of these programs can be run on the same computer using your local IP (Usually 127.0.1.1). The UDP port can be changed on both programs. The Server receives packets on the selected port and returns status on the given port+1 (Default 8000 receive, 8001 send). The client sends on the given port and receives status on the given port+1). If you run the Server first you can toggle the buttons. When you run the client it should read the status of the servers buttons and match them. As you toggle buttons on either the server or client they should sync.
I can get the Android to control the server no problem, but I am having difficulties trying to receive information back to the Android.


http://dl.dropbox.com/u/76855843/UDP SERVER.exe
http://dl.dropbox.com/u/76855843/UDP Client.exe
 
Upvote 0

kanaida

Active Member
Licensed User
Longtime User
Hey just a note on UDP vs TCP. In your case TCP is probably the right choice.

The main difference is that UDP is "best effort", its faster but doesn't care if some packets got there or not. Usually used for stuff like video streaming where some image corruption doesn't matter, or in some games to reduce latency.
udp.jpg


TCP is a connection oriented protocol that keeps track of wether it's connected or not, if packets got there, etc... it's more overhead but guaranteed traffic.
tcp.jpg
 
Upvote 0

JimAvanti

Member
Licensed User
Longtime User
I'm seeing that UDP has problems tring to send status back to the phone even when a UDP server is running on both phone and PC using two differemt ports. When using Wifi hot spots where I wouldn't have access to port forwarding it would be impossible to receive on UDP. When I tried TCP I would get errors everytime the connection was lost due to ip change.

I am thinking of a coulple other ideas that might work.
MiniDemonic posted notification server which uses an HTTP client to receive the phones IP address:
HttpReq.InitializeGet("http://automation.whatismyip.com/n09230945.asp") hc.Execute(HttpReq, 1)
Since UDP seems to work good sending from the phone to the PC server, I was thinking I could have the server update a webpage with up-to-date status and I could poll the information from the phone similar to MiniDemonic's httpreq every 5 minitues or so, and after sending commands to verify they were received. I haven't looked into SQL servers yet, but that might also be a good way to try.

I guess if I knew more about TCP I could have a connection that closes without error when IP addresses change and then re-establishes connection. I would think that a constant TCP service connection would drain the phone's battery quicker than an occational httpreq though. Am I wrong in that assumption?

Any comments to help guide me to the best way to achive my goal will be appreciated!

Thanks,
Jim

Hey just a note on UDP vs TCP. In your case TCP is probably the right choice.

The main difference is that UDP is "best effort", its faster but doesn't care if some packets got there or not. Usually used for stuff like video streaming where some image corruption doesn't matter, or in some games to reduce latency.
 
Upvote 0

JimAvanti

Member
Licensed User
Longtime User
MiniDemonic,
I was able to get my phone to reteive status back from the server by using the HTTP request example you use to get your IP address. I have my sever update a web textfile every time a button changes and the phone uses a timer to poll the text file (and it also polls it after pressing a button for quicker response to changes done from the phone). It works good, except it is not the easiest way for someone to have to configure. I should eventually try TCP again to see if I can get it working better.

Thanks for your example.
Jim


You can refer to my custom push notifications tutorial here: http://www.b4x.com/forum/basic4andr...ustom-push-notifications-server-included.html

The great thing with UDP is that you do not need to actively wait for a packet and you do not need to ping the server to keep a connection alive, because UDP is connectionless.

But I would recommend using a service if you want to receive packets when the app is in the background.
 
Upvote 0
Top