Hello,
I am facing two issues, mainly I suppose due to lack of knowledge by my side, so I would like your opinion/help on this:
The first issue has to do with sockets and asyncstreams. Whenever I want a communication to begin, I open a socket, then a asyncstream. That's ok, and it works great.
I'm closing the stream and the socket in activity_pause event, so
question 1: what happens with the corresponding objects at the server's side? From what I saw with logging, socket is closing, but the stream somehow seems to remain open, until the service module I use gets destroyed.
question 2: I remember in the beginning of my coding with netLib, I tried to close sockets and streams, whenever I thought that their job was done. Somehow, my job was not really done, thus closing the socket, didn't leave time for the streams to finish their operation (at least this is what I imagine). This is also the reason I placed the socket.close inside the activity_pause. My problem with that, is what will happen if my app crashes? I mean, would it pass from 'pause' or it would leave a socket open?
question 3: after taking Erel's precious advice, I came up to create a new socket every time I had a new connection with the server. At the same time, I had a new astream object. That's ok, but I had to do it like this:
This is my way to find a free-open socket, yet what puzzles me is more the new astream. The way I do it, I have to write code for every single new stream I want. I did this to an extent of 10 streams, but what if I need 100 different streams (100 connected devices)? Should I stick with writing that many astreams_newData, astreams_error? I believe I am missing something here, I would certainly prefer a dynamic array for this.
question 4: Occasionally, server loses its network connectivity (that's its own problem with wifi). What I noticed is that if I have an open socket with a stream, client is not getting a astreams_error, thus my app on the client side hangs. Sometimes, if server connects fast after its disconnection, streams continue, and my client continues to perform ok. It's just weird to me.
And a final question 5, irrelevant with networking:
Occasionally I experience wrong variable values when passing from an activity to another. These variables are dimmed in process_globals in my main activity, thus, I was hoping they would not get destroyed. I've read about activities' life cycle, still, I don't understand why a process_global variable, would lose its value. I know I can hold its value in a file, that's ok, but still this thing puzzles me, especially when I see an app crash because of a re-initialized variable (some of them take a 0-value time to time), while I know that this activity is in action almost all the time.
Any thoughts on these, would be really appreciated, thank you for your time.
I am facing two issues, mainly I suppose due to lack of knowledge by my side, so I would like your opinion/help on this:
The first issue has to do with sockets and asyncstreams. Whenever I want a communication to begin, I open a socket, then a asyncstream. That's ok, and it works great.
I'm closing the stream and the socket in activity_pause event, so
question 1: what happens with the corresponding objects at the server's side? From what I saw with logging, socket is closing, but the stream somehow seems to remain open, until the service module I use gets destroyed.
question 2: I remember in the beginning of my coding with netLib, I tried to close sockets and streams, whenever I thought that their job was done. Somehow, my job was not really done, thus closing the socket, didn't leave time for the streams to finish their operation (at least this is what I imagine). This is also the reason I placed the socket.close inside the activity_pause. My problem with that, is what will happen if my app crashes? I mean, would it pass from 'pause' or it would leave a socket open?
question 3: after taking Erel's precious advice, I came up to create a new socket every time I had a new connection with the server. At the same time, I had a new astream object. That's ok, but I had to do it like this:
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
question 4: Occasionally, server loses its network connectivity (that's its own problem with wifi). What I noticed is that if I have an open socket with a stream, client is not getting a astreams_error, thus my app on the client side hangs. Sometimes, if server connects fast after its disconnection, streams continue, and my client continues to perform ok. It's just weird to me.
And a final question 5, irrelevant with networking:
Occasionally I experience wrong variable values when passing from an activity to another. These variables are dimmed in process_globals in my main activity, thus, I was hoping they would not get destroyed. I've read about activities' life cycle, still, I don't understand why a process_global variable, would lose its value. I know I can hold its value in a file, that's ok, but still this thing puzzles me, especially when I see an app crash because of a re-initialized variable (some of them take a 0-value time to time), while I know that this activity is in action almost all the time.
Any thoughts on these, would be really appreciated, thank you for your time.