zoukankan      html  css  js  c++  java
  • 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();
            }
        }
    }
  • 相关阅读:
    python机器学习-数据集划分
    python机器学习-数据集的使用
    char类型标识字段
    CharIndex的用法
    临时表和表变量性能差别
    建索引数据优化实例
    公用表达式的结果集不要子查询然后连接查询
    Oracle SQL in 超过1000 的解决方案
    Oracle 11g大数据量表快速增加列
    SQL Server表值函数
  • 原文地址:https://www.cnblogs.com/endv/p/13379071.html
Copyright © 2011-2022 走看看