Android Question Server / Client using Http Server Library

Lello1964

Well-Known Member
Licensed User
Longtime User
I'm building a client / server using Http Server Library

I have two apps, one client :

App Client:
#Region  Project Attributes
    #ApplicationLabel: Test Client HTTP
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    Dim PaginaHttp As String ="http://192.168.1.161:5555/"
End Sub

Sub Globals
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
    Dim j As HttpJob
    j.Initialize("", Me)
 
    Dim txt As String = "Test"
 
    j.PostString(PaginaHttp,txt)
    j.GetRequest.SetContentType("application/x-www-form-urlencoded")
 
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Dim tmp As String =j.GetString
        Msgbox(tmp,"Risposta")
        Log(tmp)
    End If
    j.Release
End Sub

and a server :

Main Server:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
End Sub

Sub Globals
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    StartService(ServerHttp)
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
    CallSub2(ServerHttp,"RispostaHttp","Fatto")
End Sub

Service ServerHttp:
#Region  Service Attributes
    #StartAtBoot: False
 
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private server As HttpServer
    Public port As Int = 5555
    Public HttpDevice  As String
End Sub

Sub Service_Create
    server.Initialize("Server")
    server.Start(port)
    Dim n As Notification
    n.Initialize
    n.Icon = "icon"
    n.SetInfo("Http Server Attivo", "", Main)
    Service.StartForeground(1, n)
    Messages.Initialize
    HttpDevice=""
    LogColor("Server Http Avviato",Colors.Red)
End Sub

Sub Service_Start (StartingIntent As Intent)
    Service.StopAutomaticForeground 'Call this when the background task completes (if there is one)
End Sub

Sub Server_HandleRequest (Request As ServletRequest, Response As ServletResponse)
    Try
        If Request.Method = "POST" Then 'User pressed on the Send button
            Log("Incasso HTTP in corso ")

            wait for RispostaHttp (Risposta As String)

            Response.SendString(Risposta)
            Log("Risposta" & Risposta)
        Else
            Log("Non è un POST")
        End If
    Catch
        Response.Status = 500
        Log("Error serving request: " & LastException)
        Response.SendString("Errore server " & LastException )
    End Try
End Sub

Sub Service_Destroy
    server.Stop
    Service.StopForeground(1)
End Sub

When i click Button1, JobDone return empty string without wait RispostaHttp callsub.
 

Lello1964

Well-Known Member
Licensed User
Longtime User
Add Project Server and Client
 

Attachments

  • ClientTest.zip
    3.2 KB · Views: 205
  • ServerTest.zip
    4.1 KB · Views: 193
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
You can't use wait for inside HandleRequest, because as soon as it rises to the top level waiting for the event it closes the connection.
 
Upvote 0

Lello1964

Well-Known Member
Licensed User
Longtime User
B4X:
Sub Server_HandleRequest (Request As ServletRequest, Response As ServletResponse)
    Try
        If Request.Method = "POST" Then 'User pressed on the Send button
            Log("Incasso HTTP in corso ")
            FlgAttesa = True
            Risposta  = ""
            
            Do While FlgAttesa
                DoEvents
            Loop
            
            Response.SendString(Risposta)
            Log("Risposta" & Risposta)
        Else
            Log("Non è un POST")
        End If
    Catch
        Response.Status = 500
        Log("Error serving request: " & LastException)
        Response.SendString("Errore server " & LastException )
    End Try
End Sub

This is the only solution i've found, but DoEvents is deprecated, hope @Erel tell me a solution.
 
Upvote 0

Lello1964

Well-Known Member
Licensed User
Longtime User
Unfortunately I am forced to use HTTPS because my client only uses HTTPS protocol.

Can you help me?
 
Upvote 0

avalle

Active Member
Licensed User
Longtime User
Usually mobile operators do not offer a direct IP address but have NAT addresses. This means that the public IP address of the server is in reality the IP address of the NAT proxy. So if you run the server on the Android phone and you have clients that are external to your LAN, then these clients will not be able to reach the server at their public IP address.

For such a case you should use a publish/subscribe protocol like MQTT, as Erel suggested.
 
Upvote 0

Lello1964

Well-Known Member
Licensed User
Longtime User
Usually mobile operators do not offer a direct IP address but have NAT addresses. This means that the public IP address of the server is in reality the IP address of the NAT proxy. So if you run the server on the Android phone and you have clients that are external to your LAN, then these clients will not be able to reach the server at their public IP address.

For such a case you should use a publish/subscribe protocol like MQTT, as Erel suggested.


today my sim tim m2m has been activated, it has a dynamic ip, but it works fine with dyndns.

I can connect to my router via Tcp-ip using Sim 4G connection.
 
Upvote 0

avalle

Active Member
Licensed User
Longtime User
That may be specific to M2M data-only offer, while normally phone subscriptions (voice+data) provide IP services behind NAT to preserve IPV4 addresses which are running short. A public IP on mobile subscription is generally seen as a "luxury" that the carrier charges for M2M. Most phone users don't really care having a public IP and would not be open to be charged for its extra cost.
 
Upvote 0
Top