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
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