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:
The Code I am using is:
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 ?
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 ?