• EventBus tcp for android


    package cn.endv.tianyun.tcp;
    
    
    import android.util.Log;
    
    import org.greenrobot.eventbus.EventBus;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.PrintWriter;
    import java.net.Socket;
    
    public class TcpClient {
        public static Socket socket;
    
        public static void startClient(final String address ,final int port){
            if (address == null){
                return;
            }
            if (socket == null) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Log.i("tcp", "启动客户端");
                            socket = new Socket(address, port);
                            Log.i("tcp", "客户端连接成功");
                            PrintWriter pw = new PrintWriter(socket.getOutputStream());
    
                            InputStream inputStream = socket.getInputStream();
    
                            byte[] buffer = new byte[1024];
                            int len = -1;
                            while ((len = inputStream.read(buffer)) != -1) {
                                String data = new String(buffer, 0, len);
                                Log.i("tcp", "收到服务器的数据---------------------------------------------:" + data);
                                EventBus.getDefault().post(new MessageClient(data));
                            }
                            Log.i("tcp", "客户端断开连接");
                            pw.close();
    
                        } catch (Exception EE) {
                            EE.printStackTrace();
                            Log.i("tcp", "客户端无法连接服务器");
    
                        }finally {
                            try {
                                socket.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            socket = null;
                        }
                    }
                }).start();
            }
        }
    
        public static void sendTcpMessage(final String msg){
            if (socket == null ) {
                TcpClient.startClient( "192.168.0.11" , 9002);
                Log.i("tcp", "重启动客户端");
            }
    //        if (socket == null && !socket.isConnected()) {
    //            TcpClient.startClient( "192.168.0.11" , 9002);
    //            Log.i("tcp", "重启动客户端");
    //        }
            if (socket != null && socket.isConnected()) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            socket.getOutputStream().write(msg.getBytes());
                            socket.getOutputStream().flush();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        }
    }
  • 相关阅读:
    Python3安装
    HTML基础
    Python Socket
    python常用模块
    JSON.stringify的三个参数
    判断数组中存在重复元素
    vant-ui表单验证
    如何计算出浏览器的帧数?requestAnimationFrame
    js判断两个区间是否存在交集
    怎么在组件内部判断出是否插入了slot
  • 原文地址:https://www.cnblogs.com/endv/p/13379071.html
Copyright © 2020-2023  润新知