I have a socket class, and when each B4A connection requested is accepted I create a class instance for that and add it to my connections list in the main app thread. I can see the threads being created using windows resource manager. Maybe a diagram would help explain it ???
WE NEED MORE DIAGRAMS (in absolute, not relative to this thread)
No.AsyncStreams are used for socket comms, are you suggesting AsyncStreams between the Class's and the main thread to add to the list ?
Thanks ErelNo.
The threads that you see in task manager are related to AsyncStreams. The events are running in the main thread, so you don't need to worry about any threading issues.
No.
The threads that you see in task manager are related to AsyncStreams. The events are running in the main thread, so you don't need to worry about any threading issues.
You need to use AsyncStreams for that. The communication will happen with the help of two background threads.
' // client socket connection
Type mSocketConnection(index As Long, _
inUse As Boolean, _
public_id As String, _
buffer_in As List, _
buffer_out As List, _
messages_in As List, _
sck As class_socket, _
profile As mUserProfile, _
RT As mSocketConnectionRT _
)
Private Sub srvListener_NewConnection(Successful As Boolean, NewSocket As Socket)
Dim x As Long
Dim ExistingClient As mSocketConnection
Dim NewClient As mSocketConnection
Dim freeSocket As Long
'srvListener.Close
Log(DateTime.Time(DateTime.Now) & " - srvListener_NewConnection (" & Successful & ") " & NewSocket.RemoteAddress)
If Successful Then
freeSocket = -1
For x = 0 To connections.Size - 1
ExistingClient = connections.get(x)
If Not(ExistingClient.InUse) Then
freeSocket = x
x = (connections.Size+1)
End If
Next
If freeSocket = -1 Then
' // add to the list
connections.Add(NewClient)
' // pointer to new list entry
freeSocket = (connections.Size-1)
Log(DateTime.Time(DateTime.Now) & " - srvListener_NewConnection (NEW) " & freeSocket)
' // initialise client connection
NewClient.Initialize
NewClient.sck.Initialize(Me, "Client", freeSocket)
' // initialise the user profile
NewClient.profile.Initialize
NewClient.profile.RESPONDER.Initialize
NewClient.profile.RESPONDER.Initialize
NewClient.profile.HW_SWITCH.Initialize
NewClient.profile.HW_LOC.Initialize
NewClient.profile.TEST_PROC.Initialize
' //assign it
ExistingClient = NewClient
Else
Log(DateTime.Time(DateTime.Now) & " - srvListener_NewConnection (EXISTING) " & freeSocket)
End If
' // flag this as being used
ExistingClient.inUse = True
' // this client object is either a new/ or recycled one
ExistingClient.sck.NewConnection(NewSocket)
Else
Log("Connection attempt failed: " & LastException.Message)
End If
srvListener.Listen
End Sub
You can use the Threading library to run it on another thread. However it will only cause problems.
This code will be executed once per each new connection. Unless I'm missing something it will happen very fast and will not block the main thread.
If you wish to implement a high performance server then you need to use the server library instead of handling the sockets yourself. It will be simpler and will perform better.
Yes, jServer. However I don't see that you are using it. jServer works with servlet handlers.
The server threads are explained here: http://www.b4x.com/android/forum/threads/server-building-web-servers-with-b4j.37172/#content
However with jServer you are implementing a HTTP server not a custom server.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?