我在尝试使用我的android(Basic4Android)与运行.net TCP服务器的PC进行通信时出现问题。我需要能够有按钮发送4byte命令到服务器并接收回应。当我在android上运行程序时,服务器确实连接并接收到字符串“INFO”,但是直到我重新启动程序并且它只是再次发送命令“INFO”时才发送或接收任何内容。当我按下按钮发送命令时,我没有遇到任何错误,但服务器从未收到任何内容。该服务器是一个用VB.NET编写的Windows窗体多线程程序。我写了一个VB.NET客户端程序,该程序可以作为我正在尝试执行的一个示例。这是我第一次尝试Android应用程序,到目前为止我只是修改了我在教程中找到的网络示例。通过TCP套接字发送和接收数据的问题
的代码如下... 感谢
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("Alarm_Control")
Socket1.Initialize("Socket1")
Socket1.Connect("#.#.#.#" , 8000, 20000) '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
这可能是一个更好的意见,以原来的问题,而答案 - 它实际上并没有试图回答这个问题。 –