如何处理TCP数据? [英] How to handle TCP data?
问题描述
我做它不断地从TCP套接字接收数据的项目。
我需要这些数据传递到另一个活动,这样我可以完成我的行动。
数据传递每一次传球意图是无用的。
我们可以使用定时器,但他们可能会丢失一些数据。
我需要一些过程是怎样的,就应该不断通过它接收数据的瞬间。
请建议我这是在这种情况下处理TCP数据的最佳方法。
我觉得更感谢全,如果你给我一个例子。
下面是我的TCP连接,并接受code
=插座新的Socket(我的IP,端口);
而(s.isConnected()){
在=新的BufferedReader(
新的InputStreamReader(s.getInputStream()));
INT NREAD;
的char []数据=新的char [1024 + 1024];
字符串德codeD =;
而((NREAD = in.read(数据,0,data.length))!= - 1){
德codeD =新的String(数据,0,NREAD); ** //这种去codeD数据需要传递给另一个活动。**
}
你有没有尝试在看AIDL?下面是一些链接
AIDL极品教程
http://developer.android.com/guide/components/aidl.html
I am doing a project which continuously receives data from TCP socket. I need to pass that data to another activity so that I can perform my action. For passing data every time intent passing is useless. We can use timer but their could be some data loss. I need some process like, it should through data continuously in the moment it receives. please suggest me which is the best process to handle TCP data in this case. I feel more thank full if you give me an example.
Here is my TCP connection and receiving code
Sockets = new Socket("My IP",Port);
while (s.isConnected()) {
in = new BufferedReader(
new InputStreamReader(s.getInputStream()));
int nRead;
char[] data = new char[1024 + 1024];
String decoded = "";
while ((nRead = in.read(data, 0, data.length)) != -1) {
decoded = new String(data, 0, nRead); **// This decoded data need to pass to another activity.**
}
Did you try looking at AIDL? Below are some links
Need AIDL tutorials http://developer.android.com/guide/components/aidl.html
这篇关于如何处理TCP数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!