#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