android的tcp client

TcpClientConnector

package com.hadisi.socket;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

/**
 * Created by hadisi on 2016/6/28.
 */

public class TcpClientConnector {
    private static TcpClientConnector mTcpClientConnector;
    private Socket mClient;
    private ConnectLinstener mListener;
    private Thread mConnectThread;

    public interface ConnectLinstener {
        void onReceiveData(String data);
    }

    public void setOnConnectLinstener(ConnectLinstener linstener) {
        this.mListener = linstener;
    }

    public static TcpClientConnector getInstance() {
        if (mTcpClientConnector == null)
            mTcpClientConnector = new TcpClientConnector();
        return mTcpClientConnector;
    }

    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 100:
                    if (mListener != null) {
                        mListener.onReceiveData(msg.getData().getString("data"));
                    }
                    break;
            }
        }
    };

    public void creatConnect(final String mSerIP, final int mSerPort) {
        if (mConnectThread == null) {
            mConnectThread = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        connect(mSerIP, mSerPort);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });
            mConnectThread.start();
        }
    }

    /**
     * 与服务端进行连接
     *
     * @throws IOException
     */
    private void connect(String mSerIP, int mSerPort) throws IOException {
        if (mClient == null) {
            mClient = new Socket(mSerIP, mSerPort);
        }
        InputStream inputStream = mClient.getInputStream();
        byte[] buffer = new byte[1024];
        int len = -1;
        while ((len = inputStream.read(buffer)) != -1) {
            String data = new String(buffer, 0, len);
            Message message = new Message();
            message.what = 100;
            Bundle bundle = new Bundle();
            bundle.putString("data", data);
            message.setData(bundle);
            mHandler.sendMessage(message);
        }
    }

    /**
     * 发送数据
     *
     * @param data 需要发送的内容
     */
    public void send(String data) throws IOException {
        OutputStream outputStream = mClient.getOutputStream();
        outputStream.write(data.getBytes());
    }

    /**
     * 断开连接
     *
     * @throws IOException
     */
    public void disconnect() throws IOException {
        if (mClient != null) {
            mClient.close();
            mClient = null;
        }
    }
}

用法

connector = TcpClientConnector.getInstance();
connector.creatConnect("服务端IP地址","服务端端口号");
connector.setOnConnectLinstener(new TcpClientConnector.ConnectLinstener() {
    @Override
    public void onReceiveData(String data) {
        //do somethings.
    }
});
connector.send("要发送的内容");
@Override
protected void onDestroy() {
    super.onDestroy();
    try {
        connector.disconnect();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

确实帮了忙,第一次网络打赏
参考地址:
https://www.jianshu.com/p/338a61ee5b42

nodejs & tcp socket

服务端

var net = require('net');
//模块引入
var listenPort = 8080;//监听端口
var server = net.createServer(function(socket){
  // 创建socket服务端
  console.log('connect: ' +
    socket.remoteAddress + ':' + socket.remotePort);
  socket.setEncoding('binary');
  //接收到数据
  socket.on('data',function(data){
    console.log('client send:' + data);
  });
socket.write('Hello client!\r\n');
 // socket.pipe(socket);
  //数据错误事件
  socket.on('error',function(exception){
    console.log('socket error:' + exception);
    socket.end();
  });
  //客户端关闭事件
  socket.on('close',function(data){
    console.log('client closed!');
     // socket.remoteAddress + ' ' + socket.remotePort);
  });
}).listen(listenPort);
//服务器监听事件
server.on('listening',function(){
  console.log("server listening:" + server.address().port);
});
//服务器错误事件
server.on("error",function(exception){
  console.log("server error:" + exception);
});

客户端

var net = require('net');
var port = 8080;
var host = '127.0.0.1';
var client= new net.Socket();
//创建socket客户端
client.setEncoding('binary');
//连接到服务端
client.connect(port,host,function(){
  client.write('hello server');
  //向端口写入数据到达服务端
});
client.on('data',function(data){
  console.log('from server:'+ data);
  //得到服务端返回来的数据
});
client.on('error',function(error){
//错误出现之后关闭连接
  console.log('error:'+error);
  client.destory();
});
client.on('close',function(){
//正常关闭连接
  console.log('Connection closed');
});

参考地址:
https://blog.csdn.net/lockey23/article/details/76408891

node.js & express

MEAN是啥东西?查询版本

node -v

结果

v8.9.3

建立文件hello.js

console.log('1+1='+(1+1));

运行

node hello.js

创建一个package.json文件

{
  "name": "Hello",
  "description": "hello mikuq.com",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "express": "4.x"
  }
}

然后

npm install

node_modules中会下在40多个包,可以用npm list查看,建立index.js,有中文记得uft8保存,否则会有乱码

var express = require('express');
var app = express();
app.get('/', function (req, res) {
  res.send('<h1>MikuQ的站点</h1>');
});
app.listen(80);

运行node index.js然后浏览器访问http://localhost就可以了

路线2

npm init

全部回车就可以了,自动生成package.json

npm install express --save

或者

npm install express-generator -g
express myapp
cd myapp & npm install
SET DEBUG=myapp:* & npm start

有点儿乱了,http和express有啥区别呢,有没有压力测试,生产环境如何部署?

参考:
http://www.expressjs.com.cn/
http://blog.csdn.net/u012468376/article/details/53245002
https://www.cnblogs.com/stoneniqiu/p/5538109.html

nginx反向代理的dns更新(504 Gateway Time-out)

网站隔三差五的就挂掉,一直以为是球猫在搞鬼,这不,又挂掉了504 Gateway Time-out,本以为是bug已经升级了nginx的,问题依旧。
几次尝试解决,都以失败告终,只能发现后sudo nginx -s reload,总不能放到定时任务中吧,再次查看日志sudo cat /var/log/nginx/error.log。

upstream timed out (110: Connection timed out) while connecting to upstream, upstream: "http://123.123.12.3", host: "mikuq.com"

尝试浏览器中访问这个IP,访问不到,奇怪了,再次确认竟然是过期的IP,参考了几篇文章,是通病,尝试解决,查询本机的dns,写到配置文件中cat /etc/resolv.conf,重起,过几天看效果吧。

参考文献:
https://serverfault.com/questions/240476/how-to-force-nginx-to-resolve-dns-of-a-dynamic-hostname-everytime-when-doing-p
https://www.zhihu.com/question/61786355

在opiz上装docker

最近很喜欢玩docker,所以各种安装写得多一些,参考自己的《玩docker》

uname -a

得到的版本是Linux opiz 4.13.16-sunxi #20 SMP Fri Nov 24 19:50:07 CET 2017 armv7l armv7l armv7l GNU/Linux
运行安装命令

wget -qO- https://get.docker.com/ | sh

可惜报错Could not get lock /var/lib/dpkg/lock,可以用这个命令查看

ps -aux | grep apt

慢慢处理,会好的,安装成功后启动服务,然后需要重起一下

sudo service docker start
sudo usermod -aG docker pi

执行docker info查看版本是18.0.2,然后就可以学习了https://docs.docker.com/get-started/