B4J Question tcp server

walterf25

Expert
Licensed User
Longtime User
Hi Erel this is the first time playing with B4J and so far im loving it
I just created a very simple TCP server which listens for text strings being sent from my mobile each text line ends with CRLF

Im using the asyncstream library and on the New_Data event I want to add each received text line to a listview the problem is that sometimes onr line of text is received but other times 2 or 3 lines of text are received at the same time and added to the listview on the same line

Im not on my computer so I can't post the code but is only about 4 to 5 lines of code

Can you maybe think of how I can solve that issue of receiving only one text line at a time?
thanks,
walter
 

walterf25

Expert
Licensed User
Longtime User
You should use AsyncStreamsText. It will collect and split the lines for you.
I tried it already Erel but it wont work. I use it on another app in B4A and it works great but it wont work in B4J.
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
Can you upload your project and also describe how it didn't work?
Hi Erel you can download the project from here https://www.dropbox.com/sh/w02s5nzcsnkel8w/eHSuJkt90j
So basically I have a text file with 1059 lines of text each line of text ends with CRLF. When I send each line of text I expect the server to read one line at a time but as I mentioned before sometimes 2 or 3 lines of text are received at a time.
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
Hi Erel, i don't use asyncstreamtext because it doesn't work with it, here's a cleaner version of the project.

I don't know if im not making any sense, but all i want is to be able to add each line of text received to the listview on the tcp server.
as you can see this is the whole code
B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Dim lblIP As Label
    Dim ImageView1 As ImageView
    Dim lblstatus As Label
    Private server As ServerSocket
    Private astream As AsyncStreams
    Dim lblstatus As Label
    Dim List1 As ListView
    Dim fullstring As String
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("main") 'Load the layout file.
    MainForm.Show
   
    server.Initialize(10203, "server")
    server.Listen   
End Sub

Sub server_NewConnection (Successful As Boolean, NewSocket As Socket)
        If Successful Then
        If astream.IsInitialized Then astream.Close
        'astream.InitializePrefix(NewSocket.InputStream, False, NewSocket.OutputStream, "astream")
        astream.Initialize(NewSocket.InputStream, NewSocket.OutputStream, "astream")
        'astream.Initialize(Me, "astream", NewSocket.InputStream, NewSocket.OutputStream)
        lblstatus.Text = "Status: Connected"
    Else
        Log(LastException)
    End If
    server.Listen
End Sub


Sub astream_NewData (Buffer() As Byte)
Dim str As String
str = BytesToString(Buffer, 0, Buffer.Length, "UTF-8")

List1.Items.Add(str)
End Sub

Sub astream_Error
    Log("Error: " & LastException)
    astream.Close
    astream_Terminated
End Sub

Sub astream_Terminated
    lblstatus.Text = "Status: Disconnected"
End Sub

The text file attached is the data i'm sending to the server line by line, the problem i'm having is that on the astream_newData event i sometimes don't get only one text of line at a time, but instead i get 2 or 3 lines of text at once.
Hope this makes a little more sense now.

Thanks,
Walter
 

Attachments

  • TCP.zip
    1.4 KB · Views: 355
  • logAIS.txt
    51.7 KB · Views: 361
Upvote 0

walterf25

Expert
Licensed User
Longtime User
This is how network communication works. Messages can be merged or split. This is exactly why you need to use AsyncStreamsText.
I understand Erel, but i tried using the AsyncStreams Text class but it does not work, the New_Text event is never raised.

Thanks,
Walter
 
Upvote 0
Top