从 Android(作为客户端)发送 TCP 数据 - 没有发送数据? [英] Sending TCP data from Android (as client) - no data being sent?
问题描述
我正在尝试通过 TCP 将数据从我的 Android 应用发送到我的 PC.
I'm trying to send data from my Android app to my PC over 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# 编写)没有获取任何数据.它看到客户端连接到它,并看到正在发送数据,但是,数据字符串显示为空......并思考为什么会发生这种情况?
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:服务器代码复制自以下站点,并已使用 C# TCP 客户端进行测试.http://www.switchonthecode.com/tutorials/csharp-tutorial-simple-threaded-tcp-server
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
推荐答案
尝试将 out.flush();out.close();
放在 println(..)<之后/code>;
Try putting an out.flush();out.close();
after the println(..)
;
这篇关于从 Android(作为客户端)发送 TCP 数据 - 没有发送数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!