Android Question socket.connected or not?

rtek1000

Active Member
Licensed User
Longtime User
Hi,

Even if you turn off the server, this function does not realize that it can not send more data.
How can the client detect the offline server?

https://www.b4x.com/android/help/network.html#socket_connected

B4X:
#Region Module Attributes
    #FullScreen: False
    #IncludeTitle: True
    #ApplicationLabel: Client
    #VersionCode: 1
    #VersionName:
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

'Activity module
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim Socket2 As Socket
    Dim ServerSocket1 As ServerSocket

    Dim AStreamsClient As AsyncStreams
   
    Dim timer1 As Timer
   

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim lblConnect As Label
    Dim btnConnect As Button

    Private btnClientSend As Button

    Private lblClientLog As Label
    Private lblServerLog As Label
    Private etServerIP As EditText
    Private etServerPort As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("1")
   
    lblConnect.Text = ServerSocket1.GetMyIP
   
    timer1.Initialize("timer1", 1000)
   
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub timer1_tick()
    If Socket2.Connected = False Then
        timer1.Enabled = False
        lblClientLog.Text = DateTime.Now & ": " & "Disconnected!" & CRLF & lblClientLog.Text
    End If
End Sub

Sub Socket2_Connected(Connected As Boolean)As Boolean
    If Connected = True Then
        ToastMessageShow("Connected",True)
       
        lblClientLog.Text = DateTime.Now & ": " & "Connected!" & CRLF & lblClientLog.Text
        AStreamsClient.Initialize(Socket2.InputStream,Socket2.OutputStream,"AStreamsClient")
       
        timer1.Enabled = True
    Else
        ToastMessageShow("Server not available",True)
       
        lblClientLog.Text = DateTime.Now & ": " & "Server not available" & CRLF & lblClientLog.Text
        btnConnect.Text = "Connect"
    End If

End Sub

Sub AStreamsClient_NewData (Buffer() As Byte)
    Dim msg As String
    Dim cMsg As String

    Dim unsignedi As Int
    Dim signedb As Byte
   
    msg = BytesToString(Buffer, 0, Buffer.Length, "ASCII")
    lblClientLog.Text = DateTime.Now & ": " & msg & CRLF & lblClientLog.Text
    Log(msg)
   
End Sub

Sub AStreamsServer_Error
    ToastMessageShow(LastException.Message, True)
End Sub

Sub SendData(msg As String)
    Dim Buffer() As Byte
   
    Buffer = msg.GetBytes("UTF8")
    AStreamsClient.Write(Buffer)
    AStreamsClient.Write(Array As Byte(10))
    AStreamsClient.Write(Array As Byte(13))

End Sub

Sub btnConnect_Click
    Select Case btnConnect.Text
        Case "Connect"
            btnConnect.Text = "Disconnect"
            Socket2.Initialize("Socket2")
           
            Socket2.Connect(etServerIP.Text,etServerPort.Text,5000)
        Case "Disconnect"
            btnConnect.Text = "Connect"
            Socket2.Close
           
        Case Else
    End Select
End Sub

Sub btnClientSend_Click
    Dim msg As String
   
    msg = "Teste"
    SendData(msg)
End Sub
 
Top