B4J Question Building tcpserver using Asyncstreamtext

Addo

Well-Known Member
Licensed User
i am starting my road to write a tcp server in b4j
this is the starting server code


B4X:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Dim Server As ServerSocket
    Dim AStreams As AsyncStreamsText
    Dim Socket1 As Socket


End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    Server.Initialize(5251, "Server")
    Server.Listen
    MainForm.Show
End Sub


Sub Server_NewConnection (Successful As Boolean, NewSocket As Socket)
    If Successful Then
        Log("Connected")
        Socket1 = NewSocket
        AStreams.Initialize(Me, "AStreams", Socket1.InputStream, Socket1.OutputStream)
    Else
        Log(LastException.Message)
    End If
    Server.Listen
End Sub


Public Sub NewData (data As String)
Log(data)
End Sub


Sub AStreams_NewText(Text As String)
    NewData(Text)
End Sub

Public Sub SendData (data As String)

AStreams.Write(data)

End Sub


Sub AStreams_Error
    Log(LastException.Message)
    Log("AStreams_Error")
End Sub

Sub AStreams_Terminated
    Log("AStreams_Terminated")
End Sub



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


i have a question how can i set a client class for each connected client ?

like set username date connected client ip etc.. for each client that connected to server ?
 

Addo

Well-Known Member
Licensed User
by following cctv Tutorial i come up with the following sample

B4X:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Dim Server As ServerSocket
    Type ClientConn (Clip As String, Clname As String, connected As String)
    Dim connections As Map
    Dim slots As List


End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    slots.Initialize
    connections.Initialize
    Server.Initialize(5251, "Server")
    Server.Listen
    MainForm.Show
End Sub


Sub Server_NewConnection (Successful As Boolean, NewSocket As Socket)
    If Successful Then
        Dim AStreams As AsyncStreamsText
        Dim newclient As ClientConn
        Log("Connected")
        AStreams.Initialize(Me, "AStreams", NewSocket.InputStream, NewSocket.OutputStream)
        newclient.Clip = NewSocket.RemoteAddress
        newclient.connected = DateTime.Time(DateTime.Now)
        connections.Put(AStreams, newclient)

    Else
        Log(LastException.Message)
    End If
    Server.Listen
End Sub


Public Sub NewData (data As String)
Log(data)
End Sub


Sub AStreams_NewText(Text As String)
    NewData(Text)
End Sub

Public Sub SendData (data As String)
Dim astreams As AsyncStreamsText = Sender

astreams.Write(data)

End Sub


Sub AStreams_Error
    Log(LastException.Message)
    Log("AStreams_Error")
End Sub

Sub AStreams_Terminated
    Dim astream As AsyncStreamsText = Sender
    Dim newclient As ClientConn = connections.Get(astream)
    Log(newclient.connected)
    ReleaseSlot(astream)
    astream.Close
    Log("AStreams_Terminated")
End Sub


Private Sub ReleaseSlot(astream As AsyncStreamsText)
    If connections.ContainsKey(astream) Then
        connections.Remove(astream)
    End If
End Sub



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

i am planing to accsess some databse my question now is it safe to access database each request or i have to synchronize ?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You haven't provided enough information. My guess is that you are doing a mistake not to use jServer and build a real server.

Two examples of servers built with AsyncStreams:

FTP: https://www.b4x.com/android/forum/t...d-with-socket-and-asyncstreams.74320/#content

MJPEG: https://www.b4x.com/android/forum/threads/73792/#content

i am planing to accsess some databse my question now is it safe to access database each request or i have to synchronize ?
You don't need to synchronize as your code runs on the main thread.
 
Upvote 0
Top