Android Question How to send String using HTTP and get return string without timeout error?

JohnC

Expert
Licensed User
Longtime User
I am communicating with an IoT device in which I open a TCP connection, send some text, and a second later, get a string response from the IoT device.

I have VB6 code that works fine:

B4X:
Private Sub cmdSend_Click()

    sock.Protocol = sckTCPProtocol
    sock.RemoteHost = "192.168.1.170"
    sock.RemotePort = 11000
    sock.Connect

End Sub

Private Sub sock_ConnectionRequest(ByVal requestID As Long)
    Debug.Print "Sock Conn Request: " & CStr(requestID)
    sock.Accept
End Sub

Private Sub sock_Connect()
    Debug.Print "Sock Connect...Sending Command..."
    sock.SendData ("sendir,1:3,1,38000,1,69,342,171,21,63,21,21,21,63,21,21,21,21,21,21,21,21,21,63,21,21,21,21,21,21,21,21,21,63,21,63,21,21,21,21,21,21,21,63,21,63,21,21,21,63,21,63,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,63,21,63,21,1753,342,85,21,3645" & Chr(13))
End Sub

Private Sub sock_SendComplete()
    Debug.Print "Send Complete"
End Sub

Private Sub sock_DataArrival(ByVal bytesTotal As Long)
    Dim T As String
  
    Call sock.GetData(T, , bytesTotal)
    Debug.Print "Sock_DataArrival: " & T
    sock.Close
End Sub

Private Sub sock_Close()
    Debug.Print "Sock_Close"
End Sub

Private Sub sock_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
    Debug.Print "Sock_Error: " & Description
End Sub

Which results in this immediate window (debug) output:
B4X:
Sock Connect...Sending Command...
Send Complete
Sock_DataArrival: completeir,1:3,1

As you can see the IoT device returns the string of "completeir,1:3,1"

So, I am now trying to implement this in B4A with this code:

B4X:
Sub cmdTest_Click
    Dim j As HttpJob
    Dim Cmd As String
  
    j.Initialize("",Me)
    Cmd = "sendir,1:3,1,38000,1,69,342,171,21,63,21,21,21,63,21,21,21,21,21,21,21,21,21,63,21,21,21,21,21,21,21,21,21,63,21,63,21,21,21,21,21,21,21,63,21,63,21,21,21,63,21,63,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,63,21,63,21,1753,342,85,21,3645" & Chr(13)
  
    Log("Sending IR.....")
    j.PostString("192.168.1.170:11000", Cmd)
      
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Log("Job_OK: " & j.GetString)
    Else
        Log("Job_Error: " & j.ErrorMessage)
    End If
    j.Release
  
End Sub

But when I run this code, it sends the command to the IoT properly, but then there is a 30 second delay and then I get a timeout error:
B4X:
** Activity (main) Resume **
Sending IR.....
*** Service (httputils2service) Create ***
** Service (httputils2service) Start **
ResponseError. Reason: java.net.SocketTimeoutException, Response:
Job_Error: java.net.SocketTimeoutException

My guess is that when the IoT device returns a string, it's just a simple string and not in an HTTP response format (that would include a proper status code like "200"), which the HTTPJob is waiting for. And when such a code never arrives, it eventually times out and throws an error.

So, how can I receive the IoT text response right away without a timeout error?
 

Sandman

Expert
Licensed User
Longtime User
It seems to me that you're doing two completely different things in VB6 and B4X. In the first, you're opening a connection to an ip address and sending some data. In the latter, you're doing a real http request. Meaning the B4X code also is sending http request headers and stuff like that. So that request is completely wrong. You need to just open a connection in B4X and then it should work just fine.

Here's the socket documentation:
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
That's what I was thinking too, but I didn't know how to "Just open a connection" in B4A, but maybe someone does.
 
Upvote 0

Sandman

Expert
Licensed User
Longtime User
I didn't know how to "Just open a connection" in B4A, but maybe someone does.
It's actually fairly simple, check out the documentation I linked and if that's not enough, I'm sure you could find an example using the search.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Start with this Tutorial. It should give you a start.

 
Last edited:
Upvote 0

JohnC

Expert
Licensed User
Longtime User
Ah, I forgot about the Socket object.

It looks like there is no event triggered when it receives data, so I need to determine the max length of time the device needs to respond.
 
Upvote 0
Top