Home > other >  Java Socket communication heartbeat packets disconnect problem
Java Socket communication heartbeat packets disconnect problem

Time:10-03

Here is my new a socket, connects and every 10 seconds to the server sends a heartbeat data

Below is the server side or abnormal shutdown for the initiative, I continue to send the data and the abnormal heartbeat, don't know how to solve the abnormal disconnect after, how to restart the socket, nor will the great god help the younger brother to solve, such as online, thank you!

CodePudding user response:

What the demand is, the server disconnected, you want to how to deal with?
The anomaly can be detected in disconnected continuously try to connect again later, also can close the socket directly, it depends on your business,

CodePudding user response:

The eldest brother, you said that the two methods is indeed demand (I two needs are possible), but my question is the socket, close off after, I again new client class will be a communication socket has been closed abnormalities, can appear constantly try to connect again I the IO in circulation anomaly, is a communication has been disconnected, I don't have the timely monitoring to send things in to the server, also can appear problem, it is my problem, not demand, both put method I tried, is not to solve the problem

CodePudding user response:

Socket object is not necessary to reuse, each time a new client, without judging the if (socket==null), direct new a socket,

CodePudding user response:



I don't know why the socket disconnect after getlocal or the original communication?
I tried to close, also tried socket=null, but the debug program or abnormal end when I close, get out of the connection is established for the first time, don't let me new again, this is I can't understand, the trouble to help me explain, thank you

CodePudding user response:

Directly put figure do you want to let others help you put on all of the code again? Or capture, save, modify, upload again?
Try in the first sentence, also remove the outer layer of the if judgment, directly run if the inside of the code, the code only when new threadConnect object to new socket, disconnect the reconnection, first get a threadConnect object socket, this socket even if broken, there is still a object, will never be equal to null, so if the new socket will never perform,
Manually set the socket=null can also, but the correct way should be threadConnect. Set (null) (premise is wrong set method to judge the incoming parameters are null), rather than a socket=null,

CodePudding user response:

Thank you, I just know a way of thinking, don't bother you take other, I'll do it and see with you in this line of thought

CodePudding user response:

The original poster out yet? Let me also reference down...

CodePudding user response:

Change as well, is to do a simple modification, can look at the example code

Public FullDuplexClientDemo IP (String) {
Try {
//socket=threadConnect. The get ();
//if (socket==null) {
The socket=new socket (IP, Integer. The valueOf (is ConfigUtils. Properties. GetProperty (" CRScannerPort ")));
//threadConnect. Set (socket);
If (tKeep isAlive ()==false & amp; & TRecv. IsAlive ()==false) {
TKeep. Start ();
TRecv. Start ();
}
//}
OutStr=socket. GetOutputStream ();
InStr=socket. GetInputStream ();
Islive=true;
} the catch (Exception e) {
e.printStackTrace();
New WarningDialog (MessageResource. GetMessage (MainFrame. The TabbedPane. "Error", Language. The getLocale ()) + "- Err1007");
The socket=null;
}

CodePudding user response:

The building Lord, in the Android, completely shut down after application, the heartbeat connection exists, how to disconnect?

CodePudding user response:

KeepThread and recvThread implement these two classes
  • Related