My app sends data to a server (also written in b4a) and I do this from 3-4 clients simultaneously.
Following a recommendation from Erel, I used AsyncStreams and for every client I have, I created a different Stream and Socket object at the server's side, by having an array of sockets (I am not sure if this was exactly what was proposed).
So, I have an array of 10 sockets, dimed as
and a global array
, indicating if the socket(L) is free or not. Then, when a connection is established, at the server's side, I use the following code, in order to choose a free socket for the new connection:
When I test it, sometimes it works well, sometimes not, thus making debugging hard. I suspect that the problem is that when I have a new connection established, at the very same moment with another connection established, the routine for giving the free socket ID is working for both at the same time, so that the routine doesn't have enough time to write
.
Then, I've tried stopping the connection routine, in order to avoid such conflict (if it really exists), by adding this line at the beginning of the connection routine:
I write socketProccess=true and then at the end of the routine, I set it to false, leaving the next connection to be established. It didn't work 
Also, I've tried to place a doEvents inside this small loop. Again nothing.
Any ideas on what can be wrong here, or perhaps if the method I use is not neccessary? I mean, I still don't know whether I really have to have multiple sockets at the server's side. Thank you.
Following a recommendation from Erel, I used AsyncStreams and for every client I have, I created a different Stream and Socket object at the server's side, by having an array of sockets (I am not sure if this was exactly what was proposed).
So, I have an array of 10 sockets, dimed as
B4X:
dim socket1(10) as socket
B4X:
Dim socketsStatus(10) As Int
B4X:
Sub Server_NewConnection (Successful As Boolean, NewSocket As Socket)
If Successful Then
Dim freeSocketId As Int
freeSocketId=-1
Dim k As Int
k=-1
Do While freeSocketId=-1 AND k<9
k=k+1
If socketsStatus(k)=0 Then
socketsStatus(k)=1:freeSocketId=k
socket1(freeSocketId)=NewSocket
fSocket=freeSocketId
End If
Loop
If freeSocketId=-1 Then
freeSocketId=0
socket1(freeSocketId)=NewSocket
fSocket=freeSocketId
End If
aStreams(freeSocketId).InitializePrefix(socket1(freeSocketId).InputStream, False, socket1(freeSocketId).OutputStream, "AStreams" & freeSocketId)
Else
ToastMessageShow(LastException.Message, True)
End If
server.Listen
End Sub
When I test it, sometimes it works well, sometimes not, thus making debugging hard. I suspect that the problem is that when I have a new connection established, at the very same moment with another connection established, the routine for giving the free socket ID is working for both at the same time, so that the routine doesn't have enough time to write
B4X:
socketsStatus(k)=1:freeSocketId=k
Then, I've tried stopping the connection routine, in order to avoid such conflict (if it really exists), by adding this line at the beginning of the connection routine:
B4X:
Do While socketProccess=True:Log("I wait"):Loop
socketProccess=True
Also, I've tried to place a doEvents inside this small loop. Again nothing.
Any ideas on what can be wrong here, or perhaps if the method I use is not neccessary? I mean, I still don't know whether I really have to have multiple sockets at the server's side. Thank you.