zoukankan      html  css  js  c++  java
  • 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();
            }
        }
    }
  • 相关阅读:
    Python 以指定列宽格式化字符串
    Windows环境下QWT安装及配置
    iOS用户体验之-modal上下文
    android-调用系统的ContentPrivder获取单张图片实现剪切做头像及源代码下载
    Codeforces Round #253 (Div. 1) A Borya and Hanabi
    剑指offer 面试题9
    Memcache应用场景介绍
    [Unity-22] Coroutine协程浅析
    ZOJ 2706 Thermal Death of the Universe (线段树)
    为什么不建议用Table布局
  • 原文地址:https://www.cnblogs.com/zclaude/p/11590089.html
Copyright © 2011-2022 走看看