B4J Question Mixed Server and Socket Comms Question

Paul_

Member
Licensed User
I have a B4J desktop app that opens a "socket" connection and reads data from many devices and collects this data in an internal database every 1 minute, this works very reliably.

But I now want to add a "server" to the same app so that I can send the collected data to another "client" program installed on several other computers.

If the "client" app reads data from the "server" when the server is NOT collecting any data from the other devices then there is no problem.

But if the "client" reads data from the "server" when the server is collecting data from the other devices then it will work 1-2 times but mostly the "client" app will crash.

I'm not sure how to fix this, am I making some basic coding error?

How should this problem be solved?

The basics of the socket and server/client program works as shown in the code below:

B4X:
'SOCKET code -------------------------------------------------------

Private Sub SendPacket
                SetState(False)
                If astream.IsInitialized Then
                    astream.Close
                End If
                If socket.IsInitialized Then
                    socket.close
                End If
                socket.Initialize("socket")
                socket.Connect(destIPaddress,destPort,500)
                Wait for Socket_Connected(Successful As Boolean)
                If Successful Then
                    SetState(True)
                    astream.Initialize(socket.InputStream, socket.OutputStream, "Astream") 'does not use prefix mode
                    Log("Connected > "&destIPaddress)
                    Dim txPacket() As Byte= < packet data to send >
                    astream.Write(txPacket)
        End If
End Sub

Private Sub Astream_NewData (Buffer() As Byte)
    rxPacket=bc.HexFromBytes(Buffer) 'convert byte data to Hex string so can display
    Log("Bytes in RX buffer="&Buffer.Length)
    Log("rxPacket as Hex string="&rxPacket)
    'extract the data bytes from the Hex string
    DecodeHexString
End Sub

Private Sub Astream_Terminated
    Log("Remote socket failure")
    SetState(False)
End Sub

Private Sub Astream_Error
    Astream_Terminated
End Sub

'SOCKET code end -------------------------------------------------------


'SERVER code -------------------------------------------------------

 Private Sub ListenForConnections
    server.Initialize(serverPort, "server")
    Do While True
        server.Listen
        Wait For server_NewConnection (Successful As Boolean, NewSocket As Socket)
        Log("New connection")
        If Successful Then
            If astream.IsInitialized Then
                astream.Close
            End If
            astream.InitializePrefix(NewSocket.InputStream, True, NewSocket.OutputStream, "serstream") 'using prefix mode
            Log("Client connected")
        End If
    Loop
End Sub

Private Sub serstream_NewData (Buffer() As Byte)

    Dim rxData As String
    rxData=BytesToString(Buffer, 0, Buffer.Length, "UTF8") 'convert buffer bytes to string
    Log("RX New data request: "&rxData)
    If rxData="REQLIST" Then
        list1.Clear
        'build the List1 data depending on the request
        Log("Size (rows) of List object to send: "&list1.Size)
        Dim txBytes() As Byte
        txBytes=ser.ConvertObjectToBytes(list1)
        astream.Write(txBytes)
    Else
        Log("Unknown request")
    End If
End Sub

Private Sub serstream_Terminated
    Log("Lost connection with client")
End Sub

Private Sub serstream_Error
    serstream_Terminated
End Sub

'SERVER code end -------------------------------------------------------
 
Top