Server Code
i am doing fine ? or i should be careful more ?
B4X:
Sub Class_Globals
Private server As ServerSocket
Private clients As List
Public cuniqueid As Int
Private ClientTimer As Timer
End Sub
'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(port As Int)
Log("Server Initialized")
cuniqueid = 10
server.Initialize(port, "server")
clients.Initialize
ClientTimer.Initialize("ConnectionCheck",2000)
ClientTimer.Enabled = True
server.Listen
End Sub
Private Sub ConnectionCheck_tick
If clients.Size > 0 Then
For i = clients.Size -1 To 0 Step -1
Dim aClient As TcClient
Dim ctimenow As Long
Dim afloortime As Double
aClient = clients.Get(i)
ctimenow = DateTime.Now
afloortime = Floor((ctimenow - aClient.iamlive) / DateTime.TicksPerSecond)
If (afloortime >= 75) And (aClient.iamconnected = "YES") Then
aClient.iamconnected = "NO"
Log("Remove Dead Connection "&aClient.userid)
aClient.diconnectme
Exit
End If
Next
End If
End Sub
Private Sub Server_NewConnection (Successful As Boolean, NewSocket As Socket)
If Successful Then
Dim c As TcClient
c.Initialize(NewSocket, Me)
c.userip = NewSocket.RemoteAddress
c.iamlive = DateTime.Now
c.iamconnected = "YES"
c.userid = cuniqueid
cuniqueid = cuniqueid + 1
Log("New Connection "&c.userip& " id "&cuniqueid)
clients.Add(c)
End If
server.Listen
End Sub
Public Sub getMyIp As String
Return server.GetMyIP
End Sub
Public Sub getNumberOfClients As Int
Return clients.Size
End Sub
Public Sub ClientDisconnected (aclient As TcClient)
Dim i As Int = clients.IndexOf(aclient)
If i > -1 Then
aclient.iamconnected = "NO"
clients.RemoveAt(i)
End If
End Sub
Public Sub SendTome(aclient As TcClient, data As String)
aclient.SendData(data)
End Sub
Public Sub SendCommandToAll(data As String)
If clients.Size > 0 Then
For i = clients.Size -1 To 0 Step -1
Dim aClient As TcClient
aClient = clients.Get(i)
If (aClient.iamconnected = "YES") Then
aClient.SendData(data)
End If
Next
End If
End Sub
B4X:
Sub Class_Globals
Private astream As AsyncStreamsText
Private Aserver As Tcserver
Dim userid As String
Dim iamlive As Long
Dim iamconnected As String
Dim ConnectionTime As String
End Sub
'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(socket As Socket, Serv As Tcserver)
Log("New connection")
Aserver = Serv
astream.Initialize(Me, "AStream", socket.InputStream, socket.OutputStream)
End Sub
Public Sub SendData(data As String)
astream.Write(data)
End Sub
Public Sub NewData (data As String)
Dim paramset() As String
Dim paramcount As Int
Dim Command As String
paramset = Regex.Split("\|", data)
paramcount = paramset.Length
If paramcount > 0 Then
Command = paramset(0)
End If
If Command = "isalive" Then
updatelivetime
End If
If Command = "close" Then
CallSub(Main, "CloseTheserver")
End If
If Command = "Login" Then
If paramcount = 2 Then
ConnectionTime = DateTime.Time(DateTime.Now)
End If
End If
If Command = "msg" Then
If paramcount = 2 Then
Aserver.SendCommandToAll("msg|")
End If
End If
End Sub
Private Sub updatelivetime
iamlive = DateTime.Now
End Sub
Public Sub AStream_NewText(Text As String)
NewData(Text)
End Sub
Private Sub AStream_Terminated
AStream_Error
End Sub
Private Sub AStream_Error
Aserver.ClientDisconnected(Me)
astream.Close
End Sub
Public Sub diconnectme
AStream_Error
End Sub
i am doing fine ? or i should be careful more ?