Android Question Trying to connection

Cenny

Active Member
Licensed User
Longtime User
Hello,
I am trying to make a connection with a server.
I was given the address: abouttimetcp.flyingneurons.io
The port to use is: 40640
After connection I am supposed to send a JSON string.
The server would in return confirm reception.
But apparently there is something wrong as I cannot establish a connection.
Any help will be highly appreciated.

Starter:
#Region  Service Attributes
    #StartAtBoot: False
    #ExcludeFromLibrary: True
#End Region

Sub Process_Globals
    Public connected As Boolean
    Private client As Socket
    Public server As ServerSocket
    Private astreams As AsyncStreams
    Private const PORT As Int = 40640
    Private working As Boolean = True
End Sub

Sub Service_Create
    server.Initialize(PORT, "server")
    ListenForConnections
End Sub

Private Sub ListenForConnections
    Do While working
        server.Listen
        Wait For Server_NewConnection (Successful As Boolean, NewSocket As Socket)
        If Successful Then
            CloseExistingConnection
            client = NewSocket
            If astreams.IsInitialized Then astreams.Close
            astreams.Initialize( NewSocket.InputStream, NewSocket.OutputStream,"NewSocket")
            UpdateState(True)
        End If
    Loop
End Sub

Sub Service_Start (StartingIntent As Intent)

End Sub

Public Sub ConnectToServer(Host As String)
    Log("Trying to connect to: " & Host)
    CloseExistingConnection
    Dim client As Socket
    client.Initialize("client")
    client.Connect(Host, PORT, 10000)
    Wait For Client_Connected (Successful As Boolean)
    If Successful Then
        astreams.Initialize(client.InputStream,client.OutputStream, "astream")
        UpdateState (True)
    Else
        Log("Failed to connect: " & LastException)
    End If
End Sub

Public Sub Disconnect
    CloseExistingConnection
End Sub

Sub CloseExistingConnection
    If astreams.IsInitialized Then astreams.Close
    If client.IsInitialized Then client.Close
    UpdateState (False)
End Sub

Sub UpdateState (NewState As Boolean)
    connected = NewState
    CallSub(JSON, "SetState")
End Sub

Sub AStreams_Error
    UpdateState(False)
End Sub

Sub AStreams_Terminated
    UpdateState(False)
End Sub

Sub AStreams_NewData (Buffer() As Byte)
    Log("NewData")
    CallSub2(JSON, "NewData", Buffer)
End Sub

Public Sub SendData (data As String)
    Dim bc As ByteConverter
    Log("Send data")
    Log(bc.StringToBytes(data,"UTF-8"))
    
    If connected Then astreams.Write(bc.StringToBytes(data,"UTF-8"))
End Sub


'Return true to allow the OS default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

JSON:
Sub Process_Globals
    'Type MyMessage (Name As String, Age As Int, Image() As Byte)
    'Type MyMessage (JSon As String)
End Sub

Sub Globals
    Private edtIp As EditText
    Private lblMyIp As Label
    Private btnConnect As Button
    Private lblStatus As Label
    Private edtAge As EditText
    Private edtName As EditText
    Private btnSend As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("1")
    edtIp.ForceDoneButton = True
    edtAge.InputType = edtAge.INPUT_TYPE_NUMBERS
    edtAge.Text = 76
    edtIp.Text = "abouttimetcp.flyingneurons.io"
    edtName.Text = "Johnson"
End Sub

Sub Activity_Resume
    SetState
End Sub

Public Sub SetState
    btnSend.Enabled = Starter.connected
    If Starter.connected Then
        btnConnect.Text = "Disconnect"
        lblStatus.Text = "Connected"
    Else
        btnConnect.Text = "Connect"
        lblStatus.Text = "Disconnected"
    End If
    lblMyIp.Text = "My ip: " & Starter.server.GetMyWifiIP
End Sub

