Android Question [SOLVED] Communication error with B4RSerializator

RAFA BRAVO

Active Member
Licensed User
Longtime User
Everything works perfect. I get an error opening the app after I have closed it with the button back. And I have to press twice to open it

This is the error in the log:
B4X:
main_activity_create (java line: 380)
java.net.BindException: bind failed: EADDRINUSE (Address already in use)
             at libcore.io.IoBridge.bind(IoBridge.java:103)
            at java.net.PlainSocketImpl.socketBind(PlainSocketImpl.java:157)
            at java.net.AbstractPlainSocketImpl.bind(AbstractPlainSocketImpl.java:427)
            at java.net.ServerSocket.bind(ServerSocket.java:377)
            at java.net.ServerSocket.<init>(ServerSocket.java:237)
            at java.net.ServerSocket.<init>(ServerSocket.java:128)
            at anywheresoftware.b4a.objects.SocketWrapper$ServerSocketWrapper.Initialize(SocketWrapper.java:294)
            at b4a.FORDplcNEW.main._activity_create(main.java:380)
            at java.lang.reflect.Method.invoke(Native Method)
            at anywheresoftware.b4a.BA.raiseEvent2(BA.java:196)
            at b4a.FORDplcNEW.main.afterFirstLayout(main.java:104)
            at b4a.FORDplcNEW.main.access$000(main.java:17)
            at b4a.FORDplcNEW.main$WaitForLayout.run(main.java:82)
            at android.os.Handler.handleCallback(Handler.java:873)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:201)
            at android.app.ActivityThread.main(ActivityThread.java:6861)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
Caused by: android.system.ErrnoException: bind failed: EADDRINUSE (Address already in use)
            at libcore.io.Linux.bind(Native Method)
            at libcore.io.ForwardingOs.bind(ForwardingOs.java:60)
            at libcore.io.IoBridge.bind(IoBridge.java:99)
            ... 19 more
 

RAFA BRAVO

Active Member
Licensed User
Longtime User
I have no idea where the error comes from, that's why the title name. maybe with communication, attached the main module:

main:
Sub Process_Globals
    
    Private server As ServerSocket
    Dim output As String
    Dim monitor1 As String
    Private rp As RuntimePermissions
    
End Sub

Sub Globals
    
    Private astream As AsyncStreams
    Private ser As B4RSerializator
    Public connected As Boolean
    Dim ImgToggleButtonLED1 As BitmapDrawable
    Private Button1 As Button
    Private Button2 As Button
    Private Button3 As Button
    Private ToggleButton1 As ToggleButton
    Private LabelAlertaImage As Label
    Private LabelAlertaText As Label
    Private LabelAlertaText1 As Label
    Private TabHost1 As TabHost
    Private LabelMONITOR As Label
    Private RadioButton1 As RadioButton
    Private RadioButton2 As RadioButton
    Private Panel3 As Panel
    Private PanelPAG3 As Panel
    Private LabelPARADA As Label
    
End Sub

Sub Activity_Create(FirstTime As Boolean)
    
    Activity.LoadLayout("Titulo")
    
    ser.Initialize
    TabHost1.AddTab("MONITORIZAR", "MONITORIZAR.Bal")
    TabHost1.AddTab("PARAR", "Parada.Bal")
    TabHost1.AddTab("LLAMAR", "LLamada.Bal")
    
    
    ImgToggleButtonLED1.Initialize(LoadBitmap(File.DirAssets, "setaOFF.png"))
    ImgToggleButtonLED1.Gravity = Gravity.FILL
    ToggleButton1.Background = ImgToggleButtonLED1
    
    server.Initialize(51042, "server")
    server.Listen
    
End Sub



Sub Activity_Resume
    
    
End Sub

Sub Activity_Pause (UserClosed As Boolean)
'
End Sub


private Sub Server_NewConnection (Successful As Boolean, NewSocket As Socket)
'    Log($"NewConnection: ${Successful}"$)
    If Successful Then
        connected = True
        If astream.IsInitialized Then astream.Close
        astream.Initialize(NewSocket.InputStream, NewSocket.OutputStream, "astream")
    End If
    
End Sub

Private Sub AStream_NewData (Buffer() As Byte)
    Dim objects() As Object = ser.ConvertBytesToArray(Buffer)
    For Each o As Object In objects
