zoukankan      html  css  js  c++  java
  • 2016-7-30 TextView自动下滚,以及TCP Client,(更新壹)

    public class MainActivity extends AppCompatActivity {
        private String ip = "192.168.31.162";
        private int port = 20001;
        private TextView _tv;
        String str = "";
        MyApp myApp;
        Connection conn;
        boolean isRun = false;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            _tv = (TextView) this.findViewById(R.id.tv);
            _tv.setMovementMethod(ScrollingMovementMethod.getInstance());
    
            myApp = (MyApp) getApplication();
            conn = new Connection(ip, port, this);
            myApp.connection = conn;
    
            new Thread(new Runnable() {
                @Override
                public void run() {
                    conn.connect();
                    conn.start();
                }
            }).start();
        }
    
    
        public void click_start(View view) {
            if (conn.flag) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String str = "你好服务器";
                        System.out.println("发送给服务器");
                        conn.send(str);
                    }
                }).start();
            }else {
                conn.flag = true;
            }
    
        }
    
        public void showInTv(String text) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    _tv.append("
    " + conn.toUI() + " | " + MyTime.getTime());
    
                    int offset = _tv.getLineCount() * _tv.getLineHeight();
                    if (offset > _tv.getHeight()) {
                        // 更新文字时,使用View的scrollTo(int x,int y)方法
                        // 使其自动滚动到最后一行。
                        _tv.scrollTo(0, offset - _tv.getHeight());
                    }
                }
            });
        }
    
        public class My {
            String mStr;
    
            public String toUI(String string) {
                showInTv(mStr);
                return null;
            }
    
        }
    
        public void click_stop(View view) {
            conn.flag = false;
        }
    
    
    }
    Connection.class
    public class Connection extends Thread {
    
        Socket socket = null;
        DataInputStream reader = null;
        DataOutputStream writer = null;
        public boolean flag = false;
        private String ip;
        private int port;
        String str;
        MainActivity ma;
    
        public Connection(String ip, int port, MainActivity ma) {
            this.ip = ip;
            this.port = port;
            this.ma = ma;
        }
    
        public void connect() {
            try {
                if (socket == null || socket.isClosed()) {
                    socket = new Socket(ip, port);
                    System.out.println("====服务器已连接====");
                    if (writer == null) {
    
                        writer = new DataOutputStream(socket.getOutputStream());
                        reader = new DataInputStream(socket.getInputStream());
    
                        System.out.println("客户端接收/发送, 已准备完毕...");
                    }
                    flag = true;
                }
            } catch (IOException e) {
                try {
                    reader.close();
                    writer.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                e.printStackTrace();
            }
        }
    
        public void disconnect() {
            try {
                flag = false;
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
    
        public void send(String string) {
            try {
                if (socket != null) {
                    while (flag) {
                        writer.writeUTF(string);
                        try {
                            Thread.sleep(3000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        public boolean isConnected() {
            return socket.isConnected();
        }
    
        public void run() {
    
            while (flag) {
                try {
                    str = "服务器: " + reader.readUTF();
                    System.out.println(str);
    
                    ma.showInTv(str);
    
                } catch (IOException e) {
                    flag = false;
                    e.printStackTrace();
                }
            }
    
        }
    }

    服务器

    public static void main(String[] args) {
            final boolean flag = true;
            try {
                ServerSocket server = new ServerSocket(20001);
                System.out.println("Server start...");
    
                new Thread() {
                    public void run() {
                        while (flag) {
                            try {
                                Socket client = server.accept();
                                client.setKeepAlive(true);
                                System.out.println("Client connect...");
                                client.setSoTimeout(1000000);
    
                                DataInputStream reader = new DataInputStream(client.getInputStream());
                                DataOutputStream writer = new DataOutputStream(client.getOutputStream());
    
                                while (flag) {
                                    System.out.println("客户端: " + reader.readUTF() + " | " + new Date(System.currentTimeMillis()));
                                    writer.writeUTF("你好客户端");
                                }
    
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    };
                }.start();
            } catch (Exception e) {
    
                e.printStackTrace();
            }
        }
  • 相关阅读:
    TomCat安装配置教程
    Java桌面程序打包成exe可执行文件
    【android studio】 gradle配置成本地离线zip包
    使用Android Studio过程中,停留在“Building ‘工程名’ Gradle project info”的解决方法
    Android studio启动后卡在refreshing gradle project(包解决)
    Genymotion的安装与使用(附百度云盘下载地址,全套都有,无需注册Genymotion即可使用)
    CodeForcesGym 100735G LCS Revised
    CodeForcesGym 100735D Triangle Formation
    CodeForcesGym 100735B Retrospective Sequence
    HDU 2829 Lawrence
  • 原文地址:https://www.cnblogs.com/juzi-123/p/5721015.html
Copyright © 2011-2022 走看看