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 ?
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