关闭

关于Socket长连接异步单工

标签: 关于Socket长连接异步单工
1375人阅读 评论(0) 收藏 举报
分类:

1.关于异步单工长连接的概念问题,百度上有很多大牛给出了解释,在这边就不进行说明了,直接来代码

2.长连接异步单工 保持心跳,15秒发送一个心跳,服务器端60秒接收一次心跳,以此来判断两条链路是否在线

package com.credit.alive;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


import org.apache.log4j.Logger;


import com.credit.entity.ISO8583Utils;
import com.credit.utils.ByteUtils;


public class Client {


private String serverIp;
private int port;
private Socket socket;
private ServerSocket serverSocket;
private Socket serverSoc;
private int serverPort;
private boolean running = false;
private long lastSendTime;
private long lastReceTime;
OutputStream stream;
InputStream input;

Logger logger = Logger.getLogger(Client.class);
public static ConcurrentHashMap<String, Object> actionMapping = new ConcurrentHashMap<String, Object>();
private static Client client = null;


private Client() {
}


public void stop() {
if (running)
running = false;
try {
if(socket!=null)           socket.close();
if(stream!=null)          stream.close();
if(serverSoc !=null)     serverSoc.close();
if(input !=null)           input.close();
if(serverSocket!=null) serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}



private Client(String serverIp, int port, int serverPort) {
this.serverIp = serverIp;
this.serverPort = serverPort;
this.port = port;
}


public static Client getInstance(String serverIp, int port, int serverPort) throws UnknownHostException, IOException {
if (client == null) {
client = new Client(serverIp, port, serverPort);
client.start();
}
return client;
}

public static Client getInstance(){
return client;
}


public void start() throws UnknownHostException, IOException {
if (running)
return;
socket = new Socket(serverIp, port);
stream = socket.getOutputStream();
serverSocket = new ServerSocket(serverPort);
socket.setSoTimeout(60000);
lastSendTime = System.currentTimeMillis();
running = true;
Thread keepAliveWatch = new Thread(new KeepAliveWatchDog());
keepAliveWatch.setDaemon(true);
keepAliveWatch.start();
serverSoc = serverSocket.accept();
input = serverSoc.getInputStream();
new Thread(new ReceiveWatchDog()).start();
lastReceTime = System.currentTimeMillis();
}


public void reconnect() throws IOException{
logger.info("进入了重连方法:"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
stop();
socket = new Socket(serverIp, port);
stream = socket.getOutputStream();
serverSocket = new ServerSocket(serverPort);
socket.setSoTimeout(60000);
lastSendTime = System.currentTimeMillis();
running = true;
serverSoc = serverSocket.accept();
input = serverSoc.getInputStream();
lastReceTime = System.currentTimeMillis();

logger.info("重连成功...");
}

public synchronized void sendObject(byte[] data)  {
System.out.println("发送数据:" + ByteUtils.getHexStr(data));//
try {
stream.write(data);
stream.flush();
} catch (IOException e) {
e.printStackTrace();
}
}


class KeepAliveWatchDog implements Runnable {
long checkDelay = 10;
long keepAliveDelay = 15000;
public void run() {
while (true) {
if (System.currentTimeMillis() - lastSendTime > keepAliveDelay) {
sendObject("0000".getBytes());
lastSendTime =System.currentTimeMillis();
} else {
try {
Thread.sleep(checkDelay);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
class ReceiveWatchDog implements Runnable {
long keepAliveDelay = 60000;
ReceiveWatchDog(){}
public void run() {
while (running) {
try {
if(System.currentTimeMillis() - lastReceTime  > keepAliveDelay && input.available() <= 0){
logger.info("lastReceTime:"+lastReceTime+ "   System.currentTimeMillis()"+System.currentTimeMillis() +"   掉线");
reconnect();
}
ByteArrayOutputStream outAr = null;
if (input.available() > 0) {
lastReceTime = System.currentTimeMillis();
outAr = new ByteArrayOutputStream();
byte[] bytes = new byte[1024];
input.read(bytes);
byte[] Datalen = new byte[] { bytes[0], bytes[1],
bytes[2], bytes[3] };// 获取长度
int readLen = ByteUtils.bytesToIntLen(Datalen);
logger.info("读取的长度:"+readLen);
if (readLen != 0) {
outAr.write(ByteUtils.subArray(bytes, 0, readLen + 4));
byte[] result = ByteUtils.subArray(
outAr.toByteArray(), 4, -1);// 去除长度
Map<Integer, String> str = new ISO8583Utils().fromISO8583(result);
logger.info("str:"+str);
Client.actionMapping.put(str.get(11), str);
}
} else {
Thread.sleep(10);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:2151次
    • 积分:51
    • 等级:
    • 排名:千里之外
    • 原创:3篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档