Home > Back-end >  Help bosses scoket abnormal problem
Help bosses scoket abnormal problem

Time:12-19

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 IP (String) {

Try {
ComPort++;
If (comPort & gt; 65535) {
JOptionPane. ShowConfirmDialog (null, MessageResource. GetMessage (" Hint. Port ", Language. The getLocale ()));
return;
}
InetAddress InetAddress=InetAddress. GetByName (is ConfigUtils. Properties. GetProperty (" cIP "));
The socket=new socket (IP, the TCP_ML, inetAddress, comPort);
//socket=new socket ();
//socket. Connect (new InetSocketAddress (IP, the 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. The dispose ();
}
} the catch (Exception e) {
count++;
Logger. The error (" Device message socket Exception!" + DateUtil. The format (new Date ()));
Logger. The error (e);
IsError=true;
IsServerClosed=true;

The socket=null;
OutStr=null;
InStr=null;

FullDuplexClient. Socket=null;
FullDuplexClient. OutStr=null;
FullDuplexClient. InStr=null;

If (count==1) {
String error=MessageResource. GetMessage (MainFrame. The TabbedPane. "error", Language. The getLocale ())
+ "- Err1007";
String errorTitle=MessageResource. GetMessage (Message. Error7. "Title", Language. The getLocale ());
String errorContent=MessageResource. GetMessage (Message. Error7. "Content", Language. The getLocale ());
DialogAlert. ShowImageLossDialog (new JFrame (), new JFrame (),
"& lt; Html>" + error + errorTitle + "& lt; Br/& gt;" + + "errorContent & lt; Br/& gt; " , 1);
}
}
}

/* *
* Heart
*/
Private static class KeepThread implements Runnable {
Public void the run () {
while (! IsServerClosed & amp; & CRscanner. IsC) {
Try {
Thread.sleep (500);
String sendStr="heart";
Byte [] sendBuf;
SendBuf=sendStr. GetBytes (" GBK ");
SendMSG (sendBuf);
//System. Out. Println (" sends a heartbeat frame "+ DateUtil. The format (new Date ()));
} the catch (Exception e) {
Logger. The error (" heart send fail exception!" + DateUtil. The format (new Date ()));
Logger. The error (e);
}
{
HeartNum -;
If (heartNum & lt;=0) {
HeartNum=0;
}
}
If (heartNum==0) {
//System. Err. Println (" five heart frame did not receive, socket empty "+ DateUtil. The format (new Date ()));
IsServerClosed=true;
IsError=true;
The socket=null;
Iswait=true;

/* *
* automatic reconnection
*/
ALink=new AutomaticLink ();
ALink. Start ();
break;
}
}
}
}

/* *
* the Receive thread
*/
Private static class RecvThread implements Runnable {
Public void the run () {
Try {
While (true) {
Thread.sleep (100);
if (null !=inStr & amp; & InStr. Available ()!=0) {
Byte [] b=new byte [5].
Int r=inStr. Read (b);
If (r & gt; 1) {
String STR=new String (b, "GBK");

//if (STR) equals (" heart ")==false) {
//System. Out. Println (STR + "================");
//}
/* *
* Device instructions
*/
The judge (STR);
}
}
}
} the catch (Exception e) {
Logger. The error (" the Receive device error! "+ DateUtil. The format (new Date ()));
Logger. The error (e);
}
}
}

Public static void sendMSG (byte [] b) throws IOException {
Try {
OutStr. Write (b);
} the catch (Exception e) {
Logger. The error (" send message fail! "+ DateUtil. The format (new Date ()));
Logger. The error (e);
}
}


The second section:
{

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;
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related