Android Question Get socket closed value

devmobile

Active Member
Licensed User
The isClosed method from socket return socket state
I try get it with javaobject but it show error
my code
B4X:
Sub isClose As Boolean
    Dim java As JavaObject
    java.InitializeContext
    Return java.RunMethod("getClosedSocket",Array(MySocket))
End Sub
#if java
   
    import java.net.Socket;
   
    public boolean getClosedSocket(Socket socket) {
        return socket.isClosed;
    }
   
#End If
 

devmobile

Active Member
Licensed User
I attach my service for socket
Sometime socket failed because i cannot detect socket is close or not
B4X:
#Region  Service Attributes
    #StartAtBoot: True
    #StartCommandReturnValue: android.app.Service.START_STICKY
#End Region

Sub Process_Globals
    Private MySocket As Socket
    Private syn As AsyncStreams
    Public Connected As Boolean
    Private Timer1 As Timer
End Sub

Sub Service_Create

    Service.StartForeground(0,Null)
    Timer1.Initialize("tmr",200)
   
End Sub

Sub Service_Start (StartingIntent As Intent)
    Timer1.Enabled = True
End Sub

Sub tmr_Tick
    If Connected = False Then
        If CheckConnection Then
            StartSocket
            Timer1.Enabled = False
        End If
    End If
   
End Sub

Sub Service_Destroy
    Log("destory service")
End Sub

Sub StartSocket
    MySocket.Initialize("socket")
    MySocket.Connect("0.0.0.0",3003,0)
End Sub

Sub socket_Connected (Successful As Boolean)
   
    If Successful Then
       
        If Connected Then
            Return
        End If
       
        Dim id As String
        id = "device_id=" & rand(1000,9000)
       
        Try
            syn.Initialize(MySocket.InputStream,MySocket.OutputStream,"sync")
            syn.Write(id.GetBytes("UTF8")) 'register device in system
            Connected    =    True
            Log("Connect to server")
        Catch
            Connected    =    False
        End Try
   
    Else
        Timer1.Enabled = True
    End If
   
End Sub

Sub sync_NewData (Buffer() As Byte)
   
    Dim data As String
    data = BytesToString(Buffer,0,Buffer.Length,"UTF-8")
   
    If data = "registered device successfully" Then
        Return
    End If
   
    Try
        Dim js As JSONParser
        js.Initialize(data)
    Catch
        Return
    End Try
 
   
End Sub

Sub sync_Error
    MySocket.Close
    syn.Close
    Connected = False
    Log("error stream")
    Timer1.Enabled = True
End Sub

Sub sync_Terminated
    MySocket.Close
    Connected = False
    Log("error terminate")
    Timer1.Enabled = True
End Sub

Sub sync_NewStream (Dir As String, FileName As String)
   
End Sub

Sub CheckConnection As Boolean
   
    Dim r As Reflector
  
    r.Target = r.GetContext
    r.Target = r.RunMethod2("getSystemService", "connectivity", "java.lang.String")
    r.Target = r.RunMethod("getActiveNetworkInfo")
  
    If r.Target <> Null Then
        Return r.RunMethod("isConnected")
    End If
  
    Return False
   
End Sub
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
The isClosed method will not help. There are cases where the network driver thinks that it is still connected although it isn't. The Terminated event will be raised when the state is updated.

If you are connecting to your own server then you should use a http server such as B4J and send the requests with OkHttpUtils2. It will be much simpler and will work better.

The other option is to implement a "heartbeat" procedure that sends a byte every second and if the server doesn't respond after 10 seconds then it closes the connection.
 
Upvote 0

devmobile

Active Member
Licensed User
The isClosed method will not help. There are cases where the network driver thinks that it is still connected although it isn't. The Terminated event will be raised when the state is updated.

If you are connecting to your own server then you should use a http server such as B4J and send the requests with OkHttpUtils2. It will be much simpler and will work better.

The other option is to implement a "heartbeat" procedure that sends a byte every second and if the server doesn't respond after 10 seconds then it closes the connection.
Option "heartbeat",if i send byte every second,the server dont busy?
 
Upvote 0
Top