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();
}
}
}

SocketClient的更多相关文章

  1. [Golang] 从零开始写Socket Server(1): Socket-Client框架

    版权声明:本文为博主原创文章,未经博主允许不得转载. 第一次跑到互联网公司实习 ..感觉自己进步飞快啊~第一周刚写了个HTTP服务器用于微信公共号的点餐系统~ 第二周就直接开始一边自学Go语言一边写用 ...

  2. Socketclient与服务端

    package test; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamR ...

  3. socket 一个websocke对应一个socketclient对象

    using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Co ...

  4. 借助Nodejs探究WebSocket

    文章导读: 一.概述-what's WebSocket? 二.运行在浏览器中的WebSocket客户端+使用ws模块搭建的简单服务器 三.Node中的WebSocket 四.socket.io 五.扩 ...

  5. Socket通信综合示例

    1用户注册客户单选择'用户注册',提示要输入用户名,密码,确认密码,校验成功后将用户信息保存到数据库,并提示数据库注册成功,请登录2用户登录客户端选择'登录以后'后,提示输入用户名和密码,验证成功则提 ...

  6. zookeeper原理解析-选举

    1)QuorumPeerMain加载 Zookeeper集群启动的入口类是QuorumPeerMain来加载配置启动QuorumPeer线程.首先我们来看下QuorumPeer, 谷歌翻译quorum ...

  7. .net Socket 通信简单实例(初级入门)

    c/s控制台应用程序,Server.Client分别在两个项目中 服务端 using System; using System.Collections.Generic; using System.Li ...

  8. c# socket 编程

    转   http://www.cnblogs.com/cailangwei/archive/2011/11/21/2258191.html 基于Socket服务器端实现本例主要是建立多客户端与服务器之 ...

  9. AgileEAS.NET SOA 中间件平台.Net Socket通信框架-介绍

    一.前言 AgileEAS.NET SOA 中间件平台是一款基于基于敏捷并行开发思想和Microsoft .Net构件(组件)开发技术而构建的一个快速开发应用平台.用于帮助中小型软件企业建立一条适合市 ...

随机推荐

  1. Dozer映射

    1.为什么要映射 一个映射的框架在一个分层的体系架构中非常有用,特别是你在创建一个抽象的分层去包装一些特殊数据的变化 vs 这些数据传输到其它层(外部服务的数据对象.领域的数据对象.数据传输对象.内部 ...

  2. KDE-解决.docx .xlsx .pptx文档默认由Ark打开的问题

    安装KDE后,默认的压缩解压程序变成了Ark,并且原来默认用WPS Office打开的.docx .xlsx .pptx文档,从文件管理器双击打开时,也变成了用Ark打开. 查了下网上的资料,可通过如 ...

  3. 【Java】生成随机的手机号码并输出到文件

    import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.R ...

  4. vue中 keep-alive 组件的作用

    原文地址 在vue项目中,难免会有列表页面或者搜索结果列表页面,点击某个结果之后,返回回来时,如果不对结果页面进行缓存,那么返回列表页面的时候会回到初始状态,但是我们想要的结果是返回时这个页面还是之前 ...

  5. 使用青花瓷(charles)抓包

    官网下载charles: https://www.charlesproxy.com/download/ MAC & Apple 打开青花瓷charles 找到本地IP:青花瓷里面Help-&g ...

  6. 【POJ - 1950】Dessert(dfs)

    -->Dessert Descriptions: 给你一个数N(3<=N<=15);每个数之间有三种运算符“‘+’,‘-’,‘.’”.输出和值等于零的所有的运算情况及次数num,如果 ...

  7. 解决windows server 2019远程桌面许可证问题

    解决远程桌面许可证问题,你的远程桌面许可证出现问题,你的会话将在60分钟后断开. 最近装了台windows server 2019服务器做远程桌面连接,也安装了远程桌面许可证,但客户端远程连接时出现你 ...

  8. 【转帖】赤壁之战,曹操大败只因缺了Service Mesh

    赤壁之战,曹操大败只因缺了Service Mesh 本文作者把微服务向 Service Mesh 的进化融入到了三国故事中,妙趣横生.故事比较长,大家慢慢看,精彩的在后边. http://develo ...

  9. 基于.NET架构的树形动态报表设计与应用

    在一些统计报表中,利用树形结构报表来实现维度钻取功能是十分常见的.通过逐级钻取,可以快速查看更细粒度的指标数据,如项目施工进度报告等. 使用葡萄城报表控件——ActiveReports ,即可轻松设计 ...

  10. SQL Server 数据备份存储过程

    原文:SQL Server 数据备份存储过程 今天开园,分享一下我一直在使用的数据备份存储过程,欢迎转载!!! ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...