B4J Question is my b4j tcp server is a Threadsafe ?

Addo

Well-Known Member
Licensed User
Longtime User
Server Code

B4X:
Sub Class_Globals
    
    Private server As ServerSocket
    Private clients As List
    Public cuniqueid As Int
    Private ClientTimer As Timer
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(port As Int)
Log("Server Initialized")

cuniqueid = 10
server.Initialize(port, "server")
clients.Initialize
ClientTimer.Initialize("ConnectionCheck",2000)
ClientTimer.Enabled = True

server.Listen
    
End Sub


Private Sub ConnectionCheck_tick

If clients.Size > 0 Then

For i =  clients.Size -1 To 0 Step -1
    
Dim aClient As TcClient
Dim ctimenow As Long
Dim afloortime As Double
aClient = clients.Get(i)
ctimenow = DateTime.Now
afloortime = Floor((ctimenow - aClient.iamlive) / DateTime.TicksPerSecond)

If (afloortime >= 75) And (aClient.iamconnected = "YES")  Then
aClient.iamconnected = "NO"
Log("Remove Dead Connection "&aClient.userid)
aClient.diconnectme
Exit
End If


Next
    
    
        
End If

End Sub

Private Sub Server_NewConnection (Successful As Boolean, NewSocket As Socket)
If Successful Then
Dim c As TcClient
c.Initialize(NewSocket, Me)
c.userip = NewSocket.RemoteAddress
c.iamlive = DateTime.Now
c.iamconnected = "YES"
c.userid = cuniqueid
cuniqueid = cuniqueid + 1
Log("New Connection "&c.userip& " id "&cuniqueid)
clients.Add(c)
End If

server.Listen


End Sub


Public Sub getMyIp As String
Return server.GetMyIP
End Sub

Public Sub getNumberOfClients As Int
Return clients.Size
End Sub


Public Sub ClientDisconnected (aclient As TcClient)
Dim i As Int = clients.IndexOf(aclient)
    
If i > -1 Then
aclient.iamconnected = "NO"
clients.RemoveAt(i)
End If

End Sub


Public Sub SendTome(aclient As TcClient, data As String)
    
aclient.SendData(data)

End Sub


Public Sub SendCommandToAll(data As String)
If clients.Size > 0 Then
    
For i =  clients.Size -1 To 0 Step -1

Dim aClient As TcClient
aClient = clients.Get(i)

If (aClient.iamconnected = "YES") Then
aClient.SendData(data)
End If

Next
            
End If
    

End Sub



B4X:
Sub Class_Globals

Private astream As AsyncStreamsText
Private Aserver As Tcserver
Dim userid As String
Dim iamlive As Long
Dim iamconnected As String
Dim ConnectionTime As String


    
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(socket As Socket, Serv As Tcserver)
Log("New connection")
Aserver = Serv
astream.Initialize(Me, "AStream", socket.InputStream, socket.OutputStream)   
    
End Sub

Public Sub SendData(data As String)
    astream.Write(data)
End Sub


Public Sub NewData (data As String)
Dim paramset() As String
Dim paramcount As Int   
Dim Command As String

paramset = Regex.Split("\|", data)
paramcount = paramset.Length

If paramcount > 0 Then

Command = paramset(0)

End If

If Command = "isalive" Then
updatelivetime
End If

If Command = "close" Then
CallSub(Main, "CloseTheserver")
End If

If Command = "Login" Then

If paramcount = 2 Then
ConnectionTime = DateTime.Time(DateTime.Now)

End If

End If

If Command = "msg" Then
    
If paramcount = 2 Then
Aserver.SendCommandToAll("msg|")   
End If


End If


    


End Sub

Private Sub updatelivetime
iamlive = DateTime.Now
End Sub


Public Sub AStream_NewText(Text As String)
NewData(Text)
End Sub


Private Sub AStream_Terminated
AStream_Error
End Sub

Private Sub AStream_Error

Aserver.ClientDisconnected(Me)

astream.Close
    
    
End Sub


Public Sub diconnectme
AStream_Error
End Sub


i am doing fine ? or i should be careful more ?
 

Addo

Well-Known Member
Licensed User
Longtime User
i know that you mentioned many times and advised to build a server based on jserver

The thing is right now i am trying to migrate a previous Socket server that i made with delphi

i had to use a socket server at this moment until i fully migrating my previous apps that built with other ide used a socket server

i see an outstanding performance with b4j for handling Large Clients using EOL Sockets Compared to my previous background

sense i will still use other ide to built a a socket client my server with b4j should be A socket server
 
Upvote 0

Addo

Well-Known Member
Licensed User
Longtime User
Addition info

should i use CallSubDelayed if i am going to send multi able requests to a client ?


as example

B4X:
Public Sub NewData (data As String)
Dim paramset() As String
Dim paramcount As Int 
Dim Command As String

paramset = Regex.Split("\|", data)
paramcount = paramset.Length

If paramcount > 0 Then

Command = paramset(0)

End If

If Command = "isalive" Then
updatelivetime
End If

If Command = "close" Then
CallSub(Main, "CloseTheserver")
End If

If Command = "Login" Then

If paramcount = 2 Then
ConnectionTime = DateTime.Time(DateTime.Now)
Aserver.SendTome(Me, "logged")
End If

End If

If Command = "msg" Then
  
If paramcount = 2 Then
Aserver.SendCommandToAll("msg|") 
End If


End If

If Command = "PING" Then
  
If paramcount = 2 Then
Aserver.SendCommandToAll("PONG|")
End If


End If


If Command = "SET" Then
  
If paramcount = 2 Then
Aserver.SendCommandToAll("SETDATA|")
End If


End If

' if iam going to use SendCommandToAll  that way should i use CallSubDelayed ?


End Sub


if i am going receive and send multiable data that way should i use CallSubDelayed to Call SendCommandToall(data) ?

also i use two subs to send data Aserver.SendTome(Me, "Data") and Aserver.SendCommandToall(data) is it safe that way ?
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…