Sub btnSend_Click
    
    Dim JSONGenerator As JSONGenerator
            
    JSONGenerator.Initialize(Main.mJSON)
    MsgboxAsync(JSONGenerator.ToString & "\n","")
    CallSub2(Starter, "SendData",JSONGenerator.ToString)
End Sub

Sub btnConnect_Click
    If Starter.connected = False Then
        If edtIp.Text.Length = 0 Then
            ToastMessageShow("Please enter the server ip address.", True)
            Return
        Else
            CallSub2(Starter, "ConnectToServer", edtIp.Text)
        End If
    Else
        CallSub(Starter, "Disconnect")
    End If
End Sub

Public Sub NewData (buffer() As Byte)
    edtName.Text = BytesToString(buffer,0,buffer.Length,"UTF-8")
End Sub
 

aeric

Expert
Licensed User
Longtime User
Have you tried with OkHttpUtils2 library?

 
Upvote 0

Cenny

Active Member
Licensed User
Longtime User
Have you tried with OkHttpUtils2 library?

Thanks for your suggestion.
I tried something quick but no luck.
B4X:
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download("abouttimetcp.flyingneurons.io")
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Log(j.GetString)
    Else
        Log("Sorry")
    End If
    j.Release
    
End Sub
 
Upvote 0

Cenny

Active Member
Licensed User
Longtime User
So I tried this...

B4X:
            Dim JSONGenerator As JSONGenerator
            JSONGenerator.Initialize(Main.mJSON)
            
            Dim j As HttpJob
            j.Initialize("", Me)
            j.PostString("https://abouttimetcp.flyingneurons.io:40640",JSONGenerator.ToString & "\n")
            Wait For (j) JobDone(j As HttpJob)
            If j.Success Then
                Log(j.Response)
            End If
            j.Release

No reaction however.
What am I missing?
 
Upvote 0

sirjo66

Well-Known Member
Licensed User
Longtime User
I was given the address: abouttimetcp.flyingneurons.io
The port to use is: 40640
Are you sure that address and port number is correct ?
If I try to connect with Putty program, it doesn't work
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
No reaction however.
Use Log(job.ErrorMessage)

Confirm the port number.

What protocol to use? REST, RPC, TCP?

Any documentation or sample code e.g CURL?

B4X:
Sub PostApi (json As String)
    Dim j As HttpJob
    j.Initialize("", Me)
    j.PostString("https://abouttimetcp.flyingneurons.io:40640", json)
    ' j.GetRequest.SetHeader("Content-Type", "application/json") ' may be need additional header
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Log("Message: " & j.GetString)
    Else
        Log("Error: " & j.ErrorMessage)
    End If
    j.Release
End Sub
B4X:
Sub Activity_Create (FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    
    Dim m As Map = CreateMap("name": "Johnson", "age": 76, "ip": "192.168.0.1")
    Dim data As String = m.as(JSON).ToString ' Reference to Json library
    PostApi(data)
End Sub
 
Last edited:
Upvote 0

Cenny

Active Member
Licensed User
Longtime User
Use Log(job.ErrorMessage)

Confirm the port number.

What protocol to use? REST, RPC, TCP?

Any documentation or sample code e.g CURL?

B4X:
Sub PostApi (json As String)
    Dim j As HttpJob
    j.Initialize("", Me)
    j.PostString("https://abouttimetcp.flyingneurons.io:40640", json)
    ' j.GetRequest.SetHeader("Content-Type", "application/json") ' may be need additional header
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Log("Message: " & j.GetString)
    Else
        Log("Error: " & j.ErrorMessage)
    End If
    j.Release
End Sub
B4X:
Sub Activity_Create (FirstTime As Boolean)
    Activity.LoadLayout("Layout")
   
    Dim m As Map = CreateMap("name": "Johnson", "age": 76, "ip": "192.168.0.1")
    Dim data As String = m.as(JSON).ToString ' Reference to Json library
    PostApi(data)
End Sub
Same result.
I am coming to the conclusion that the address is wrong.
 
Upvote 0
Top