权限

//网络权限

服务器线程封装类

package com.example.thrdemo;import android.util.Log;import java.io.IOException;import java.io.InputStream;import java.net.ServerSocket;import java.net.Socket;import java.util.HashMap;import java.util.Iterator;import java.util.Map;/** * Created by yk on 2018/7/11. */public class ServerThread extends Thread {    private final String TAG = "ServerThread";    private ServerSocket serverSocket;    private boolean isServerRuning = false; //服务器是否在运行    public Map socketThreadMap = new HashMap<>(); //客户端列表  ip:端口,客户端    private int port = 0;    public ServerThread(int port) {        this.port = port;    }    public void start() {        try {            serverSocket = new ServerSocket(port);            isServerRuning = true;            super.start();            Log.e(TAG, "服务器运行成功");        } catch (IOException e) {            e.printStackTrace();            isServerRuning = false;            Log.e(TAG, "服务器运行失败");        }    }    @Override    public void interrupt() {        isServerRuning = false;        if (socketThreadMap != null) {            Log.e(TAG, "客户端个数:" + socketThreadMap.size());            Iterator iterator = socketThreadMap.keySet().iterator();            while (iterator.hasNext()) {                String key = iterator.next();                socketThreadMap.get(key).interrupt();            }            socketThreadMap.clear();            socketThreadMap = null;        }        Log.e(TAG, "客户端资源释放完毕");        //关闭服务器        if (serverSocket != null) {            try {                serverSocket.close();                serverSocket = null;            } catch (IOException e) {                e.printStackTrace();            }        }        Log.e(TAG, "关闭服务器完毕");        super.interrupt();    }    //获取服务器是否在运行    public boolean isServerRuning() {        return isServerRuning;    }    @Override    public void run() {        super.run();        while (isServerRuning()) {            try {                Socket socket = serverSocket.accept(); //等待客户端连接                Log.e(TAG, "有客户端连接上:" + socket.toString());                if (socket != null) {                    SocketThread socketThread = new SocketThread(socket);                    socketThread.start();                    socketThreadMap.put(socket.getInetAddress() + ":" + socket.getPort(), socketThread);                    if (iWifiRec != null)                        iWifiRec.onRecWifiDataCom(socket, null, 0);                }            } catch (IOException e) {//                e.printStackTrace();            }        }    }    //客户端线程    class SocketThread extends Thread {        Socket socket;        InputStream inputStream;        String key = "";        public SocketThread(Socket socket) {            key = socket.getInetAddress() + ":" + socket.getPort();            this.socket = socket;        }        @Override        public void interrupt() {            if (socket != null) {                try {                    socket.close();                    socket = null;                } catch (IOException e) {                    e.printStackTrace();                }            }            if (inputStream != null) {                try {                    inputStream.close();                    inputStream = null;                } catch (IOException e) {                    e.printStackTrace();                }            }            try {                this.join();            } catch (InterruptedException e) {                e.printStackTrace();            }            super.interrupt();        }        @Override        public void run() {            super.run();            while (socket != null && !socket.isClosed()) {                try {                    Thread.sleep(100);//                    sendByte("".getBytes());                } catch (InterruptedException e) {//                    e.printStackTrace();                }                if (socket != null && !socket.isClosed()) {                    try {                        inputStream = socket.getInputStream();                        int num = inputStream.available();                        byte[] buff = new byte[num];                        int ret = inputStream.read(buff);                        if (ret > 0) {                            if (iWifiRec != null)                                iWifiRec.onRecWifiDataCom(socket, buff, ret);                        }                    } catch (IOException e) {//                        e.printStackTrace();                        Log.e(TAG, "客户端已断开:" + socket);                        this.interrupt();                    }                }            }        }    }    //客户端接口    public interface IWifiRec {        /*-------------------------------------------------------------------------------   1、名称:onRecDataCom   2、参数:socket 客户端            buffer 客户端接收数据            int 客户端接收长度  3、返回值:            void        /********************************************************************************/        void onRecWifiDataCom(Socket socket, byte[] buffer, int len);    }    private IWifiRec iWifiRec;    public void setiWifiRec(IWifiRec iWifiRec) {        this.iWifiRec = iWifiRec;    }}

 

更多相关文章

  1. Android(安卓)transformClassesWithDexForAdh5Debug 的解决方法
  2. 【Android】Android开源项目精选(一)
  3. Android(安卓)service实例
  4. Android中单行横向滑动的日历
  5. android 如何判断程序是否在前台运行
  6. 运行Android(安卓)Studio时,APP安装失败--Installation failed wi
  7. Android判断程序是否第一次运行
  8. android socket
  9. android 状态栏显示运行图标

随机推荐

  1. 【Android】Scrollview 相关问题汇总
  2. eclipse中运行android工程启动失败的问题
  3. android amlogic系统源码中去除屏保
  4. android 申请移动应用的签名生成方法
  5. android查看异常技巧
  6. Eclipse下android相关设置
  7. ProgressBar背景的改变
  8. android framework 低电量关机处理流程
  9. Android(安卓)开发技巧之Log发送UDP报文,S
  10. Android(安卓)滑动效果基础篇(四)—— Gall