Trying to send and receive text to a Java echo server on my cloud linux box. (this will be a key aspect of the app) ; the Java server and client work, test strings go both ways. After a week of work I can now send from an Android client to a Java server and see the server pick up the text in one test program, but unlike the Java client I cant get the Android app to hear the response. No firewalls. I’ve tried all the methods and test code I can find on the site.
If I understand the documentation there is no need, as with Java, to create a looping read structure, but it’s unclear as to how to pick up data from the socket having turned on “listen”; there are many ways to design a program here and without further guidance I could go around in circles for weeks missing success by a small bit of code.
Will (Socket1.InputStream) work by itself or do I need to work with ServerSocket ?
Here's some modified test code I picked up on the site and have worked with. The first example sends successfully to the Java EchoServer. If you can point me to some complete simple working code that listens and displays the echoed text it would help very much.
=================================
Sub Process_Globals
Dim Socket1 As Socket
End Sub
Sub Globals
Dim Button_ARM As Button
Dim Button_STAY As Button
Dim Button_AUTO As Button
Dim Button_OFF As Button
Dim Label_Received As Label
Dim Label_Sent As Label
Dim tr As TextReader
Dim tw As TextWriter
Dim sb As StringBuilder
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("socket32")
Socket1.Initialize("Socket1")
Socket1.Connect("192.168.0.45" , 2222, 2000) 'My IP address goes here
End Sub
Sub Socket1_Connected (Successful As Boolean)
If Successful = False Then
Msgbox(LastException.Message, "Error connecting")
Return
End If
tr.Initialize(Socket1.InputStream)
tw.Initialize(Socket1.OutputStream)
tw.WriteLine("INFO")
Label_Sent.Text = "Sent INFO"
tw.Flush
sb.Initialize
sb.Append(tr.ReadLine)
Label_Received.Text = sb.ToString
'Socket1.Close
End Sub
Sub Button_ARM_Click
tw.WriteLine("O001")
tw.Flush
Label_Sent.Text = "Sent O001"
End Sub
Sub Button_STAY_Click
tw.WriteLine("O002")
tw.Flush
Label_Sent.Text = "Sent O002"
End Sub
Sub Button_OFF_Click
tw.WriteLine("O000")
tw.Flush
Label_Sent.Text = "Sent O000"
End Sub
====================================
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim Socket1 As Socket
Dim ServerSocket1 As ServerSocket
Dim AStreams As AsyncStreams
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 Timer1 As Timer
Dim btnConnect As Button
Dim btnClose As Button
Dim btnTest As Button
Dim lvLog As ListView
Dim lblMyIP As Label
Dim label1 As Label
Dim label2 As Label
Dim label4 As Label
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("socket3")
'Todo: Change typeface of lvLog
' lvlog.Initialize("")
'lvlog.Color ="white"
' lvLog.SingleLineLayout.ItemHeight = 16dip
' lvLog.SingleLineLayout.Label.TextSize = 12
lblMyIP.Initialize("")
label1.text = ServerSocket1.GetMyIP
End Sub
Sub btnTest_Click
Dim Buffer() As Byte
msg = "Hello 2222"
Buffer = msg.GetBytes("UTF8")
Astreams.Write(Buffer)
' lvLog.AddSingleLine ("Sent " & msg)
label2.Text = "Sent " & msg
End Sub
Sub btnConnect_Click
Socket1.Initialize("Socket1")
Socket1.Connect("192.168.0.45",2222,5000)
'lvLog.AddSingleLine ("Do Connect")
End Sub
Sub btnClose_Click
Socket1.Close
' lvLog.AddSingleLine ("Do Close")
label2.Text = "close"
End Sub
Sub Socket1_Connected(Connected As Boolean)As Boolean
If Connected = True Then
AStreams.Initialize(Socket1.InputStream,Socket1.OutputStream,"Astreams")
'lvLog.AddSingleLine ("Is Connected")
label1.text = "connected"
End If
End Sub
Sub Activity_Resume
ServerSocket1.Listen
End Sub
Sub Activity_Pause (UserClosed As Boolean)
If UserClosed Then
Timer1.Enabled = False
Socket1.Close
ServerSocket1.Close 'stop listening
End If
End Sub
Sub ServerSocket1_NewConnection (Successful As Boolean, NewSocket As Socket)
If Successful Then
Socket1 = NewSocket
Timer1.Enabled = True
InputStream1 = Socket1.InputStream
OutputStream1 = Socket1.OutputStream
ToastMessageShow("Connected", True)
Else
Msgbox(LastException.Message, "Error connecting")
End If
ServerSocket1.Listen 'Continue listening to new incoming connections
label4.text = InputStream1
End Su
If I understand the documentation there is no need, as with Java, to create a looping read structure, but it’s unclear as to how to pick up data from the socket having turned on “listen”; there are many ways to design a program here and without further guidance I could go around in circles for weeks missing success by a small bit of code.
Will (Socket1.InputStream) work by itself or do I need to work with ServerSocket ?
Here's some modified test code I picked up on the site and have worked with. The first example sends successfully to the Java EchoServer. If you can point me to some complete simple working code that listens and displays the echoed text it would help very much.
=================================
Sub Process_Globals
Dim Socket1 As Socket
End Sub
Sub Globals
Dim Button_ARM As Button
Dim Button_STAY As Button
Dim Button_AUTO As Button
Dim Button_OFF As Button
Dim Label_Received As Label
Dim Label_Sent As Label
Dim tr As TextReader
Dim tw As TextWriter
Dim sb As StringBuilder
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("socket32")
Socket1.Initialize("Socket1")
Socket1.Connect("192.168.0.45" , 2222, 2000) 'My IP address goes here
End Sub
Sub Socket1_Connected (Successful As Boolean)
If Successful = False Then
Msgbox(LastException.Message, "Error connecting")
Return
End If
tr.Initialize(Socket1.InputStream)
tw.Initialize(Socket1.OutputStream)
tw.WriteLine("INFO")
Label_Sent.Text = "Sent INFO"
tw.Flush
sb.Initialize
sb.Append(tr.ReadLine)
Label_Received.Text = sb.ToString
'Socket1.Close
End Sub
Sub Button_ARM_Click
tw.WriteLine("O001")
tw.Flush
Label_Sent.Text = "Sent O001"
End Sub
Sub Button_STAY_Click
tw.WriteLine("O002")
tw.Flush
Label_Sent.Text = "Sent O002"
End Sub
Sub Button_OFF_Click
tw.WriteLine("O000")
tw.Flush
Label_Sent.Text = "Sent O000"
End Sub
====================================
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim Socket1 As Socket
Dim ServerSocket1 As ServerSocket
Dim AStreams As AsyncStreams
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 Timer1 As Timer
Dim btnConnect As Button
Dim btnClose As Button
Dim btnTest As Button
Dim lvLog As ListView
Dim lblMyIP As Label
Dim label1 As Label
Dim label2 As Label
Dim label4 As Label
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("socket3")
'Todo: Change typeface of lvLog
' lvlog.Initialize("")
'lvlog.Color ="white"
' lvLog.SingleLineLayout.ItemHeight = 16dip
' lvLog.SingleLineLayout.Label.TextSize = 12
lblMyIP.Initialize("")
label1.text = ServerSocket1.GetMyIP
End Sub
Sub btnTest_Click
Dim Buffer() As Byte
msg = "Hello 2222"
Buffer = msg.GetBytes("UTF8")
Astreams.Write(Buffer)
' lvLog.AddSingleLine ("Sent " & msg)
label2.Text = "Sent " & msg
End Sub
Sub btnConnect_Click
Socket1.Initialize("Socket1")
Socket1.Connect("192.168.0.45",2222,5000)
'lvLog.AddSingleLine ("Do Connect")
End Sub
Sub btnClose_Click
Socket1.Close
' lvLog.AddSingleLine ("Do Close")
label2.Text = "close"
End Sub
Sub Socket1_Connected(Connected As Boolean)As Boolean
If Connected = True Then
AStreams.Initialize(Socket1.InputStream,Socket1.OutputStream,"Astreams")
'lvLog.AddSingleLine ("Is Connected")
label1.text = "connected"
End If
End Sub
Sub Activity_Resume
ServerSocket1.Listen
End Sub
Sub Activity_Pause (UserClosed As Boolean)
If UserClosed Then
Timer1.Enabled = False
Socket1.Close
ServerSocket1.Close 'stop listening
End If
End Sub
Sub ServerSocket1_NewConnection (Successful As Boolean, NewSocket As Socket)
If Successful Then
Socket1 = NewSocket
Timer1.Enabled = True
InputStream1 = Socket1.InputStream
OutputStream1 = Socket1.OutputStream
ToastMessageShow("Connected", True)
Else
Msgbox(LastException.Message, "Error connecting")
End If
ServerSocket1.Listen 'Continue listening to new incoming connections
label4.text = InputStream1
End Su