Android Question Connecting multiple clients to a ServerSocket

Status
Not open for further replies.

deantangNYP

Active Member
Licensed User
Longtime User
May i know how to connect multiple clients, having same PORT number, to a ServerSocket.
I could only get 1 client to connect at any time.
May i know what i do wrong?

sorry, i do not understand how to implement the similar in "https://www.b4x.com/android/forum/threads/b4j-cctv-example.34695/#post-203347"

Please advise. Thanks

B4A Server code
B4X:
Sub Process_Globals
    Dim AStreams1 As AsyncStreams
    Dim Server1 As ServerSocket
    Dim Socket1 As Socket
End Sub

Sub Activity_Create(FirstTime As Boolean)

        Server1.Initialize(2222, "Server1")
        Server1.Listen
End If

Sub Server1_NewConnection (Successful As Boolean, NewSocket As Socket)
    If Successful Then
        ToastMessageShow("Connected_Client", False)
        Socket1 = NewSocket
    AStreams1.Initialize(Socket1.InputStream, Socket1.OutputStream, "AStreams1")
    Else
        ToastMessageShow(LastException.Message, True)
    End If
    Server1.Listen
End Sub

Sub AStreams1_NewData (Buffer() As Byte)
    Dim msg As String
    msg = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
    ToastMessageShow(msg, False)
    Log(msg)
End Sub

Sub AStreams1_Error
    ToastMessageShow(LastException.Message, True)
End Sub

Sub SendMessage
    Dim str As String
    str = "Test"
    AStreams1.Write(str.GetBytes("UTF8"))
End Sub

Python RaspberryPi multiple Clients code, all having same port, 2222.
B4X:
import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM )
host = '192.168.43.1'
print(host)
port = 2222
s.connect((host,port))
 
Last edited:

deantangNYP

Active Member
Licensed User
Longtime User
Upvote 0

Ferraz71

Member
Licensed User
Longtime User
the code below works well for me, I can connect multiple clients, receive and send data to each independent

Server multiple client sockets (B4A):
#Region Module Attributes
    #FullScreen: False
    #IncludeTitle: True
    #ApplicationLabel: Server Test
    #VersionCode: 1
    #VersionName:
    #SupportedOrientations: landscape
    #CanInstallToExternalStorage: False
#End Region

'Activity module
Sub Process_Globals
    Dim Server As ServerSocket
    Dim Socket1 As Socket
    Dim PORT As Int=12345
    
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.
    'Dim WebViewExtras1 As WebViewExtras '***************************************************************************
    Dim LBL As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        Server.Initialize(PORT, "Server")
        Server.Listen
    End If
    LBL.Initialize("Label")
    LBL.TextSize=20
    ShowURL("")
    Activity.AddView(LBL, 0,0, 100%x, 100%y)


End Sub

Sub ShowURL(URL As String)
    LBL.Text = "IP: " & Server.GetMyIP & ":" & 12345 & " URL: " & URL
End Sub


Sub Activity_Resume
    Server.Listen
End Sub

Sub Activity_Pause(UserClosed As Boolean)
    Server.Close
    If UserClosed Then
        Socket1.Close
    End If
End Sub

Sub Server_NewConnection (Successful As Boolean, NewSocket As Socket)
    If Successful Then
          Log("CONEXAO: "& NewSocket)
        Dim AStreams As AsyncStreams
        AStreams.Initialize(NewSocket.InputStream, NewSocket.OutputStream, "astream")
        Dim r As Reflector
        r.Target = NewSocket
        r.Target = r.GetField("socket")
        r.Target = r.RunMethod("getInetAddress") 'InetAddress
        LBL.Text= r.RunMethod("getHostAddress")

    Else
        Log(LastException.Message)
    End If
 Server.LISTEN   
End Sub


Sub astream_NewData (Buffer() As Byte)
    Log("NEWDATA"& Sender)
    Dim AStreams As AsyncStreams
    AStreams=Sender
    Dim msg As String
    msg=BytesToString(Buffer, 0, Buffer.Length, "UTF8")
    LBL.Text=LBL.Text & Chr(10) & msg 'Print incoming data............
    Dim msg2 As String
    msg2="Receive is OK"
    AStreams.Write(msg2.GetBytes("UTF8")) 'Response to client..........
    
End Sub

Sub astream_Error
    ToastMessageShow(LastException.Message, True)
End Sub


Sub astream_Terminated
    Log(Sender)
    Dim astream As AsyncStreams = Sender
    astream.Close
End Sub
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

Eric Baker

Member
Licensed User
Longtime User
I think his post was appropriate as it is a proposed solution to the original question asked. It increased the signal to noise ratio in the forum and that is a good thing. One thread less that is a dead end with no solution. Don't be so pedantic.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
It is never appropriate to post in old threads.
The code is also far from being perfect and shouldn't be used. However this is not the place to discuss the mistakes.

Correct implementation of multiple clients: https://www.b4x.com/android/forum/threads/mjpeg-cctv-server.73792/#content (this is not the same as the link in the first post).

Another, more complicated, example is the FTP server example.
 
Upvote 0
Status
Not open for further replies.
Top