B4J Question doubt on ServerSockect and multiple access

marcick

Well-Known Member
Licensed User
Longtime User
I have doubts my code is correct about ServerSocket when multiple connections can occour in the same time.
I think the string SerBuffer is the same for all the connections and I should use a separate buffer for each connection. I'm a bit confused, anybody can help me ?
How can I declare a separate buffer for each connection ?

B4X:
Sub Class_Globals
    Public Bls1ServerSocket As ServerSocket
    Private SerBuffer, Bls1String As String
    Private Bls1PORT As Int
    Private working As Boolean = True
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
    Conf=File.ReadMap(File.DirApp, "config.properties")
    Bls1PORT=Conf.Get("Bls1Port")
    Bls1ServerSocket.Initialize(Bls1PORT, "server")
    ListenForBls1Connections
    Log("Bls1Connector is listening on port: " & Bls1PORT)
End Sub

Private Sub ListenForBls1Connections
    Do While working
        Bls1ServerSocket.Listen
        Wait For Server_NewConnection (Successful As Boolean, NewSocket As Socket)   
        If Successful Then   
            'Log("New connection from " & NewSocket.RemoteAddress)
            Dim astream As AsyncStreams
            astream.Initialize(NewSocket.InputStream,NewSocket.OutputStream,"astream")
            'NewSocket.TimeOut=15000
        End If
    Loop
End Sub

Sub AStream_NewData (Buffer() As Byte)
    Dim astream As AsyncStreams = Sender
    Dim bc As Bls1Client=Main.Bls1ClientMap.Get(astream)
    bc.Time=DateTime.now
    Main.Bls1ClientMap.Put(astream, bc)
    SerBuffer=SerBuffer & BytesToString(Buffer, 0, Buffer.Length, "utf-8")
    If SerBuffer.IndexOf(Chr(13))=-1 Then Return
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
I'll have a look, but at this time I don't want to start from scratch, I would prefer to tune the actual project.
So I'm right that the SerBuffer variable is shared between all the connections ?
Or do I have a separate SerBuffer for each connection that is created ?
 
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
Ok, I think I have the answer, The Serbuffer is shared between all the connection and this can bring to unexpected behaviours. mad:)
So I have corrected the code using the Map, where each connection has its own buffer
Thank you, I'll have a look to jServer then

B4X:
Sub AStream_NewData (Buffer() As Byte)
    Dim astream As AsyncStreams = Sender
    Dim bc As Bls1Client=Main.Bls1ClientMap.Get(astream)
    bc.Time=DateTime.now
    bc.SerBuff=bc.SerBuffer & BytesToString(Buffer, 0, Buffer.Length, "utf-8")
    Main.Bls1ClientMap.Put(astream, bc)
    If bc.SerBuffer.IndexOf(Chr(13))=-1 Then Return
 
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
The code in the first post is a class module, but I have one single instance for all the client, right ?
How should be modified to have a new instance for each client ?
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…