Sub Server_NewConnection (Successful As Boolean, NewSocket As Socket)
If Successful Then
Dim freeSocketId As Int, freeStreamsId As Int
freeSocketId=-1:freeStreamsId=-1
Dim k As Int
k=-1
Do While freeSocketId=-1 AND k<99
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
Log(DateTime.Time(DateTime.Now) & " - " & "new connection - socket:" & freeSocketId)
Dim k2 As Int
k2=-1
Do While freeStreamsId=-1 AND k2<99
k2=k2+1
If streamsStatus(k2)=0 Then
streamsStatus(k2)=1:freeStreamsId=k2
fStreams=freeStreamsId
End If
Loop
If freeStreamsId=-1 Then
freeStreamsId=0
End If
aStreams(freeStreamsId).InitializePrefix(socket1(freeSocketId).InputStream, False, socket1(freeSocketId).OutputStream, "AStreams" & freeStreamsId)
Log("init astreams" & freeStreamsId)
Else
ToastMessageShow(LastException.Message, True)
End If
server.Listen
End Sub
Where is the code that closes the socket?
Sub freeThisSocket(whichSocket As Int,whichStream as Int)
'log("freeThisSocket("&whichSocket&") called")
'If aStreams(whichSocket).IsInitialized Then aStreams(whichSocket).Close
aStreams(whichStream).Close
'If socket1(whichSocket).IsInitialized Then socket1(whichSocket).Close
socket1(whichSocket).Close
socketsStatus(whichSocket)=0
Log("socket " & whichSocket & " closed")
End Sub
Not exactly. The client is sending a 'closeSocket' message to the server. Then we're freeing the socket at the server's side. At the clients side, after we get the final data, we truly close the socket. This happens in a service module which is recreated everytime we set a new connection.My guess is that the other side sends "close" and then closes the connection. The connection is actually closed before you get to handle this message. You can "free" the socket / astream after 100ms (or try with CallSubDelayed).
correspondingStreamToSocket(freeSocketId)=freeStreamsId
Sub freeThisSocket(whichSocket As Int)
aStreams(correspondingStreamToSocket(whichSocket)).Close
socket1(whichSocket).Close
socketsStatus(whichSocket)=0
Log("socket " & whichSocket & " closed")
End Sub
Sub aStreamsTerminated(currentStream As Int)
Log("astreams" & currentStream & "terminated")
streamsStatus(currentSocket)=0
End Sub
LogCat connected to: c08903280035ae6e
--------- beginning of /dev/log/main
--------- beginning of /dev/log/system
** Service (serverlistener) Create **
** Service (serverlistener) Start **
13:31:44 - new connection - socket:0
init astreams0
aStreams0_newData
13:31:44 - test02 READDBCHANGES 1 1 1
aStreams0_newData
13:31:44 - test02 end
send2client=dbChanges 1 1 1
send2client=end
aStreams0_newData
13:31:44 - test02 CLOSESOCKET
aStreams0_newData
13:31:44 - test02 end
socket 0 closed
astreams0terminated
13:31:50 - new connection - socket:0
init astreams0
aStreams0_newData
13:31:50 - test02 READSHIFTS 1
aStreams0_newData
13:31:50 - test02 end
send2client=shiftExists 4
send2client=end
aStreams0_newData
13:31:50 - test02 CLOSESOCKET
aStreams0_newData
13:31:50 - test02 end
socket 0 closed
13:31:50 - new connection - socket:0
init astreams1
astreams0terminated
aStreams1_newData
13:31:51 - test02 READTABLES 1000
aStreams1_newData
13:31:51 - test02 end
server tables=error
send2client=error
send2client=end
aStreams1_newData
13:31:51 - test02 CLOSESOCKET
aStreams1_newData
13:31:51 - test02 end
socket 1 closed
astreams1terminated
13:32:06 - new connection - socket:1
init astreams0
aStreams0_newData
13:32:06 - test02 READTABLES 1001
aStreams0_newData
13:32:06 - test02 end
server tables=10004 17 17 0 0
send2client=10004 17 17 0 0
send2client=end
aStreams0_newData
13:32:06 - test02 CLOSESOCKET
aStreams0_newData
13:32:06 - test02 end
socket 0 closed
What is exactly the problem? Do you need to ignore an AsyncStream_Terminated event?
You should not reuse any socket or asyncstream object. You should instead create a new object (by calling Dim again) and use new objects.
Cannot redim the objects cause I have an array.You should instead create a new object (by calling Dim again) and use new objects.
dim aStreams(myIndex) as aSyncStreams
Sure and thanks again, I will give this a try too.Just for the record, you can create a new object with:
B4X:Dim a As AsyncStream aStreams(x) = a
socket1(freeSocketId)=NewSocket
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?