B4J Question B4J Socket remove CRLF from received string

aaronk

Well-Known Member
Licensed User
Hi,

I am trying to work out why a string doesn't match another string when incoming data is sent using a TCP Socket.

I am receiving the command like below:

B4X:
    Sub astream_NewData (Buffer() As Byte)
       
        Dim value As String = "bob"
       
        Dim msg As String   
        msg = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
        log("'" & msg & "'")
              
        If msg = value Then
            Log("value matches")
          Else
            Log("value doesn't match")
        End If
       
    End Sub
I am sending the string bob to the B4J program and it is logging it as:
'bob
'
Notice it is putting a CRLF

I then change it to:
B4X:
    Sub astream_NewData (Buffer() As Byte)
       
        Dim value As String = "bob"
       
        Dim msg As String   
        msg = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
        msg = msg.Replace(CRLF,"")
        log("'" & msg & "'")
              
        If msg = value Then
            Log("value matches")
          Else
            Log("value doesn't match")
        End If
       
    End Sub
It is now logging the string as:
'bob'

Which now it seems to be logging the string fine, except its saying the string is different (value doesn't match)

I am sending the string with:
bob + CRLF

I am using a B4A app for testing and I am using the following code to send the string to the B4J app:

B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private normal_socket As Socket
    Private AStreams_normal As AsyncStreams
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.

    Private Button1 As Button
    Private Button2 As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("Main")

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Socket_Connected (Successful As Boolean)

    If Successful Then
        AStreams_normal.Initialize(normal_socket.InputStream ,normal_socket.OutputStream ,"Socket")
    End If
End Sub

Sub Socket_NewData (Buffer() As Byte)

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

End Sub

Sub SendData(msg As String)

    If msg = "" Then Return
       msg = msg & CRLF
    Dim Buffer() As Byte
        Buffer = msg.GetBytes("UTF8")
  
        AStreams_normal.Write(Buffer)

End Sub
Sub Button1_Click
    If normal_socket.IsInitialized = False Then
        normal_socket.Initialize("Socket")
    End If
      
    normal_socket.connect("192.168.0.10",3000,9000)
End Sub
Sub Button2_Click
    If normal_socket.IsInitialized = False Then Return
    SendData("bob")
End Sub
The above code doesn't work correctly with my B4J app since it's adding the CRLF to the end of the string.

I then replaced the SendData sub with the following code and the B4J app worked fine since its now not sending the CRLF at the end of the string it sends.
B4X:
Sub SendData(msg As String)

    If msg = "" Then Return

    Dim Buffer() As Byte
        Buffer = msg.GetBytes("UTF8")
  
        AStreams_normal.Write(Buffer)

End Sub
Since I want my B4A app send CRLF at the end of the string, how do I remove the CRLF from the string that the B4J app receives ?

I could just remove the CRLF from the B4A app string it sends, but I have other products that will be connecting to this B4J app and that needs to send CRLF at the end of the command so I need a way to remove it in the B4J app.

Any ideas on what might be wrong and how to fix it ?
 

Erel

Administrator
Staff member
Licensed User
It is a mistake to assume that a message will arrive as a single packet in non-prefix mode. It will sometimes work and sometimes fail.

I'm unable to reproduce it. Replacing CRLF like this, does remove it:
B4X:
Dim m As String = "bob" & CRLF
Log("'" & m & "'")
m = m.Replace(CRLF, "")
Log("'" & m & "'")
 
Top