Android Question After AStreams.Close and Socket1.Close the Connection is still alive

Discussion in 'Android Questions' started by GvD, Aug 9, 2018.

  1. GvD

    GvD Member Licensed User

    i tried your first example with aStreams and an esp8266 sending data through
    "client.connect"; "client.println(...)"; and "client.stop();".
    It works fine. The mobile receives all data.
    But when i close the app - the connection still stays alive.
    After starting the app again - it crashes and the connection is closed.
    Now starting the app - all ok.
    I don't use toastmessages ...
    Code:
    Sub Activity_Pause(UserClosed As Boolean)
        
    If UserClosed Then
            
    Log("closing")
            AStreams.Close
            Socket1.Close
        
    End If
    End Sub
    This is not enough to end the connection?


    here a part of the log after crash:

    Code:
    ** Activity (main) Create, isFirst = false **
    running waiting messages (
    1)
    ** 
    Activity (main) Resume **
    ON
    AStreams_Terminated
    main_astreams_terminated (java line: 
    413)
    java.lang.RuntimeException: Object should first be initialized (
    Exception).
       at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:
    50)
       at anywheresoftware.b4a.objects.B4AException.getMessage(B4AException.java:
    21)
       at b4a.GvDIT_SOS.main._astreams_terminated(main.java:
    413)
       at java.lang.reflect.Method.invoke(Native Method)
       at anywheresoftware.b4a.BA.raiseEvent2(BA.java:
    191)
       at anywheresoftware.b4a.BA$
    2.run(BA.java:365)
       at android.os.Handler.handleCallback(Handler.java:
    739)
       at android.os.Handler.dispatchMessage(Handler.java:
    95)
       at android.os.Looper.loop(Looper.java:
    158)
       at android.app.ActivityThread.main(ActivityThread.java:
    7224)
       at java.lang.reflect.Method.invoke(Native Method)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:
    1230)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:
    1120)

    sorry about my english ...
    anyone know this problem? and can tell me the solution?
     
  2. GvD

    GvD Member Licensed User

    ... after sleeping i tryed the newer example from B4A_Network.zip... same result: receiving message is ok but after ending the app the connection stayes alive :(
     
  3. Erel

    Erel Administrator Staff Member Licensed User

  4. GvD

    GvD Member Licensed User

    sorry - but no
    - the mobile has to act as a server ( receiving messages )
    - in the 2. Version of your example a service is used. The only change i'd done is to change from
    Code:
    Private Sub ListenForConnections
        
    Do While working
            
    server.Listen
            
    Wait For Server_NewConnection (Successful As Boolean, NewSocket As Socket)
            
    If Successful Then
                CloseExistingConnection
                client = NewSocket
                
    'astream.InitializePrefix(client.InputStream, False, client.OutputStream, "astream")
                astream.Initialize(client.InputStream, client.OutputStream, "astream")
                UpdateState(
    True)
            
    End If
        
    Loop
    End Sub
    :(

    and
    Code:
    Sub Activity_Pause (UserClosed As Boolean)
        
    CallSub(Starter, "Disconnect")
    End Sub
    when i kill the app in adroid ( version 6.0.1 ) - the connection is closed immediately
     
    Last edited: Aug 10, 2018
  5. Erel

    Erel Administrator Staff Member Licensed User

    I can only guess that you are somehow making multiple connections. Calling Socket.Close should close the last socket created.
     
  6. GvD

    GvD Member Licensed User

    hmmm - that's possible... the sender acts sometimes every 3 seconds and close this connection after sending...
    -> is there a possibility to close all connections with one call?
     
  7. Erel

    Erel Administrator Staff Member Licensed User

    You should make sure to close the socket before you reuse the variable.
     
  8. GvD

    GvD Member Licensed User

    ok - thanks a lott! i'll try this!
    After testing you get an respond :)
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice