B4J Question How to Get status of server continuously

pythonds1

New Member
I Made a UI which send Text message from server to client and client to server but when i close server client should be disconnected but it did't. How to do that ?

server.b4j

B4X:
#Region Project Attributes 
    #MainFormWidth: 300
    #MainFormHeight: 400 
#End Region


Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Public server As ServerSocket   
    Public astream As AsyncStreams
    Private msg1 As TextField
    Private lbl3 As Label
    Private btn1 As Button
End Sub

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

Sub Listen_Connection()
    server.Listen
    If server.IsInitialized = True Then
        Log("Started")
    End If
   
    Wait For Server_NewConnection (Successful As Boolean, NewSocket As Socket)
    If Successful Then
        Log("New Connection")
        lbl3.Text = "Connected"
        astream.Initialize(NewSocket.InputStream, NewSocket.OutputStream, "astream")
    End If
   
End Sub

Sub astream_NewData (Buffer() As Byte)
    Dim msg As String
    msg = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
    Log(msg)
End Sub
   
Sub btn1_MouseClicked (EventData As MouseEvent)   
    server.Initialize(6565,"Server")
    If server.IsInitialized = True Then
        btn1.Text = "Started"
    End If
    'Log(server.GetMyIP())
    Listen_Connection   
End Sub

Sub btn2_MouseClicked (EventData As MouseEvent)
    astream.Write(msg1.Text.GetBytes("utf8"))
    msg1.Text = ""
End Sub

client.b4j
B4X:
#Region Project Attributes 
    #MainFormWidth: 300
    #MainFormHeight: 400 
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private fx As JFX
    Public socket As Socket
    Private astream As AsyncStreams
    Private txt1 As TextField
    Private btn1 As Button   
    Private msg1 As TextField
    Private lbl3 As Label
    Private sh As Shell
End Sub

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

Sub Socket_Connected (Successful As Boolean)
    Try
        If socket.Connected = True Then
            lbl3.Text = "Connected"       
        Else
            lbl3.Text = "Not Connected"   
        End If
       
        If Successful Then
            astream.Initialize(socket.InputStream, socket.OutputStream,"astream")
        End If
    Catch
        Log("Error ")
    End Try           
End Sub

Sub astream_NewData (Buffer() As Byte)
    Dim msg As String   
    msg = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
    Log(msg) 
End Sub

Sub btn1_MouseClicked (EventData As MouseEvent)   
    Try       
        socket.Initialize("Socket")
        socket.Connect(txt1.Text,6565,10000)
    Catch
        Log("Waiting to server")
    End Try   
End Sub

Sub btn2_MouseClicked (EventData As MouseEvent)
    astream.Write(msg1.Text.GetBytes("utf8"))
    msg1.Text = ""
End Sub
 
Top