B4J Question Server Socket with TableView issue

aaronk

Well-Known Member
Licensed User
Longtime User
Hi,

I am trying to create a server that will listen on a port and wait for client devices to connect to it.

I then want to display the current connected devices in a TableView and when a device disconnects I then want the device to disappear from the TableView.

I then want to update the TableView with the last ASCII message that the client sent.

I am using the code below..

I seem to be able to add the client to the TableView when it connects but I am having issues when they send a ASCII message to it.

The error I get is:
B4X:
Program started.
java.lang.RuntimeException: Message size too large. Prefix mode can only work if both sides of the connection follow the 'prefix' protocol.
    at anywheresoftware.b4a.randomaccessfile.AsyncStreams$AIN.run(AsyncStreams.java:205)
    at java.lang.Thread.run(Thread.java:744)

The Code I am using is:

B4X:
#Region  Project Attributes
    #MainFormWidth: 790
    #MainFormHeight: 545
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private Table As TableView
    Private server As ServerSocket
    Dim connectionId As Int = 0
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Main") 'Load the layout file.
    MainForm.Show

    Table.SetColumns(Array As String("Connection ID", "Remote IP","Last Incoming ASCII Message"))
   
    server.Initialize(5005, "server")
    server.Listen

   
End Sub

Sub Server_NewConnection (Successful As Boolean, NewSocket As Socket)
    If Successful Then
        Dim astream As AsyncStreams
        astream.InitializePrefix(NewSocket.InputStream, False, NewSocket.OutputStream, "astream")
        Dim row(3) As Object
            row(0) = connectionId
            row(1) = NewSocket.RemoteAddress
            row(2) = ""
            Table.Items.Add(row)
            connectionId = connectionId + 1
    Else
        Log(LastException)
    End If
    server.Listen
End Sub

Sub astream_NewData (Buffer() As Byte)
'    Log("received: " & DateTime.GetSecond(DateTime.Now))

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

    Dim selected As Int = table.SelectedRow
    If selected = -1 Then Return
    Dim row() As Object = table.SelectedRowValues
    Dim lbl As Label = row(1)
    lbl.Text = msg
   
End Sub

Anyone know why it crashes when it receives the incoming ASCII message ?

Also am I using the correct code to update the TableView with the incoming command ?
And what is the best code to delete a item from the TableView when the client disconnects ?
 

aaronk

Well-Known Member
Licensed User
Longtime User
I am using the following code in B4A for testing which is connecting to my B4J project..

Press the 'connect' it will connect to my B4J project, and you will see the connection pop up in the list.
When you press the 'Send Message' button it will cause the error as descripted in post 1.

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
   
    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",5005,9000)
End Sub
Sub Button2_Click
    If normal_socket.IsInitialized = False Then Return
    SendData("hello")
End Sub
 
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
Changing that seems to of fixed the issue.

Just need help on updating the field in the TableView with the incoming ASCII command.
I can't seem to get it to update the field in the TableView when new incoming data is displayed.
 
Upvote 0
Top