B4J Question how to implement a server inside a b4j form program?

omarruben

Active Member
Licensed User
Longtime User
Hi, I see that we can create web server with b4j but only non UI, what would be the way to give the desktop application web server or communication capabilities ?

on the example at : https://www.b4x.com/android/forum/t...ol-the-desktop-with-your-phone.60887/#content

the program listens UDP,TCP,MQTT... why is not possible to use http? at least to receive commands not to server pages , from a web browser (to avoid create and install a additional app in Android or IOS)
 
Last edited:
Solution
i know is mediocre, but works!!! thanks again everybody :
i leave this as a reference for the future

B4X:
Sub AStream_NewData (Buffer() As Byte)
    
    Dim txt As String = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
    
    Dim components() As String
    components = Regex.Split("\r\n",txt) 
    
    If components(0) = "GET /test HTTP/1.1" Then
        Log("test")
    End If
       
    If components(0) = "GET /autodj HTTP/1.1" Then
        Log("autodj")
        Button8_Click
    End If
    
    Dim html(6) As String
    html(5) = "<html><body><h1>Hello, World!</h1></body></html>" & CRLF
    html(0) = "HTTP/1.1 200 OK" & CRLF
    html(1) = "Content-Type: text/html; charset=utf-8" & CRLF
    html(2) = "Content-Length: " &...

Erel

B4X founder
Staff member
Licensed User
Longtime User
Technically you can implement a simple http server. There are several examples in the forum.
This is a good solution if you want to waste your time and create a mediocre server.

A better solution is to separate the server and the UI app and if it sounds difficult then it is still 1000 times simpler than creating a reliable web server.
 
Upvote 0

omarruben

Active Member
Licensed User
Longtime User
thank you for your answer, I just want to give the app the capability to receive a few commands (not a full server)
following a sample using sockets I have achieve what I want but still can't send a html response:
B4X:
Sub AStream_NewData (Buffer() As Byte)
    Dim txt As String = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
    Dim components() As String
    components = Regex.Split("\r\n",txt)
    If components(0) = "GET /play HTTP/1.1" Then
        Log("play")
    End If
    If components(0) = "GET /autodj HTTP/1.1" Then
        Log("autodj")
        Button8_Click ' plays all music
    End If
    ' send answer (it does not show anything on the browser...why?)'
    Dim  dd As String =$"HTTP/1.1 200 OK
    Content-Type: text/html; charset=utf-8
    Content-Length: 48
    Connection: Closed   
    <html><body><h1>Hello, World!</h1></body></html>"$
    astream.Write(dd.GetBytes("UTF8")) ' <---- whows nothing on the browser
    astream.SendAllAndClose
End Sub
 
Upvote 0

omarruben

Active Member
Licensed User
Longtime User
i know is mediocre, but works!!! thanks again everybody :
i leave this as a reference for the future

B4X:
Sub AStream_NewData (Buffer() As Byte)
    
    Dim txt As String = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
    
    Dim components() As String
    components = Regex.Split("\r\n",txt) 
    
    If components(0) = "GET /test HTTP/1.1" Then
        Log("test")
    End If
       
    If components(0) = "GET /autodj HTTP/1.1" Then
        Log("autodj")
        Button8_Click
    End If
    
    Dim html(6) As String
    html(5) = "<html><body><h1>Hello, World!</h1></body></html>" & CRLF
    html(0) = "HTTP/1.1 200 OK" & CRLF
    html(1) = "Content-Type: text/html; charset=utf-8" & CRLF
    html(2) = "Content-Length: " & html(5).Length & CRLF
    html(3) = "Connection: Closed " & CRLF
    html(4) = CRLF

    Dim  dd As String = html(0) & html(1) & html(2) & html(3) & html(4) & html(5)
    astream.Write(dd.GetBytes("UTF8"))
    astream.SendAllAndClose
    
    
End Sub
 
Upvote 0
Solution
Cookies are required to use this site. You must accept them to continue using the site. Learn more…