public class CommandClient {
private static Logger logger = Logger.getLogger(CommandClient.class);
public static OutputStream outStr = null;
public static InputStream inStr = null;
private static Thread tKeep = new Thread(new KeepThread());
private static Thread tRecv = new Thread(new RecvThread());
public static Socket socket = null;
private static boolean iswait = false;
private static int heartNum = 15;
private static String error;
private static String errorTitle;
private static String errorContent;
public static String temperature = "45";
public static Boolean isError = false;
// socket state
public static boolean isServerClosed = true;
private static Thread aLink;
private static int count = 0;
private static int comPort = 55000;
public CommandClient(String ip) {
try {
comPort++;
if (comPort > 65535) {
JOptionPane.showConfirmDialog(null, MessageResource.getMessage("Hint.Port", Language.getLocale()));
return;
}
InetAddress inetAddress = InetAddress.getByName(ConfigUtils.properties.getProperty("cIP"));
socket = new Socket(ip, Constants.TCP_ML, inetAddress, comPort);
// socket = new Socket();
// socket.connect(new InetSocketAddress(ip, Constants.TCP_ML), 1000);
isServerClosed = false;
if (tKeep.isAlive() == false) {
if (iswait == false) {
tKeep.start();
} else {
tKeep = new Thread(new KeepThread());
tKeep.start();
}
}
if (tRecv.isAlive() == false) {
tRecv.start();
}
outStr = socket.getOutputStream();
inStr = socket.getInputStream();
heartNum = 5;
count = 0;
if (DialogAlert.dialogmode != null) {
DialogAlert.dialogmode.dispose();
}
} catch (Exception e) {
count++;
logger.error("Device message socket Exception!" + DateUtil.format(new Date()));
logger.error(e);
isError = true;
isServerClosed = true;
socket = null;
outStr = null;
inStr = null;
FullDuplexClient.socket = null;
FullDuplexClient.outStr = null;
FullDuplexClient.inStr = null;
if (count == 1) {
String error = MessageResource.getMessage("MainFrame.TabbedPane.Error", Language.getLocale())
+ "-Err1007";
String errorTitle = MessageResource.getMessage("Message.Error7.Title", Language.getLocale());
String errorContent = MessageResource.getMessage("Message.Error7.Content", Language.getLocale());
DialogAlert.showImageLossDialog(new JFrame(), new JFrame(),
"<html>" + error + errorTitle + "<br/>" + errorContent + "<br/></html>", 1);
}
}
}
/**
* Heart
*/
private static class KeepThread implements Runnable {
public void run() {
while (!isServerClosed && CRscanner.isC) {
try {
Thread.sleep(500);
String sendStr = "heart";
byte[] sendBuf;
sendBuf = sendStr.getBytes("GBK");
sendMSG(sendBuf);
// System.out.println("發送心跳幀"+DateUtil.format(new Date()));
} catch (Exception e) {
logger.error("heart send fail exception!" + DateUtil.format(new Date()));
logger.error(e);
}
{
heartNum--;
if (heartNum <= 0) {
heartNum = 0;
}
}
if (heartNum == 0) {
// System.err.println("心跳幀五次未接收,socket置空" + DateUtil.format(new Date()));
isServerClosed = true;
isError = true;
socket = null;
iswait = true;
/**
* 自動重連
*/
aLink = new AutomaticLink();
aLink.start();
break;
}
}
}
}
/**
* Receive thread
*/
private static class RecvThread implements Runnable {
public void run() {
try {
while (true) {
Thread.sleep(100);
if (null != inStr && inStr.available() != 0) {
byte[] b = new byte[5];
int r = inStr.read(b);
if (r > -1) {
String str = new String(b, "gbk");
// if (str.equals("heart") == false) {
// System.out.println(str +"================");
// }
/**
* Device instructions
*/
judge(str);
}
}
}
} catch (Exception e) {
logger.error("Receive device error! " + DateUtil.format(new Date()));
logger.error(e);
}
}
}
public static void sendMSG(byte[] b) throws IOException {
try {
outStr.write(b);
} catch (Exception e) {
logger.error("send message fail! " + DateUtil.format(new Date()));
logger.error(e);
}
}
第二段:
{
private static Logger logger = Logger.getLogger(FullDuplexClient.class);
public static InputStream inStr = null;
public static OutputStream outStr = null;
private static Thread tRecv = new Thread(new RecvThread());
public static Socket socket = null;
private static int r;
private static File file = null;
private static int fullPort = 45000;
public FullDuplexClient(String ip) {
try {
/**
* 讀取本地
*/
fullPort++;
if (fullPort > 65535) {
JOptionPane.showConfirmDialog(null, MessageResource.getMessage("Hint.Port", Language.getLocale()));
return;
}
InetAddress inetAddress = InetAddress.getByName(ConfigUtils.properties.getProperty("cIP"));
socket = new Socket(ip, Constants.TCP_IMG, inetAddress, fullPort);
// socket = new Socket();
// socket.connect(new InetSocketAddress(ip, Constants.TCP_IMG), 1000);
if (tRecv.isAlive() == false) {
tRecv.start();
}
outStr = socket.getOutputStream();
inStr = socket.getInputStream();
} catch (Exception e) {
logger.error("Receive Image Data Socket Exception! " + DateUtil.format(new Date()));
logger.error(e);
socket = null;
outStr= null;
inStr = null;
CommandClient.socket = null;
CommandClient.outStr= null;
CommandClient.inStr = null;
}
}
/**
* Receive Image Data
*/
private static class RecvThread implements Runnable {
public void run() {
try {
while (true) {
if (null != inStr && inStr.available() != 0) {
String binName = Constants.CONF_PATH + ScanControl.binName;
file = new File(binName);
FileOutputStream fos = new FileOutputStream(file, true);
int a = inStr.available();
byte[] b = new byte[a];
r = inStr.read(b);
if (r > -1) {
fos.write(b);
fos.close();
}
}
if (file == null || !file.exists()) {
Thread.sleep(500);
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
logger.error("FullDuplexClient Exception!" + DateUtil.format(new Date()));
logger.error(e);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
logger.error("FullDuplexClient Exception!" + DateUtil.format(new Date()));
logger.error(e);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
logger.error("FullDuplexClient Exception!" + DateUtil.format(new Date()));
logger.error(e);
}
}
}
/**
* send byte
*
* @param b
*/
public static void sendMSG(byte[] b) {
try {
outStr.write(b);
} catch (Exception e) {
logger.error("Send Message Exception! " + DateUtil.format(new Date()));
logger.error(e);
}
}
}
uj5u.com熱心網友回復:
INFO adminlogin! 2020-12-14 13:13:40 (LoginFrame.java:370)INFO adminlogin! 2020-12-14 13:20:46 (LoginFrame.java:370)
ERROR Device message socket Exception!2020-12-14 13:25:36 (CommandClient.java:93)
ERROR java.net.ConnectException: Connection timed out: connect (CommandClient.java:94)
INFO Manual connection! 2020-12-14 13:26:05 (DialogNetSet.java:227)
ERROR net connect exception ! Mon Dec 14 13:26:06 CST 2020 (DialogNetSet.java:250)
ERROR java.util.concurrent.TimeoutException (DialogNetSet.java:251)
ERROR Receive Image Data Socket Exception! 2020-12-14 13:26:22 (FullDuplexClient.java:64)
ERROR java.net.ConnectException: Connection timed out: connect (FullDuplexClient.java:65)
ERROR Receive Image Data Socket Exception! 2020-12-14 13:26:26 (FullDuplexClient.java:64)
ERROR java.net.ConnectException: Connection timed out: connect (FullDuplexClient.java:65)
ERROR Device message socket Exception!2020-12-14 13:26:43 (CommandClient.java:93)
ERROR java.net.ConnectException: Connection timed out: connect (CommandClient.java:94)
ERROR Device message socket Exception!2020-12-14 13:26:47 (CommandClient.java:93)
ERROR java.net.ConnectException: Connection timed out: connect (CommandClient.java:94)
ERROR Receive Image Data Socket Exception! 2020-12-14 13:27:49 (FullDuplexClient.java:64)
ERROR java.net.ConnectException: Connection timed out: connect (FullDuplexClient.java:65)
ERROR Device message socket Exception!2020-12-14 13:28:10 (CommandClient.java:93)
ERROR java.net.ConnectException: Connection timed out: connect (CommandClient.java:94)
ERROR Receive Image Data Socket Exception! 2020-12-14 13:29:10 (FullDuplexClient.java:64)
ERROR java.net.NoRouteToHostException: No route to host: connect (FullDuplexClient.java:65)
ERROR Device message socket Exception!2020-12-14 13:29:10 (CommandClient.java:93)
============
上面是例外日志
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/236702.html
標籤:Java EE
上一篇:2020-12-18:java和go,并發控制有哪幾種方式?
下一篇:Ambiguous mapping controler類實作介面的requestmapping 報錯 Ambiguous mapping
