Multi socket

sioconcept

Active Member
Licensed User
Longtime User
Hi,

I would like create a multi-socket application. At the begin, i use only 1 socket but i've some problem because the client ask too quickly different information and my app don't send data correctly. The best way is to create a multisocket application. I can't use Astreams because i can't follow the number of the socket.

Actually, i use this code :

B4X:
Sub Process_Globals
   Dim ServerSocket1 As ServerSocket
   Dim Socket1(255) As Socket
   Dim nbSocket As Int
   Dim ServerPort As Int: ServerPort = 3113
End Sub

Sub ServerSocket1_NewConnection (Successful As Boolean, NewSocket As Socket)

   If Successful = False Then
      Log("Socket not succefull")
      Socket1(nbSocket).Close
      Return
   Else
      nbSocket = nbSocket + 1
      Log("New socket")
      Socket1(nbSocket) = NewSocket
      ServerSocket1.Listen
   End If

End Sub

Sub Socket1_Connected (Successful As Boolean)

   Dim InputStream1 As InputStream
   Dim OutputStream1 As OutputStream
   Dim StrGet As String

   InputStream1 = Socket1(nbSocket).InputStream

   If InputStream1.BytesAvailable > 0 Then
      Dim buffer() As Byte
      StrGet = InputStream1.ReadBytes(buffer, 0, 10)
      Log(Str) '(don't make effect for the moment)
   End if

End Sub

I would like know what's the socket number when it receive some data via the sub Socket1_Connected : with the method sender maybe ?

Imagine a grid with 255 sockets and the state; if you've got ideas to manage correctly some sockets, please reply :sign0163:
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Your code will not work. You should not expect that there will be any data available immediately when the connection is set.

You should use AsyncStreams.
For each socket you open you should also create an AsyncStream object.

I recommend you to use a Class for that. The class will logically connect between the AsyncStreams and the socket (though the socket is not really needed).
 
Upvote 0

sioconcept

Active Member
Licensed User
Longtime User
Can i hve a little exemple ? Because i don't understand how the AStreams can reconize who is the good calling socket.

My problem is it :

ServerSocket receive 2 task, so 2 sockets are create
"New task 1"
"New task 2"
And sockets don't know if data are for the first or the second.

Thans Master
 
Upvote 0
Top