博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android使用LocalSocket抓取数据
阅读量:5128 次
发布时间:2019-06-13

本文共 3021 字,大约阅读时间需要 10 分钟。

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

贴上一个LocalSocket简单应用

 

[java] 
  1. package com.snowice.local;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.InputStream;  
  5. import java.io.OutputStream;  
  6.   
  7. import android.app.Activity;  
  8. import android.net.LocalServerSocket;  
  9. import android.net.LocalSocket;  
  10. import android.net.LocalSocketAddress;  
  11. import android.os.Bundle;  
  12. import android.util.Log;  
  13. import android.view.View;  
  14. import android.widget.Button;  
  15.   
  16. public class AndroidLocalSocketActivity extends Activity {  
  17.       
  18.     private static final String TAG = "MY_LOCAL_SOCKET";  
  19.       
  20.     /** 开始演示按钮 */  
  21.     private Button button_start;  
  22.     /** 结束演示按钮 */  
  23.     private Button button_end;  
  24.       
  25.         private LocalSocket receiver;   
  26.         private LocalSocket sender;  
  27.     private LocalServerSocket lss;  
  28.       
  29.     /** 数据缓冲大小 */  
  30.     private static final int BUFFER_SIZE = 500000;  
  31.       
  32.     /** 判断是否正在运行 */  
  33.     private boolean running;  
  34.       
  35.     /** 用于计数 */  
  36.     private int i = 1;  
  37.       
  38.     /** Called when the activity is first created. */  
  39.     @Override  
  40.     public void onCreate(Bundle savedInstanceState) {  
  41.         super.onCreate(savedInstanceState);  
  42.         setContentView(R.layout.main);  
  43.                  
  44.         button_start = (Button) findViewById(R.id.button_start);  
  45.         button_end = (Button) findViewById(R.id.button_end);  
  46.           
  47.         // 设置监听事件  
  48.         button_start.setOnClickListener(new Button.OnClickListener(){  
  49.   
  50.             @Override  
  51.             public void onClick(View v) {  
  52.                 receiver = new LocalSocket();  
  53.                 try {  
  54.                     lss = new LocalServerSocket("Local_Socket");  
  55.                       
  56.                     receiver.connect(new LocalSocketAddress("Local_Socket"));  
  57.                     receiver.setReceiveBufferSize(BUFFER_SIZE);  
  58.                     receiver.setSendBufferSize(BUFFER_SIZE);  
  59.                       
  60.                     sender = lss.accept();  
  61.                     sender.setReceiveBufferSize(BUFFER_SIZE);  
  62.                     sender.setSendBufferSize(BUFFER_SIZE);  
  63.                       
  64.                     // 将控制器running设置为true  
  65.                     running = true;  
  66.                       
  67.                     // 启动发送接受线程  
  68.                     new Thread (local_send).start();  
  69.                     new Thread (local_receive).start();  
  70.                 } catch (IOException e) {  
  71. e.printStackTrace();  
  72.                 }  
  73.             }  
  74.         });  
  75.           
  76.         // 取消数据发送  
  77.         button_end.setOnClickListener(new Button.OnClickListener(){  
  78.               
  79.             @Override  
  80.         public void onClick(View v) {  
  81.                 running = false;  
  82.             }  
  83.         });  
  84.     }  
  85.       
  86.     // 发送线程  
  87.     Thread local_send = new Thread (){  
  88.         // 线程运行函数  
  89.         public void run() {  
  90.             OutputStream m_Send = null;  
  91.               
  92.             try {  
  93.                 m_Send = sender.getOutputStream();  
  94.                   
  95.                 while(running) {  
  96.                     byte[] data = ("LOCAL-SOCKET" + i).getBytes();  
  97.                     sender.setSendBufferSize(data.length);  
  98.                     sender.setReceiveBufferSize(data.length);  
  99.                     m_Send.write(data);  
  100.                     m_Send.flush();  
  101.   
  102.                     Thread.sleep(100);  
  103.                     i ++;  
  104.                 }  
  105.                   
  106.                 m_Send.close();  
  107.                 sender.close();  
  108.             } catch (IOException e) {  
  109. e.printStackTrace();  
  110.             } catch (InterruptedException e) {  
  111. e.printStackTrace();  
  112.             }  
  113.         }  
  114.     };  
  115.       
  116.     // 接收线程  
  117.     Thread local_receive = new Thread(){  
  118.         public void run(){  
  119.             InputStream m_Rece = null;  
  120.               
  121.             try {  
  122.                 m_Rece = receiver.getInputStream();  
  123.                   
  124.                 byte[] data;  
  125.                 int receiveLen = 0;  
  126.                   
  127.                 while(running) {  
  128.                     receiveLen = receiver.getReceiveBufferSize();  
  129.                     data = new byte[receiveLen];  
  130.                     m_Rece.read(data);  
  131. Log.i(TAG, "receiver.getReceiveBufferSize()" + receiveLen + " --- "+new String(data) + " ---");  
  132.                     Thread.sleep(1000);  
  133.                       
  134.                     // 将i设为0  
  135.                     i = 0;  
  136.                 }  
  137.                   
  138.                 m_Rece.close();  
  139.                 receiver.close();  
  140.             } catch (IOException e) {  
  141. e.printStackTrace();  
  142.             } catch (InterruptedException e) {  
  143. e.printStackTrace();  
  144.             }  
  145.         }  
  146.     };  
  147. }  

转载于:https://www.cnblogs.com/GoAhead/archive/2012/07/17/2595892.html

你可能感兴趣的文章
Hibernate 的原生 SQL 查询
查看>>
PHP 环境搭建及zabbix安装遇到的一些坑.
查看>>
MYSQL常用函数(系统信息函数)
查看>>
搜索二维矩阵 II(java实现)
查看>>
MP3文件结构及解码概述
查看>>
Install Linux Kernel - AT91SAM9260EK
查看>>
node(4)express 框架 EJS模板,cookie, session的学习
查看>>
第四节 pandas 数据加载
查看>>
MS SQL Server 增删改查
查看>>
[转]Linux必学的60个命令
查看>>
对Tarjan——有向图缩点算法的理解
查看>>
kvc和kvo
查看>>
多条单据,每个单据多条明细,循环时,判断出每个单据的开始
查看>>
org.apache.mina.filter.codec.ProtocolDecoderException: java.nio.BufferOverflowException解决
查看>>
要明白每个变量的语义,尽量避免一个变量有多重语义
查看>>
「Python」字符串操作内置函数
查看>>
css知多少(9)——float下篇
查看>>
[HttpPost] vs [AcceptVerbs(HttpVerbs.Post)]
查看>>
selenium(七)webdriverwait,高级等待,替代sleep
查看>>
docker 部署 flask(一)配置环境及测试
查看>>