Android使用LocalSocket抓取数据

LocalSocket类描述:在Unix域名下创建一个(非服务器)Socket,这种Socket和java.net.Socket不同

贴上一个LocalSocket简单应用


package com.snowice.local;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.app.Activity;
import android.net.LocalServerSocket;
import android.net.LocalSocket;
import android.net.LocalSocketAddress;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class AndroidLocalSocketActivity extends Activity {
	
	private static final String TAG = "MY_LOCAL_SOCKET";
	
	/** 开始演示按钮 */
	private Button button_start;
	/** 结束演示按钮 */
	private Button button_end;
	
        private LocalSocket receiver; 
        private LocalSocket sender;
	private LocalServerSocket lss;
	
	/** 数据缓冲大小 */
	private static final int BUFFER_SIZE = 500000;
	
	/** 判断是否正在运行 */
	private boolean running;
	
	/** 用于计数 */
	private int i = 1;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
               
        button_start = (Button) findViewById(R.id.button_start);
        button_end = (Button) findViewById(R.id.button_end);
        
        // 设置监听事件
        button_start.setOnClickListener(new Button.OnClickListener(){

			@Override
			public void onClick(View v) {
			    receiver = new LocalSocket();
			    try {
					lss = new LocalServerSocket("Local_Socket");
					
					receiver.connect(new LocalSocketAddress("Local_Socket"));
					receiver.setReceiveBufferSize(BUFFER_SIZE);
					receiver.setSendBufferSize(BUFFER_SIZE);
					
					sender = lss.accept();
					sender.setReceiveBufferSize(BUFFER_SIZE);
					sender.setSendBufferSize(BUFFER_SIZE);
					
					// 将控制器running设置为true
					running = true;
					
					// 启动发送接受线程
					new Thread (local_send).start();
					new Thread (local_receive).start();
				} catch (IOException e) {
e.printStackTrace();
				}
			}
		});
        
        // 取消数据发送
        button_end.setOnClickListener(new Button.OnClickListener(){
        	
        	@Override
		public void onClick(View v) {
        		running = false;
        	}
        });
    }
    
    // 发送线程
    Thread local_send = new Thread (){
    	// 线程运行函数
    	public void run() {
    		OutputStream m_Send = null;
    		
    		try {
				m_Send = sender.getOutputStream();
				
				while(running) {
					byte[] data = ("LOCAL-SOCKET" + i).getBytes();
					sender.setSendBufferSize(data.length);
					sender.setReceiveBufferSize(data.length);
					m_Send.write(data);
					m_Send.flush();

					Thread.sleep(100);
					i ++;
				}
				
				m_Send.close();
				sender.close();
			} catch (IOException e) {
e.printStackTrace();
			} catch (InterruptedException e) {
e.printStackTrace();
			}
    	}
    };
    
    // 接收线程
    Thread local_receive = new Thread(){
    	public void run(){
    		InputStream m_Rece = null;
    		
    		try {
				m_Rece = receiver.getInputStream();
				
				byte[] data;
				int receiveLen = 0;
				
				while(running) {
					receiveLen = receiver.getReceiveBufferSize();
					data = new byte[receiveLen];
					m_Rece.read(data);
Log.i(TAG, "receiver.getReceiveBufferSize()" + receiveLen + " --- "+new String(data) + " ---");
					Thread.sleep(1000);
					
					// 将i设为0
					i = 0;
				}
				
				m_Rece.close();
				receiver.close();
			} catch (IOException e) {
e.printStackTrace();
			} catch (InterruptedException e) {
e.printStackTrace();
			}
    	}
    };
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值