B4J Question Detect connection that closed

aaronk

Well-Known Member
Licensed User
Longtime User
Hi,

My project will accept multiple incoming connections using a ServerSocket.

When a device (such as a B4A app) connects to this project it will add the connection to a TableView.
However, if the device (such as the B4a app) disconnects the connection it runs the code in the Server_Terminated sub, which is fine.

However, since multiple devices (such as multiple apps) will connect how can I tell which device just disconnected?

The code I am using is below..
Any ideas on how to detect which connection just closed ?

B4X:
#Region  Project Attributes
    #MainFormWidth: 790
    #MainFormHeight: 545
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private Table As TableView
    Private server As ServerSocket
    Dim connectionId As Int = 0
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Main") 'Load the layout file.
    MainForm.Show

    Table.SetColumns(Array As String("Connection ID", "Remote IP","Last Incoming ASCII Message"))
  
    server.Initialize(5005, "server")
    server.Listen

  
End Sub

Sub Server_NewConnection (Successful As Boolean, NewSocket As Socket)
    If Successful Then
        Dim astream As AsyncStreams
        astream.Initialize(NewSocket.InputStream, NewSocket.OutputStream, "astream")
        Dim row(3) As Object
            row(0) = connectionId
            row(1) = NewSocket.RemoteAddress
            row(2) = ""
            Table.Items.Add(row)
            connectionId = connectionId + 1
    Else
        Log(LastException)
    End If
    server.Listen
End Sub

Sub Server_Terminated
    Log("astream_Terminated")
End Sub
 
Top