2011-06-10 109 views
11

我試圖通過TCP將我的Android應用程序的數據發送到我的電腦。從Android(作爲客戶端)發送TCP數據 - 沒有數據正在發送?

的代碼如下:

Socket socket = new Socket("10.0.78.75", 50505); 

OutputStream out = socket.getOutputStream();  
PrintWriter output = new PrintWriter(out);   

mStatusText.setText("Sending Data to PC");   
output.println("Hello from Android");    
mStatusText.setText("Data sent to PC");    

socket.close();          
mStatusText.setText("Socket closed");    

我完全不同時這樣做,但是,服務器應用程序(C#編寫)沒有得到任何數據得到任何錯誤。 它看到客戶端連接到它,並看到數據正在發送,但是,數據字符串空出來......並思考爲什麼發生這種情況?

PS:從以下站點複製服務器代碼,並使用C#TCP客戶端進行了測試。 http://www.switchonthecode.com/tutorials/csharp-tutorial-simple-threaded-tcp-server

+0

您的設備上或在仿真器之前?你的清單中是否有互聯網許可? – 2011-06-10 16:23:41

回答

13

試着在println(..)之後放out.flush();out.close();;

+0

這樣做,謝謝! – 2011-06-10 16:42:04

2

總猜猜這一個,但你有沒有試過在關閉之前在輸出流上調用flush()

10

我有同樣的問題,Haphazard的解決方案對我來說還不夠好。我認爲你應該使用(在這種情況下)output.flush();output.close();而不是out.flush();out.close();。而且你要記住網上許可,不得以AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET"></uses-permission> 

<uses-sdk><application>

+0

是的,你是正確的@CookieMonssster,output.flush();和output.close();爲我工作。感謝您在這裏糾正。 – 2013-04-12 06:31:54

相關問題