I Can't Manage Multiple Client for two weeks any one can help me to modify my project
code main b4a
code starter
code b4j main
code Class module
code main b4a
B4X:
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Private xui As XUI
Type Information(LabelName As String,spl As String,icon () As Byte,id As String,name As String,version As String,Model As String )
Type cmd(spl As String,code As String )
End Sub
code starter
B4X:
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim p As Phone
Dim socket1 As Socket
Dim str As AsyncStreams
Dim ser As B4XSerializator
Dim tmr As Timer
End Sub
Sub tmr_Tick
' If socket1.IsInitialized Then socket1.Close
' If str.IsInitialized Then str.Close
socket1.Initialize("socket1")
socket1.Connect("192.168.1.2",1111,0)
End Sub
Sub Service_Create
'This is the program entry point.
'This is a good place to load resources that are not specific to a single activity.
tmr.Initialize("tmr",1000)
tmr.Enabled=True
End Sub
Sub socket1_Connected (Successful As Boolean)
If Successful Then
If str.IsInitialized Then str.Close
str.InitializePrefix(socket1.InputStream,False, socket1.OutputStream,"str")
tmr.Enabled=False
Dim info As Information
info.Initialize
info.spl="info"
info.icon=ImageToBytes(Application.Icon)
info.id=p.GetSettings("android_id")
info.LabelName=Application.LabelName
info.name=Application.PackageName
info.version=Application.VersionCode
info.Model=p.Model
str.Write(ser.ConvertObjectToBytes(info))
Else
tmr.Enabled=True
End If
End Sub
Public Sub ImageToBytes(Image As B4XBitmap) As Byte()
Dim out As OutputStream
out.InitializeToBytesArray(0)
Image.WriteToStream(out, 100, "JPEG")
out.Close
Return out.ToBytesArray
End Sub
Sub Service_Start (StartingIntent As Intent)
Service.StopAutomaticForeground 'Starter service can start in the foreground state in some edge cases.
End Sub
Sub str_Error
Log("str_Error")
str_Terminated
End Sub
Sub str_Terminated
Log("str_Terminated")
tmr.Enabled=True
End Sub
code b4j main
B4X:
Sub Process_Globals
Private fx As JFX
Private MainForm As Form
Private xui As XUI
Dim svr As ServerManager
Dim socket1 As Socket
Dim ser As B4XSerializator
Type Information(LabelName As String,spl As String,icon () As Byte,id As String,name As String,version As String,Model As String )
Type cmd(spl As String,code As String )
Dim TblMain As TableView
Dim info As Information
' Dim list As List
Dim con As Map
Dim Label1 As Label
End Sub
Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.RootPane.LoadLayout("Layout1")
MainForm.Show
TblMain.SetColumns(Array As String("Icon","ID","Pkg","Ver","Model"))
For i =0 To TblMain.ColumnsCount-1
TblMain.SetColumnWidth(i,TblMain.Width/5)
Next
con.Initialize
' list.Initialize
svr.Initialize
End Sub
code Class module
B4X:
Sub Class_Globals
Private fx As JFX
Dim server As ServerSocket
Dim newstr As AsyncStreams
Dim str As AsyncStreams
Dim ClientInfo As List
Dim Connectedclients As List
End Sub
'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
server.Initialize(1111,"server")
Connectedclients.Initialize
ClientInfo.Initialize
server.Listen
End Sub
Sub server_NewConnection (Successful As Boolean, NewSocket As Socket)
If Successful Then
Log("connected")
If str.IsInitialized Then str.Close
str.InitializePrefix(NewSocket.InputStream,False,NewSocket.OutputStream,"str")
newstr=str
Connectedclients.Add(newstr)
Else
Log("wait....")
Log(LastException.Message)
server.Listen
Log("listen")
End If
server.Listen
End Sub
Sub str_Error
Log("str_Error")
str_Terminated
End Sub
Sub str_Terminated
Log("str_Terminated")
disconnect(str)
Return
End Sub
Sub ConnectedClient As Int
Return Connectedclients.Size
End Sub
Sub disconnect(s As AsyncStreams)
If Main.con.ContainsKey(s) Then
Log(s)
For Each v1 As List In Main.con.Values
For Each v As Information In v1
Log(v.id)
Next
Next
Log("...................................")
' Log(Main.con.Keys)
Log(Main.con.Values)
Main.con.Remove(s)
' Log(Main.con.Keys)
' Log(Main.con.Values)
For Each v1 As List In Main.con.Values
For Each v As Information In v1
Log(v.id)
Next
Next
UpdateTbleView
Else
Log("no")
End If
End Sub
Public Sub BytesToImage(bytes() As Byte) As B4XBitmap
Dim in As InputStream
in.InitializeFromBytesArray(bytes, 0, bytes.Length)
Dim bmp As Image
bmp.Initialize2(in)
Return bmp
End Sub
Sub UpdateTbleView
Main.TblMain.Items.Clear
If Main.con.Size>0 Then
For Each info As List In Main.con.Values
For Each s As Information In info
Dim img As ImageView
img.Initialize("")
img.SetSize(60,60)
img.SetImage(BytesToImage(s.icon))
Dim row(5) As Object
row(0)=img
row(1)=s.id
row(2)=s.name
row(3)=s.version
row(4)=s.Model
Main.TblMain.Items.Add(row)
Next
Next
Else
Log(Main.con.Size)
End If
ClientInfo.Clear
Main.Label1.Text="Clients Connected {"&ConnectedClient &"}"
ClientInfo.Clear
End Sub
Sub str_NewData (Buffer() As Byte)
Main.info=Main.ser.ConvertBytesToObject(Buffer)
Dim value As String = Main.info.spl
Select value
Case "info"
Log(newstr)
ClientInfo.Add(Main.info)
Main.con.Put(newstr,ClientInfo)
UpdateTbleView
Case "Msg"
Log(value)
Case "snd"
Log(value)
Case "Run"
Log(value)
Case "Close"
Log(value)
End Select
End Sub