hxb
2021-12-21 8b66be08179b026cb0e601733dacd43de97e5b01
HDLSDK/hdl-socket/src/main/java/com/hdl/sdk/socket/client/TcpClient.java
@@ -15,6 +15,7 @@
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
@@ -31,12 +32,32 @@
    private Socket mSocket;
    private byte[] readBuffer;
    private final static List<TcpClient> tcpClientList = new ArrayList();
    /**
     * 从连接池中找出当前IP及端口的连接客户端
     * @param ipAdderss 连接IP地址
     * @param port 连接端口
     * @return
     */
    public static TcpClient getTcpClientByIP(String ipAdderss,int port) {
        for(TcpClient tcpClient:tcpClientList){
            if(tcpClient.ip.equals(ipAdderss)&&tcpClient.port==port)
            {
                return tcpClient;
            }
        }
        return  null;
    }
    private byte[] readBuffer = new byte[4*1024];
    private TcpClient(String ip, int port, SocketOptions socketOptions) {
        this.socketOptions = socketOptions;
        this.ip = ip;
        this.port = port;
        socketOptions.setIp(ip);
        socketOptions.setPort(port);
    }
    public static SocketBoot init(String ip, int port, SocketOptions options) {
@@ -52,8 +73,11 @@
        mSocket.setTcpNoDelay(true);
        mSocket.setReuseAddress(true);
        mSocket.setKeepAlive(true);
        readBuffer = new byte[options.getReadMaxBufferSize()];
        tcpClientList.add(this);
    }
    @Override
@@ -66,16 +90,6 @@
            }
        }
    }
    @Override
    public boolean isConnect() {
        if (mSocket == null) {
            return false;
        }
        return mSocket.isConnected() && !mSocket.isClosed();
    }
    @Override
    public synchronized SocketOptions getOptions() {