B4J Question TCP multiple connection

Hakanunver

Member
Licensed User
Longtime User
We develop tcp server for multiple connections for 20 clients

and searched in forum but solutions are generally single connection how we can do it multiple

B4X:
#Region  Project Attributes
    #MainFormWidth: 1366
    #MainFormHeight: 768
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Dim lblstatus As Label
    Private server As ServerSocket
    Private astream As AsyncStreams
    Dim lblstatus As Label
    Dim List1 As ListView
    Dim convert As ByteConverter
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("main") 'Load the layout file.
    MainForm.Show
 
    server.Initialize(10000, "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  :" & NewSocket.RemoteAddress
    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")
Log(str&"asdasdasdads")
Dim cr As Int
cr = str.LastIndexOf(CRLF)
Log("found cr " & cr)
'If str.Contains(Chr(13)) Then
List1.items.Add(str)
Log(str)
'End If
send_data
End Sub

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

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


Sub send_data
    astream.Write(convert.StringToBytes("1 HAKAN UNVER;","UTF8"))
    Log("data gitti")
End Sub

Edit : When i work only one client this code works fine but when i connect others clients . Reconnect loop begins for clients when one of them connects the server other one disconnect i can achieve only one
 
Last edited:

Hakanunver

Member
Licensed User
Longtime User
Hi @Erel
We have custom devices 15 pcs. This Device need connections low level TCP/IP connections.(not UDP) All device use port : 10000 and work on local networks.

we try Jnetwork library , when run single device no problem. when run multi devices all time connections up and down..
we before used VB winsock multi connections and no problem.

how we implement Jnetwork library like winsock multi connections. or any library.

thanks..
 
Upvote 0
Top