'        Log(o)
        Select Case o
            Case "paro1T"
                LabelAlertaImage.Text=Chr(0xE002)
                LabelAlertaImage.TextColor = Colors.Red
                LabelAlertaText.Text= "PARO ZONA 1"
                LabelAlertaText1.Text= "CERRADURA"
                LabelAlertaText.TextColor = Colors.Red
                LabelAlertaText1.TextColor = Colors.Red
                LabelMONITOR.Text= "1"
                monitor1 = "666332938"
                RadioButton1.Checked=True
                LabelPARADA.Visible=True
                
        
            Case "paro2T"
                LabelAlertaImage.Text=Chr(0xE002)
                LabelAlertaImage.TextColor = Colors.Red
                LabelAlertaText.Text= "PARO ZONA 1"
                LabelAlertaText1.Text= "SETA MOTOR"
                LabelAlertaText.TextColor = Colors.Red
                LabelAlertaText1.TextColor = Colors.Red
                LabelMONITOR.Text="1"
                monitor1 = "666332938"
                RadioButton2.Checked=True
                LabelPARADA.Visible=True
                
            Case "paro3T"
                LabelAlertaImage.Text=Chr(0xE002)
                LabelAlertaImage.TextColor = Colors.Red
                LabelAlertaText.Text= "PARO ZONA 1"
                LabelAlertaText1.Text= "POCAYOKE ANTIPIN"
                LabelAlertaText.TextColor = Colors.Red
                LabelAlertaText1.TextColor = Colors.Red
                LabelMONITOR.Text="1"
                monitor1 = "666332938"
                RadioButton2.Checked=True
                LabelPARADA.Visible=True
                
            Case "paro4T"
                LabelAlertaImage.Text=Chr(0xE002)
                LabelAlertaImage.TextColor = Colors.Red
                LabelAlertaText.Text= "PARO ZONA 1"
                LabelAlertaText1.Text= "TORNILLO MASA"
                LabelAlertaText.TextColor = Colors.Red
                LabelAlertaText1.TextColor = Colors.Red
                LabelMONITOR.Text="1"
                monitor1 = "666332938"
                RadioButton2.Checked=True
                LabelPARADA.Visible=True
                
            Case "paro5T"
                LabelAlertaImage.Text=Chr(0xE002)
                LabelAlertaImage.TextColor = Colors.Red
                LabelAlertaText.Text= "PARO ZONA 1"
                LabelAlertaText1.Text= "PRISIONERO DERECHA"
                LabelAlertaText.TextColor = Colors.Red
                LabelAlertaText1.TextColor = Colors.Red
                LabelMONITOR.Text="1"
                monitor1 = "666332938"
                RadioButton2.Checked=True
                LabelPARADA.Visible=True
                
            Case "paro6T"
                LabelAlertaImage.Text=Chr(0xE002)
                LabelAlertaImage.TextColor = Colors.Red
                LabelAlertaText.Text= "PARO ZONA 1"
                LabelAlertaText1.Text= "SETA LEBOMAT TRASERA DERECHA"
                LabelAlertaText.TextColor = Colors.Red
                LabelAlertaText1.TextColor = Colors.Red
                LabelMONITOR.Text="1"
                monitor1 = "666332938"
                RadioButton2.Checked=True
                LabelPARADA.Visible=True
                
            Case "paro7T"
                LabelAlertaImage.Text=Chr(0xE002)
                LabelAlertaImage.TextColor = Colors.Red
                LabelAlertaText.Text= "PARO ZONA 1"
                LabelAlertaText1.Text= "FINAL DE CARRERA DELANTERA IZQUIERDA"
                LabelAlertaText.TextColor = Colors.Red
                LabelAlertaText1.TextColor = Colors.Red
                LabelMONITOR.Text="1"
                monitor1 = "666332938"
                RadioButton2.Checked=True
                LabelPARADA.Visible=True
                
            Case "paro8T"
                LabelAlertaImage.Text=Chr(0xE002)
                LabelAlertaImage.TextColor = Colors.Red
                LabelAlertaText.Text= "PARO ZONA 2"
                LabelAlertaText1.Text= "ASIENTO TRASERO"
                LabelAlertaText.TextColor = Colors.Red
                LabelAlertaText1.TextColor = Colors.Red
                LabelMONITOR.Text="2"
                monitor1 = "651422831"
                RadioButton2.Checked=True
                LabelPARADA.Visible=True
                
            Case "paro9T"
                LabelAlertaImage.Text=Chr(0xE002)
                LabelAlertaImage.TextColor = Colors.Red
                LabelAlertaText.Text= "PARO ZONA 2"
                LabelAlertaText1.Text= "ASIENTO DELANTERO"
                LabelAlertaText.TextColor = Colors.Red
                LabelAlertaText1.TextColor = Colors.Red
                LabelMONITOR.Text="2"
                monitor1 = "651422831"
                RadioButton2.Checked=True
                LabelPARADA.Visible=True
                
            Case "paro10T"
                LabelAlertaImage.Text=Chr(0xE002)
                LabelAlertaImage.TextColor = Colors.Red
                LabelAlertaText.Text= "PARO ZONA 2"
                LabelAlertaText1.Text= "ROBOT"
                LabelAlertaText.TextColor = Colors.Red
                LabelAlertaText1.TextColor = Colors.Red
                LabelMONITOR.Text="2"
                monitor1 = "651422831"
                RadioButton2.Checked=True
                LabelPARADA.Visible=True
                
            Case "paro11T"
                LabelAlertaImage.Text=Chr(0xE002)
                LabelAlertaImage.TextColor = Colors.Red
                LabelAlertaText.Text= "PARO ZONA 2"
                LabelAlertaText1.Text= "FINAL DE CARRERA MANIPULADOR IZQUIERDA"
                LabelAlertaText.TextColor = Colors.Red
                LabelAlertaText1.TextColor = Colors.Red
                LabelMONITOR.Text="2"
                monitor1 = "651422831"
                RadioButton2.Checked=True
                LabelPARADA.Visible=True
                
            Case "paro12T"
                LabelAlertaImage.Text=Chr(0xE002)
                LabelAlertaImage.TextColor = Colors.Red
                LabelAlertaText.Text= "PARO ZONA 2"
                LabelAlertaText1.Text= "POKAYOKE TAPONES"
                LabelAlertaText.TextColor = Colors.Red
                LabelAlertaText1.TextColor = Colors.Red
                LabelMONITOR.Text="2"
                monitor1 = "651422831"
                RadioButton2.Checked=True
                LabelPARADA.Visible=True
                
            Case "paro13T"
                LabelAlertaImage.Text=Chr(0xE002)
                LabelAlertaImage.TextColor = Colors.Red
                LabelAlertaText.Text= "PARO ZONA 2"
                LabelAlertaText1.Text= "STRAIKER DERECHA"
                LabelAlertaText.TextColor = Colors.Red
                LabelAlertaText1.TextColor = Colors.Red
                LabelMONITOR.Text="2"
                monitor1 = "651422831"
                RadioButton2.Checked=True
                LabelPARADA.Visible=True
                
            Case "paro14T"
                LabelAlertaImage.Text=Chr(0xE002)
                LabelAlertaImage.TextColor = Colors.Red
                LabelAlertaText.Text= "PARO ZONA 2"
                LabelAlertaText1.Text= "MASA CAPO"
                LabelAlertaText.TextColor = Colors.Red
                LabelAlertaText1.TextColor = Colors.Red
                LabelMONITOR.Text="2"
                monitor1 = "651422831"
                RadioButton2.Checked=True
                LabelPARADA.Visible=True
                
                
            Case "SETA"
                LabelAlertaImage.Text=Chr(0xE002)
                LabelAlertaImage.TextColor = Colors.Red
                LabelAlertaText.Text= "PARO SETA APP"
                LabelAlertaText1.Text= ""
                LabelAlertaText.TextColor = Colors.Red
                LabelMONITOR.Text=""
                monitor1 = ""
                RadioButton1.Checked=False
                RadioButton2.Checked=False
                LabelPARADA.Visible=True
                
                
            Case "ACTIVADA"
                LabelAlertaImage.Text=Chr(0xE5CA)
                LabelAlertaImage.TextColor = Colors.Green
                LabelAlertaText.Text = "ACTIVADA"
                LabelAlertaText1.Text= ""
                LabelAlertaText.TextColor=Colors.Green
                LabelPARADA.Visible=False
        End Select
    Next

    astream.Write(ser.ConvertArrayToBytes(Array(output)))
