Sending TCP data from Android (as client) - no data being sent?

半城伤御伤魂 提交于 2019-11-28 23:03:30

问题


I'm trying to send data from my Android app to my PC over TCP.

The code is as follows:

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");              

I don't get any errors at all while doing this, however, the server application (written in C#) does not get any data. It sees the client connect to it, and sees that data is being sent, however, the data string comes out empty... And thoughts on why this is happening?

PS: The server code is copied from the following site and has been tested with a C# TCP client. http://www.switchonthecode.com/tutorials/csharp-tutorial-simple-threaded-tcp-server


回答1:


Try putting an out.flush();out.close(); after the println(..);




回答2:


I had the same problem and Haphazard's solutions wasn't good enough for me. I think that you should use (in this case) output.flush(); and output.close(); instead of out.flush(); and out.close();. And you have to remember about internet permission in AndroidManifest.xml:

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

after <uses-sdk> and before <application>




回答3:


Total guess this one, but have you tried calling flush() on the output stream before closing?



来源:https://stackoverflow.com/questions/6309201/sending-tcp-data-from-android-as-client-no-data-being-sent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!