Android Question socket or no socket?, this is a problem.

fifiddu70

Well-Known Member
Licensed User
Longtime User
Hello everyone, I need to use 3 smartphone connected to a tablet, each of these devices send data with AsyncStream, use wifi and socket library and AsyncStream, the tablet I use it as a server, my problem is that as soon as one 3 device connects and transmits the data, others can not connect, displaying the message: Socket closed, my question is this: you can connect all three and send from any device the various text messages? or you can connect and disconnect the device to receive data from all three device?
B4X:
#Region Module Attributes
    #FullScreen: False
    #IncludeTitle: True
    #ApplicationLabel: Test Client Socket
    #VersionCode: 2
    #VersionName: 1.1
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
    #BridgeLogger: True
#End Region

'Activity module
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim CltSock As Socket
    Dim Astreams As AsyncStreams
    ' If you install the "SocketServer" on local machine - change this ip to "127.0.0.1"
    Dim ip As String : ip = "192.168.1.13"
   
    Dim port As Int: port = 9092
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 btn_client As Button
    Dim lbl_status As Label
    Dim EditText1 As EditText
    Dim txt_out As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("ClientForm")
    EditText1.Text = ip
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub AStreams_NewData (Buffer() As Byte)
    Dim msg As String
    msg = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
  
    lbl_status.Text = msg
  
End Sub

Sub btn_client_Click
    CltSock.Initialize("Client")
    CltSock.Connect(ip,port,20000)
   
End Sub

Sub Client_Connected(ConStatus As Boolean)
    If ConStatus = True Then
        Msgbox("Connection Successful","")
        Astreams.InitializePrefix(CltSock.InputStream, False, CltSock.OutputStream, "AStreams")
    Else
         Msgbox(LastException.Message, "Error connecting")
       
    End If   

End Sub

Sub EditText1_EnterPressed
    ip = EditText1.Text
End Sub
Sub txt_out_EnterPressed
      If Astreams.IsInitialized = False Then Return
    If txt_out.Text.Length > 0 Then
        Dim sNewLine As String
        sNewLine = txt_out.text & CRLF
        Dim buffer() As Byte
        buffer = sNewLine.GetBytes("UTF8")
        Astreams.Write(buffer)
       
           ToastMessageShow("Sending:" & sNewLine,False)
    End If
End Sub
 

fifiddu70

Well-Known Member
Licensed User
Longtime User
i have added this code and work done,
B4X:
sub checkclose_tick
  if astream.outputqueuesize<>0 then return
  checkclose_tick.enable=false
  astream.close
  socket1.close
end sub
now when receive the stream of first device, whn finish stream the socket and astream is closed, now is possible connect second device for send another message, etc.
 
Upvote 0

frasc

Member
Licensed User
You don't want to close them, just StopListening on Socket1 and start a new socket (Socket2) with it's own object and startlistening on that one.
 
Upvote 0
Top