End Sub




Sub ToggleButton1_CheckedChange(Checked As Boolean)
    
    If Checked = False Then
        ImgToggleButtonLED1.Initialize(LoadBitmap(File.DirAssets, "setaOFF.png"))
        ImgToggleButtonLED1.Gravity = Gravity.FILL
        ToggleButton1.Background = ImgToggleButtonLED1
        output = "out1 T"
        ToastMessageShow ("SETA OFF", False)
    Else
        ImgToggleButtonLED1.Initialize(LoadBitmap(File.DirAssets, "setaON.png"))
        ImgToggleButtonLED1.Gravity = Gravity.FILL
        ToggleButton1.Background = ImgToggleButtonLED1
        output = "out1 F"
        ToastMessageShow ("SETA ON", False)
    End If
End Sub

Sub Button1_Click
    server.Listen
End Sub

Sub Button2_Click
    rp.CheckAndRequest(rp.PERMISSION_CALL_PHONE)
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    If Result = False Then Return
    ' PERMISSION_CALL_PHONE includes phone state
    Dim pc As PhoneCalls
    StartActivity(pc.Call(monitor1))
End Sub



Sub Button3_Click
    Panel3.Visible=True
End Sub

Sub RadioButton1_CheckedChange(Checked As Boolean)
    monitor1 = "666332938"
    LabelMONITOR.Text = "1"
    Panel3.Visible=False
End Sub

Sub RadioButton2_CheckedChange(Checked As Boolean)
    monitor1 = "651422831"
    LabelMONITOR.Text = "2"
    Panel3.Visible=False
End Sub

Sub PanelPAG3_Click
    Panel3.Visible=False
End Sub



Private Sub Astream_Error
'    Log("Disconnected")
    connected = False
End Sub

Private Sub Astream_Terminated
    Astream_Error
End Sub
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
It is failing on
B4X:
 server.Initialize(51042, "server")
Because you don't close 'server' and are reopening it on the same port. 'server' is a Process_Global so will survive Activity_Pause. Android will not necessarily immediately kill your process so 'server' can remain instantiated. The same error will occur if you have the attribute '#SupportedOrientations: unspecified' and rotate the device.
 
Upvote 0
Top