SocketClient

Stella981
• 阅读 463
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();
        }
    }
}
点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Wesley13 Wesley13
3年前
java将前端的json数组字符串转换为列表
记录下在前端通过ajax提交了一个json数组的字符串,在后端如何转换为列表。前端数据转化与请求varcontracts{id:'1',name:'yanggb合同1'},{id:'2',name:'yanggb合同2'},{id:'3',name:'yang
待兔 待兔
5个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Wesley13 Wesley13
3年前
java将pdf文件转为word
importjava.io.File;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.OutputStreamWriter;importjava.io.Writer;importorg.apache.pdfbox.pdmode
Wesley13 Wesley13
3年前
java获取前端请求字段的方法
java获取前端请求字段的方法packagetest.request;importjava.io.IOException;importjava.io.InputStream;importjava.lang.reflect.InvocationTargetException;importjava.util.Enume
Wesley13 Wesley13
3年前
TCP
importjava.io.IOException;importjava.io.InputStream;importjava.io.OutputStream;importjava.net.ServerSocket;importjava.net.Socket;/\\ \Socket,serversocket
Wesley13 Wesley13
3年前
Java 操作ini文件(往ini文件中读写数据(key
importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.OutputStream
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Wesley13 Wesley13
3年前
mysql数据库备份
importjava.io.BufferedReader;importjava.io.BufferedWriter;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.IOException;i
Python进阶者 Python进阶者
11个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这