Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim AStreams As AsyncStreams
Dim Server As ServerSocket
Dim Socket1 As Socket
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.
Private EditText1 As EditText
Private EditText2 As EditText
Private Button1 As Button
End Sub
Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
'Activity.LoadLayout("Layout1")
Activity.LoadLayout("Main")
If FirstTime Then
Server.Initialize(5500, "Server")
Server.Listen
EditText2.text = Server.GetMyIP
End If
Socket1.Initialize("Socket1")
Socket1.Connect("192.168.0.96","5500",0)
If Socket1.Connected = True Then
Button1.Text = "OK"
Else
Button1.Text = "KO"
End If
' EditText1.ForceDoneButton = True
' Activity.AddView(EditText1, 10dip, 10dip, 300dip, 60dip)
End Sub
Sub Activity_Resume
End Sub
Sub Server_NewConnection (Successful As Boolean, NewSocket As Socket)
If Successful Then
ToastMessageShow("Connected", False)
Socket1 = NewSocket
'Can only use prefix mode if both sides of the connection implement the prefix protocol!!!
AStreams.InitializePrefix(Socket1.InputStream, False, Socket1.OutputStream, "AStreams")
Else
ToastMessageShow(LastException.Message, True)
End If
Server.Listen
End Sub
Sub AStreams_NewData (Buffer() As Byte)
Dim msg As String
msg = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
ToastMessageShow(msg, False)
Log(msg)
End Sub
Sub AStreams_Error
ToastMessageShow(LastException.Message, True)
Log("AStreams_Error")
End Sub
Sub AStreams_Terminated
Log("AStreams_Terminated")
End Sub
'press on the Done button to send text
Sub Activity_Pause(UserClosed As Boolean)
If UserClosed Then
Log("closing")
AStreams.Close
Socket1.Close
End If
End Sub
Sub Button1_Click
If AStreams.IsInitialized = False Then Return
If EditText1.Text.Length > 0 Then
Dim buffer() As Byte
buffer = EditText1.Text.GetBytes("UTF8")
AStreams.Write(buffer)
EditText1.SelectAll
Log("Sending: " & EditText1.Text)
End If
End Sub