B4J Question Multi-Connection to SocketServer

jinyistudio

Well-Known Member
Licensed User
Longtime User
Hi

If multi-client(Server_NewConnection) connect to server as following, How to know which socket's data is receiving When astream_NewData has received data from multi-client(NewSocket) ?

B4X:
Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("1")
    MainForm.Show
    MainForm.BackColor = fx.Colors.White

    server.Initialize(17178, "server")
    server.Listen
    lblIP.Text = "My IP: " & server.GetMyIP
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")
        lblStatus.Text = "Status: Connected"
    Else
        Log(LastException)  
    End If
    server.Listen
End Sub

Sub astream_NewData (Buffer() As Byte)
    Log("received: " & DateTime.GetSecond(DateTime.Now))
    Dim In As InputStream
    Log(Buffer.Length)
    In.InitializeFromBytesArray(Buffer, 0, Buffer.Length)
    Dim img As Image
    img.Initialize2(In)
    ImageView1.SetImage(img)
End Sub
 
Top