【问题标题】:Problems sending and receiving data over TCP socket通过 TCP 套接字发送和接收数据的问题
【发布时间】:2012-04-03 09:11:57
【问题描述】:

我在尝试使用我的 android (Basic4Android) 与运行 .net TCP 服务器的 PC 通信时遇到问题。我需要能够拥有向服务器发送 4 字节命令并接收响应的按钮。当我在android上运行程序时,服务器确实连接并接收到字符串“INFO”,但在我重新启动程序之前没有其他任何发送或接收,它只再次发送命令“INFO”。当我按下按钮发送命令时,我没有收到任何错误,但服务器从未收到任何内容。服务器是一个用VB.NET编写的Windows窗体多线程程序。我编写了一个可以工作的 VB.NET 客户端程序,我可以附上它作为我正在尝试做的事情的示例。这是我第一次尝试 Android 应用程序,到目前为止,我只是在修改教程中找到的网络示例。

代码如下... 谢谢

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

【问题讨论】:

    标签: android .net visual-studio-2010 basic4android


    【解决方案1】:

    你是如何读取服务器端的数据的?请注意,TextWriter.WriteLine 写入以 chr(10) 结尾的行。确保您没有在等待 chr(13) 和 chr(10)。

    【讨论】:

      【解决方案2】:

      我在侦听器调用的线程中使用networkStream.readnetworkStream.write。 vb.net 客户端程序与 vb.net 服务器正常工作。

      Public Sub handlerThread()
          Dim handlerSocket As Socket
          handlerSocket = alSockets(alSockets.Count - 1)
          Dim networkStream As NetworkStream = New NetworkStream(handlerSocket)
          Dim bytes(4) As Byte
          networkStream.Read(bytes, 0, CInt(4))        ' Read the stream into 4-byte array
          Dim clientdata As String = Encoding.ASCII.GetString(bytes) 
          Label_Received.Text = clientdata
          Dim responseString As String = "Received " + clientdata
          Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(responseString)
          networkStream.Write(sendBytes, 0, sendBytes.Length)
          Label_Sent.Text = responseString
          handlerSocket = Nothing
      End Sub
      

      【讨论】:

        【解决方案3】:

        Chr(10) 在这里似乎不是问题。 我在使用这个 java 服务器代码时遇到了同样的问题:

        import java.io.*;
        import java.net.*;
        
        public class ec192 {
          public static void main(String[] args) throws IOException {
        
            Socket echoSocket = null;
            PrintWriter out = null;
            BufferedReader in = null;
        
            try {
        
          echoSocket = new Socket("192.168.0.45", 2222);
              out = new PrintWriter(echoSocket.getOutputStream(), true);
              in = new BufferedReader(new InputStreamReader(
                                      echoSocket.getInputStream()));
            } catch (UnknownHostException e) {
              System.err.println("Don't know about host: taranis.");
              System.exit(1);
            } catch (IOException e) {
              System.err.println("Couldn't get I/O for "
                                 + "the connection to: 2222 taranis.");
              System.exit(1);
            }
        
        BufferedReader stdIn = new BufferedReader(
                                       new InputStreamReader(System.in));
        String userInput;
        
            while ((userInput = stdIn.readLine()) != null) {
            out.println(userInput);
            System.out.println("echo: " + in.readLine());
        }
        
        out.close();
        in.close();
        stdIn.close();
        echoSocket.close();
          }
        }
        

        【讨论】:

        • 作为对原始问题的评论而不是作为答案,这可能会更好——它实际上并不试图回答问题。
        猜你喜欢
        • 2014-07-25
        • 2012-02-12
        • 1970-01-01
        • 2010-09-24
        • 1970-01-01
        • 2021-09-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多