B4J Question Detect connection that closed

Discussion in 'B4J Questions' started by aaronk, Jan 25, 2015.

  1. aaronk

    aaronk Well-Known Member Licensed 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 ?

    Code:
    #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(3As 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
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    Peter Simpson likes this.
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