SocketClient

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class SocketClient {
    public Socket socket = null;
    public OutputStream outputStream = null;
    public OutputStreamWriter outputStreamWriter = null;
    public BufferedWriter bufferWrite = null;
    public InputStream inputStream = null;
    public SocketState socketState = SocketState.Closed;
    public Thread socketThread = null;
    public List<String> protocolList = new ArrayList();
    private static boolean closeSocket = false;

    public void closeSocket() {
        closeSocket = true;
        socketThread = null;
    }

    public void connectSocket() {
        startThread();
    }

    public void sendProtocolAPI(String protocol) {
        protocolList.add(protocol);
    }

  
    private void startThread() {
        if (socketThread == null) {
            socketThread = new Thread() {
                public void Recivce(String protocol) {
                    //Protocol.ParseProtocol(protocol);
                }
                public void run() {
                    byte[] buffer = new byte['?'];
                    while (true) {
                        try {
                            if (closeSocket) {
                                closeSocket = false;
                                try {
                                    if (socket != null) {
                                        socket.close();
                                    }
                                } catch (IOException localIOException1) {
                                }
                                socket = null;
                                outputStream = null;
                                outputStreamWriter = null;
                                inputStream = null;
                                System.gc();
                                socketState = SocketState.Closed;
                                break;
                            }
                            if (SocketState.Connected != socketState) {
                                socketState = SocketState.Connecting;
                                socket = new Socket();
                                InetSocketAddress ipa = new InetSocketAddress(Config.IP, Config.Port);
                                socket.connect(ipa, 500);
                                outputStream = socket.getOutputStream();
                                outputStreamWriter = new OutputStreamWriter(outputStream);
                                bufferWrite = new BufferedWriter(outputStreamWriter);
                                inputStream = socket.getInputStream();
                                socket.setTcpNoDelay(true);
                                socket.setKeepAlive(true);
                                socketState = SocketState.Connected;
                                Thread.sleep(1000L);
                            }
                            if (SocketState.Connected == socketState) {
                                if (protocolList.size() > 10) {
                                    protocolList.removeAll(protocolList);
                                } else if (protocolList.size() > 0) {
                                    while (protocolList.size() > 0) {
                                        bufferWrite.write(protocolList.get(0));
                                        bufferWrite.flush();
                                        protocolList.remove(0);
                                        Config.PaySDKAPI.ReceiveTcpLinkAPI(Config.IP, Config.Port);
                                    }
                                }
                            }
                        } catch (Exception e) {
                            socketState = SocketState.Closed;
                            try {
                                Thread.sleep(5000L);
                            } catch (InterruptedException localInterruptedException) {
                            }
                        }
                        try {
                            if ((inputStream != null) && (SocketState.Connected == socketState)) {
                                int readSize = inputStream.read(buffer);
                                if (readSize > 0) {
                                    Recivce(new String(buffer, 0, readSize));
                                }
                            }
                        } catch (IOException e) {
                            socketState = SocketState.Closed;
                        }
                    }
                }
            };
            socketThread.start();
        }
    }
}
原文地址:https://www.cnblogs.com/zclaude/p/11590089.html