Android Question String length

gruizelgruis

Member
Licensed User
Longtime User
Hi all,
Sorry if this has been asked before.
I'm debugging a project I created a view years back. I think I run into a string length problem. My question is:
What is the max lengts of a variabel declared as a string ?
Are there any alternatives ?

Pls advice
 

gruizelgruis

Member
Licensed User
Longtime User
I just checked it all out: My app communicates with a VB6 program. The VB6 application sends a LONG string but the B4a app only recieves about 1449 character of the string. I'm using "AStreams_NewData (Buffer() As Byte) " Could the buffer() as byte be a problem. ?
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
What is the length of the buffer when the data is received and how are you converting it to a string?
 
Upvote 0

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
Without seeing your code it is really hard to answer.

Since you're communicating with a VB6 program, I would assume that you are not using prefix mode to communicate, since the data arrives asynchronously that means that the bytes you are receiving from the VB6 program may arrive in various length packets at different intervals; and you need to allow for that.

You should implement the prefix mode handling on your VB6 side if possible and initialize with prefix, or use some type of STX / ETX / EOT indicators to frame your byte stream.

If you are using prefix mode then disregard the above :)
 
Upvote 0

gruizelgruis

Member
Licensed User
Longtime User
What is the length of the buffer when the data is received and how are you converting it to a string?
The data that was meant to be send is 8383 characters long.

B4X:
Sub AStreams_NewData (Buffer() As Byte)
     Dim MsgReceived As String
    Dim MsgPart() As String
    MsgReceived = BytesToString(Buffer, 0, Buffer.Length, "ASCII")
    Log(MsgReceived)
    '// en de verbinding sluiten   
    AStreams.Close
    Socket1.Close

I use BytesToString to convert it.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
The data that was meant to be send is 8383 characters long.
that means that the bytes you are receiving from the VB6 program may arrive in various length packets at different intervals; and you need to allow for that.
Asyncstream data can be splitted...

Do you get the event raised multiple times?
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
What is the length of Buffer when the data is received?
B4X:
Log(Buffer.Length)
 
Upvote 0

gruizelgruis

Member
Licensed User
Longtime User
Without seeing your code it is really hard to answer.

Since you're communicating with a VB6 program, I would assume that you are not using prefix mode to communicate, since the data arrives asynchronously that means that the bytes you are receiving from the VB6 program may arrive in various length packets at different intervals; and you need to allow for that.

You should implement the prefix mode handling on your VB6 side if possible and initialize with prefix, or use some type of STX / ETX / EOT indicators to frame your byte stream.

If you are using prefix mode then disregard the above :)

As I remember it (it a bit rusty) in 'prefix mode' You send the length of the string at the end with the sendstring.
As i'm debugging now It looks like a timing problem. Perhaps if I slowdown the VB6 program a bit ?
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
In your code you are closing the stream and socket when the first message is received, so you are unlikely to receive subsequent messages.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
OK So you need to remove the
B4X:
AStreams.Close
 Socket1.Close

Code and see if the rest of the data is received in subsequent NewData calls.

If it is, you will need to workout how to manage it properly so that you know when all of the data has been received.
 
Upvote 0

gruizelgruis

Member
Licensed User
Longtime User
OK So you need to remove the
B4X:
AStreams.Close
 Socket1.Close

Code and see if the rest of the data is received in subsequent NewData calls.

If it is, you will need to workout how to manage it properly so that you know when all of the data has been received.

I Did as you suggested. But I stil do not receive the whole thing. I'll work on the "Prefix" method.

I feel it's not hard to implement. altough I Dont know what to send from the VB program.

Thank you
 
Upvote 0

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
All you need to do is to pre-pend the length of the data you are sending to the actual data sent in the first 4 bytes, here's an example in .NET (you should be able to VB6-it though):
B4X:
    Private Sub SendDataToSocket(ByRef oSocket As Winsock, ByVal Data As String)
        Dim pyBytes() As Byte
        Dim pySize() As Byte
        Dim pyOut() As Byte

        pyBytes = System.Text.Encoding.UTF8.GetBytes(Data)
        pySize = System.BitConverter.GetBytes(pyBytes.Length)

        Array.Resize(pyOut, pySize.Length + pyBytes.Length)
        Array.Copy(pySize, 0, pyOut, 0, pySize.Length)
        Array.Copy(pyBytes, 0, pyOut, 4, pyBytes.Length)

        If oSocket IsNot Nothing Then
            oSocket.Send(pyOut)
        End If
    End Sub

On the VB6 receiving end, you basically do the opposite of this, take the first 4 bytes off the incoming data and continue to build a buffer until you reach the size you sent or you reach your timeout value waiting for it to arrive.
 
Last edited:
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Upvote 0

gruizelgruis

Member
Licensed User
Longtime User
Upvote 0

gruizelgruis

Member
Licensed User
Longtime User
All you need to do is to pre-pend the length of the data you are sending to the actual data sent in the first 4 bytes, here's an example in .NET (you should be able to VB6-it though):
B4X:
    Private Sub SendDataToSocket(ByRef oSocket As Winsock, ByVal Data As String)
        Dim pyBytes() As Byte
        Dim pySize() As Byte
        Dim pyOut() As Byte

        pyBytes = System.Text.Encoding.UTF8.GetBytes(Data)
        pySize = System.BitConverter.GetBytes(pyBytes.Length)

        Array.Resize(pyOut, pySize.Length + pyBytes.Length)
        Array.Copy(pySize, 0, pyOut, 0, pySize.Length)
        Array.Copy(pyBytes, 0, pyOut, 4, pyBytes.Length)

        If oSocket IsNot Nothing Then
            oSocket.Send(pyOut)
        End If
    End Sub

On the VB6 receiving end, you basically do the opposite of this, take the first 4 bytes off the incoming data and continue to build a buffer until you reach the size you sent or you reach your timeout value waiting for it to arrive.

Im not getting anywhere with this. am I correct to say if the length of the string is Len(senddata) = 603 Then the prefix would be chr(93) & chr(2) & chr(0) & chr(0) ?
please advice I'm lost
 
Upvote 0
Top