当前位置:   article > 正文

android java socket server端 可以不断的连接断开,不断的收发 TCP转发_安卓java socket 接收发送

安卓java socket 接收发送

在这里插入图片描述

adb.exe forward tcp:5902 tcp:5902
前面本地5901 转发到 后面设备为5902

查看转发
adb forward --list

删除所有转发
adb forward --remove-all
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
    public static final String TAG = "Communicate";
    private static boolean isEnable;
    private final WebConfig webConfig;//配置信息类
    private final ExecutorService threadPool;//线程池
    private ServerSocket socket;
 public void startServerAsync() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                doProcSync();
            }
        }).start();
    }
    private void doProcSync() {
        try {
            InetSocketAddress socketAddress = new InetSocketAddress(webConfig.getPort());
            socket = new ServerSocket();
            socket.setReuseAddress(true);
            socket.bind(socketAddress);
            AppServiceApplication.mHandler.post(new Runnable() {
                @Override
                public void run() {
                    AppUtils.setTextView(MainActivity.getTextViewInfo(), "start server success");
                }
            });
            isEnable = true;
            LogS.e(TAG, "remotePeer...............进入循环等待客户端连接 " + isEnable);
            while (isEnable) {
                final Socket remotePeer = socket.accept();
                LogS.d(TAG, "有一个新连接");
                threadPool.submit(new Runnable() {
                    @Override
                    public void run() {
                            LogS.d(TAG, "接收处理线程 isEnable:" + isEnable);
                            receiveData(remotePeer);
                    }
                });
            }
            LogS.d(TAG, "退出等待客户端循环");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private void receiveData(final Socket remotePeer) {
        try {
            InputStream inputStream = remotePeer.getInputStream();
            byte buffer[] = new byte[2048];
            int len=0;
            while ((len = inputStream.read(buffer)) != -1)  { //阻塞 直到
                String message = new String(buffer).substring(0, len);
                LogS.d(TAG, "接收报文:" + message);
                sendData(remotePeer, message);
                //recvDataDeal(remotePeer,message); //详细处理 并 根据接收报文做出发送等响应
            }
            LogS.d(TAG, "客户端断开连接");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/920827
推荐阅读
相关标签
  

闽ICP备14008679号