1.socket客户端代码
package com.tqjc.system.system.socket;
import lombok.extern.slf4j.Slf4j;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
@Slf4j
public class DigitalizeSocketSendOperator {
private static final ThreadLocal<Socket> threadConnect = new ThreadLocal();
private static Socket client;
private static OutputStream outStream = null;
private static InputStream inStream = null;
private static Thread keepThread = new Thread(new DigitalizeSocketSendOperator.KeepThread());
public static Lock lock = new ReentrantLock();
private static String host = "192.168.8.25";
private static int port = 9009;
public static void connect() {
try {
lock.lock();
disconnect();
client = threadConnect.get();
if (client == null) {
client = new Socket(host, port);
threadConnect.set(client);
log.info("========连接开始!========");
} else if (client.isClosed()) {
client = new Socket(host, port);
threadConnect.set(client);
log.info("========连接开始!========");
} else {
client = threadConnect.get();
log.info("当前已建立连接,请勿重复建立");
}
outStream = client.getOutputStream();
inStream = client.getInputStream();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
keepThread = new Thread(new DigitalizeSocketSendOperator.KeepThread());
keepThread.start();
}
}
public static void disconnect() {
try {
lock.lock();
client = threadConnect.get();
if (outStream != null) {
outStream.close();
outStream.flush();
}
if (inStream != null) {
inStream.close();
}
if (client != null) {
if (!client.isClosed()) {
client.close();
threadConnect.set(client);
}
}
if (keepThread != null) {
keepThread.interrupt();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public static void sendMsg(String msg) {
try {
lock.lock();
byte[] bytes = msg.getBytes("UTF-8");
outStream.write(bytes);
outStream.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public static void sendKeepMsg(String msg) throws IOException {
byte[] bytes = msg.getBytes("UTF-8");
outStream.write(bytes);
outStream.flush();
}
private static class KeepThread implements Runnable {
@Override
public void run() {
while (true) {
try {
Thread.sleep(30*1000);
lock.lock();
log.info("数字化通道发送心跳数据包:{}" , new SimpleDateFormat("yyyy.MM.dd HH:mm:ss").format(new Date()));
sendKeepMsg("00" + new Date());
} catch (Exception e) {
connect();
log.error("发送心跳数据包出现异常",e);
throw new RuntimeException(e);
}finally {
lock.unlock();
}
}
}
}
}
2.socket服务端代码
package com.tqjc.system.system.socket;
import com.tqjc.system.common.constant.GlobalConstant;
import com.tqjc.system.core.threadpool.ThreadPoolManager;
import com.tqjc.system.core.util.SpringUtils;
import com.tqjc.system.system.service.SysTaskService;
import lombok.extern.slf4j.Slf4j;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
@Slf4j
public class MESSocketRecvOperator {
private static ServerSocket serverSocket;
public static void connet() throws IOException {
log.info("服务端启动监听");
serverSocket = new ServerSocket(9009);
new Thread(new Runnable() {
@Override
public void run() {
log.info("==============MESSocketRecvOperator 服务端启动监听===============");
while (true){
try {
Socket accept = serverSocket.accept();
ThreadPoolManager.getInstance().execute(new RecvThread(accept));
} catch (Exception e){
e.printStackTrace();
log.error("监听线程出现异常",e);
}
}
}
}).start();
}
@Slf4j
private static class RecvThread implements Runnable {
private Socket socket;
public RecvThread(Socket socket) {
this.socket = socket;
}
public RecvThread() {
}
@Override
public void run() {
log.info("==============MESSocketRecvOperator 开始接收数据===============");
try {
InputStream inputStream = socket.getInputStream();
byte[] b = new byte[5000];
if (inputStream != null && inputStream.available() > 0) {
int len = inputStream.read(b);
String content = new String(b, 0, len);
if(content.length() <= 30){
log.info("心跳信息过滤,内容是:{}",content);
return;
}
SysTaskService sysTaskService = SpringUtils.getBean("sysTaskService");
sysTaskService.acceptTask(content);
}
} catch (Exception e) {
log.error("接受socket数据线程出现异常",e);
e.printStackTrace();
}
}
}
}