2011-11-04 241 views
0

我在嘗試使用我的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 

回答

0

你是如何在服務器端讀取數據?請注意,TextWriter.WriteLine寫入一個以chr(10)結尾的行。確保你沒有等待chr(13)和chr(10)。

0

我在列表程序調用的線程中使用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 
0

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(); 
    } 
} 
+1

這可能是一個更好的意見,以原來的問題,而答案 - 它實際上並沒有試圖回答這個問題。 –

相關問題