zoukankan      html  css  js  c++  java
  • Android使用LocalSocket抓取数据

    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. }  
  • 相关阅读:
    Py修行路 python基础 (二十五)线程与进程
    Py修行路 python基础 (二十一)logging日志模块 json序列化 正则表达式(re)
    Py修行路 python基础 (二十四)socket编程
    Py修行路 python基础 (二十三)模块与包
    Py修行路 python基础 (二十二)异常处理
    Py修行路 python基础 (二十)模块 time模块,random模块,hashlib模块,OS及sys模块
    Py修行路 python基础 (十九)面向对象进阶(下)
    Oracle数据库的下载和安装
    单体测试详解
    单体测试书的检查要点
  • 原文地址:https://www.cnblogs.com/GoAhead/p/2595892.html
Copyright © 2011-2022 走看看