Dear friends,
occasionally I receive an illegalState error, while I'm trying to send data over the local network to another device. I use sockets and asynchronus streams, and just because the error is not frequent, I cannot easily debug it.
Here's what the log gives:
	
	
	
	
	
	
	
		
			
			
			
			
			
		
	
	
	
		
	
	
		
	
The routine in which this error seems to be cause is:
	
	
	
	
	
	
	
		
			
			
			
			
			
		
	
	
	
		
	
	
		
	
From an earlier discussion with Erel, I create individual sockets and aStreams for each connected device to my 'server' over which the above procedure runs. Anything wrong that I don't see in my code (as usual) ? Thank you.
PS: as already been said, I use individual sockets and streams, even though I'm not 100% sure I do it correctly.
Here's how I denote a new socket:
	
	
	
	
	
	
	
		
			
			
			
			
			
		
	
	
	
		
	
	
		
	
Then, my astreams are of the form
	
	
	
	
	
	
	
		
			
			
			
			
			
		
	
	
	
		
	
	
		
	
that is, I have astreams0, astreams1 and so on.
From the 'client' side, when reading is done, I am sending a "closeSocket" string-command, and so my 'server' is closing the corresponding open socket and stream.
			
			occasionally I receive an illegalState error, while I'm trying to send data over the local network to another device. I use sockets and asynchronus streams, and just because the error is not frequent, I cannot easily debug it.
Here's what the log gives:
			
				B4X:
			
		
		
		serverlistener_astreams2_newdata (B4A line: 142)
aStreams(selectedSocket).Write(buffer)
java.lang.IllegalStateException: View com.android.internal.policy.impl.PhoneWindow$DecorView@4826adf0 has already been added to the window manager.
   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:125)
   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
   at android.view.Window$LocalWindowManager.addView(Window.java:424)
   at android.app.Dialog.show(Dialog.java:241)
   at anywheresoftware.b4a.debug.Debug.wait(Debug.java:202)
   at anywheresoftware.b4a.debug.Debug.reachBP(Debug.java:252)
   at anywheresoftware.b4a.debug.Debug.ErrorCaught(Debug.java:141)
   at wos.androidserver.serverlistener._astreams2_newdata(serverlistener.java:324)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:521)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:136)
   at anywheresoftware.b4a.BA$2.run(BA.java:244)
   at android.os.Handler.handleCallback(Handler.java:587)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:123)
   at android.app.ActivityThread.main(ActivityThread.java:4627)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:521)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
   at dalvik.system.NativeStart.main(Native Method)The routine in which this error seems to be cause is:
			
				B4X:
			
		
		
		Sub sendDataToClient(returningString As String,selectedSocket As Int)
    Dim myData As String 
    myData=returningString
   
    If myData.Length > 0 Then
        Dim buffer() As Byte
        buffer = myData.GetBytes("UTF8")
        aStreams(selectedSocket).Write(buffer)
    End If
End SubFrom an earlier discussion with Erel, I create individual sockets and aStreams for each connected device to my 'server' over which the above procedure runs. Anything wrong that I don't see in my code (as usual) ? Thank you.
PS: as already been said, I use individual sockets and streams, even though I'm not 100% sure I do it correctly.
Here's how I denote a new socket:
			
				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 subThen, my astreams are of the form
			
				B4X:
			
		
		
		Sub AStreams0_NewData (Buffer() As Byte)
Dim currentSocket As Int 
currentSocket=0
msg = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
End SubFrom the 'client' side, when reading is done, I am sending a "closeSocket" string-command, and so my 'server' is closing the corresponding open socket and stream.
			
				Last edited: 
			
		
	
								
								
									
	
								
							
							 
				 
 
		 
 
		 
 
		 
 
		 
 
		 
 
		