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. 一百零七:CMS系统之权限和角色模型定义

    模型与权限关系映射表 class CMSPersmission: """ 权限管理映射 """ # 255的二进制方式来表示 1111 11 ...

  2. ES 可视化工具

    1.ElasticSearch Head 用途:展示ES(集群)数据信息 视图:https://mobz.github.io/elasticsearch-head/ 下载:https://github ...

  3. Kafka管理与监控——查看和重设消费者组位移

    kafka 0.11.0.0版本丰富了kafka-consumer-groups脚本的功能,用户可以直接使用该脚本很方便地为已有的consumer group重新设置位移. 前提必须consumer ...

  4. nginx的负载均衡和反向代理

    本文介绍一些负载均衡和反向代理的一些基本概念,然后介绍如何基于nginx实现,包括两种安装nginx的方法:yum安装和源码安装,以及ngix该如何配置等. 什么是负载均衡? 概念 负载均衡是高可用网 ...

  5. CentOS8 使用 aliyun 阿里云 镜像站点的方法

    CentOS8现已可使用国内的aliyun阿里云镜像站,方法如下: 用cd命令切换到yum.repos目录,备份原始的3个repo文件:cd /etc/yum.repos.d/sudo cp Cent ...

  6. Swift 3.0 闭包的定义和使用

    // // ViewController.swift // 闭包的定义和使用 // // Created by 思 彭 on 16/9/17. // Copyright © 2016年 思 彭. Al ...

  7. python基础知识(属性property)

    属性property property               !=          类属性和实例属性 访问计算后所得的值           返回所存储的值 创建用于计算的属性 @proper ...

  8. linux下配置maven并修改maven源

    参考文章 <Linux下Maven的安装与使用> <aliyun阿里云Maven仓库镜像地址> <maven国内镜像配置(Ubuntu)> 下载maven,具体目录 ...

  9. selenium实现京东商城首页查找元素

    语言:Python 京东商城想要找到某个未加载出的元素,需要将页面加载出来. 使用js实现 流程: 1. 需要引入包: # 引入需要的包 import time from selenium impor ...

  10. ULB

    https://docs.ucloud.cn/network/ulb/intro/whatisulb 创建ulb ulb里创建vserver, 设置监听端口 配置后端转发节点(服务节点) 在服务节点上 ...