您的位置:首页 > 移动开发 > Android开发

给予Socket一个android日志收集程序 客户端调用DEMO

2014-12-05 20:09 549 查看
服务端的EXE程序在文章:http://www.shijieweb.cn/post/1cc4ef53_40fc277中.

此文描述ANDROID客户端相关代码.

JAVA客户端DEMO代码:

new SendPcLog(log).start();

package cn.sj.soft;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;import
java.net.SocketTimeoutException;
import java.net.URLEncoder;

/**

* 发送日志文件到PC 定位错误 android
* @author zw
*/

public class SendPcLog extends Thread {
private static String HOST = "192.168.1.88";
private static int PORT = 19730;
static String buffer = "";
static Socket socket = null;
public String txt1;
public SendPcLog(String str) {
txt1 = android.os.Build.MODEL+":"+str;
}

@Override
public void run() {

//定义消息
try {
//连接服务器 并设置连接超时为5秒
socket = new Socket();
socket.connect(new InetSocketAddress(HOST, PORT), 5000);
//获取输入输出流
OutputStream ou = socket.getOutputStream();
//向服务器发送信息
ou.write(URLEncoder.encode(txt1, "utf-8").getBytes());
ou.flush();
ou.close();
socket.close();
} catch (SocketTimeoutException aa) {
// bundle.putString("msg", "服务器连接失败!请检查网络是否打开");

} catch (IOException e) {
e.printStackTrace();

}

}

}



你也可以这样:
socket = new Socket();
socket.connect(new InetSocketAddress(HOST, PORT), 5000);
OutputStream ou = socket.getOutputStream();
while (Tools.isfalse) {
if(Tools.list.size()>0){
ou.write(URLEncoder.encode(Tools.list.get(0), "utf-8").getBytes());
System.out.println(Tools.list.get(0)+"|");
Tools.list.remove(0);
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
ou.flush();
ou.close();
socket.close();
public static List<String> list=new ArrayList<String>();
public static Boolean isfalse=true;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