Hello,
I am trying to make a connection with a server.
I was given the address: abouttimetcp.flyingneurons.io
The port to use is: 40640
After connection I am supposed to send a JSON string.
The server would in return confirm reception.
But apparently there is something wrong as I cannot establish a connection.
Any help will be highly appreciated.
I am trying to make a connection with a server.
I was given the address: abouttimetcp.flyingneurons.io
The port to use is: 40640
After connection I am supposed to send a JSON string.
The server would in return confirm reception.
But apparently there is something wrong as I cannot establish a connection.
Any help will be highly appreciated.
Starter:
#Region Service Attributes
#StartAtBoot: False
#ExcludeFromLibrary: True
#End Region
Sub Process_Globals
Public connected As Boolean
Private client As Socket
Public server As ServerSocket
Private astreams As AsyncStreams
Private const PORT As Int = 40640
Private working As Boolean = True
End Sub
Sub Service_Create
server.Initialize(PORT, "server")
ListenForConnections
End Sub
Private Sub ListenForConnections
Do While working
server.Listen
Wait For Server_NewConnection (Successful As Boolean, NewSocket As Socket)
If Successful Then
CloseExistingConnection
client = NewSocket
If astreams.IsInitialized Then astreams.Close
astreams.Initialize( NewSocket.InputStream, NewSocket.OutputStream,"NewSocket")
UpdateState(True)
End If
Loop
End Sub
Sub Service_Start (StartingIntent As Intent)
End Sub
Public Sub ConnectToServer(Host As String)
Log("Trying to connect to: " & Host)
CloseExistingConnection
Dim client As Socket
client.Initialize("client")
client.Connect(Host, PORT, 10000)
Wait For Client_Connected (Successful As Boolean)
If Successful Then
astreams.Initialize(client.InputStream,client.OutputStream, "astream")
UpdateState (True)
Else
Log("Failed to connect: " & LastException)
End If
End Sub
Public Sub Disconnect
CloseExistingConnection
End Sub
Sub CloseExistingConnection
If astreams.IsInitialized Then astreams.Close
If client.IsInitialized Then client.Close
UpdateState (False)
End Sub
Sub UpdateState (NewState As Boolean)
connected = NewState
CallSub(JSON, "SetState")
End Sub
Sub AStreams_Error
UpdateState(False)
End Sub
Sub AStreams_Terminated
UpdateState(False)
End Sub
Sub AStreams_NewData (Buffer() As Byte)
Log("NewData")
CallSub2(JSON, "NewData", Buffer)
End Sub
Public Sub SendData (data As String)
Dim bc As ByteConverter
Log("Send data")
Log(bc.StringToBytes(data,"UTF-8"))
If connected Then astreams.Write(bc.StringToBytes(data,"UTF-8"))
End Sub
'Return true to allow the OS default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
Return True
End Sub
JSON:
Sub Process_Globals
'Type MyMessage (Name As String, Age As Int, Image() As Byte)
'Type MyMessage (JSon As String)
End Sub
Sub Globals
Private edtIp As EditText
Private lblMyIp As Label
Private btnConnect As Button
Private lblStatus As Label
Private edtAge As EditText
Private edtName As EditText
Private btnSend As Button
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("1")
edtIp.ForceDoneButton = True
edtAge.InputType = edtAge.INPUT_TYPE_NUMBERS
edtAge.Text = 76
edtIp.Text = "abouttimetcp.flyingneurons.io"
edtName.Text = "Johnson"
End Sub
Sub Activity_Resume
SetState
End Sub
Public Sub SetState
btnSend.Enabled = Starter.connected
If Starter.connected Then
btnConnect.Text = "Disconnect"
lblStatus.Text = "Connected"
Else
btnConnect.Text = "Connect"
lblStatus.Text = "Disconnected"
End If
lblMyIp.Text = "My ip: " & Starter.server.GetMyWifiIP
End Sub
Sub btnSend_Click
Dim JSONGenerator As JSONGenerator
JSONGenerator.Initialize(Main.mJSON)
MsgboxAsync(JSONGenerator.ToString & "\n","")
CallSub2(Starter, "SendData",JSONGenerator.ToString)
End Sub
Sub btnConnect_Click
If Starter.connected = False Then
If edtIp.Text.Length = 0 Then
ToastMessageShow("Please enter the server ip address.", True)
Return
Else
CallSub2(Starter, "ConnectToServer", edtIp.Text)
End If
Else
CallSub(Starter, "Disconnect")
End If
End Sub
Public Sub NewData (buffer() As Byte)
edtName.Text = BytesToString(buffer,0,buffer.Length,"UTF-8")
End Sub