1. Client disconnection caused by forced server disconnection
Method 1:If the captured data is 0, the server is disconnected, the socket is closed, and the connection is reconnected.
Method 2:The socket has always had this problem. After the server is disconnected, I believe your client's CPU is always at 100%, because it will continue to receive a 0-byte packet in an infinite loop.
The solution is to send a heartbeat packet or send a 0-byte packet every time a packet is received. If the packet is disconnected, an exception occurs.
In fact, the main problem is that if the server is forced to disconnect, that is, the physical disconnection or the process is terminated directly, the client cannot receive the disconnection request, that is to say, the client does not think it is disconnected at this time, and the client is still receiving a zero-byte packet, infinite loop, so the client's CPU will remain in the 100% status.
I encountered this problem when I wrote a c/s communication program. It was very troublesome to find a solution for a day. So I thought of a solution myself, it is to send a 0
In this case, if the server is disconnected, the client will start to receive a 0-byte packet in an infinite loop. Then, after receiving the packet, follow the solution I mentioned, A 0-byte packet is returned, but it cannot be sent.
Send, so it will be abnormal, and it will be OK !!
Client code:
'Accept data, and return a garbage package
Xxx. Send (encoding. Unicode. getbytes ("0") 'garbage package
'If the server is disconnected, it cannot be sent out. If the result is abnormal, it can be flushed.
Method 3:It may be that your client has not received the disconnect message. This requires a heartbeat packet to be added to the communication protocol. If the packet is not received at any time, the reconnection is closed.
2. If the client fails to connect for the first time, the client will be reconnected/disconnected. The client will automatically reconnect after a period of time.
Method 1:If the connection fails for the first time, capture an exception and try again.
Method 2:If the connection is forcibly disconnected by the remote host, a timer can be added to determine the socket status. If the connection is not in the connection status, the system automatically reconnects the connection from time to time.
Simple Example code:
Public class mysocket
{
Socket m_socket = NULL;
Public String server_ip;
Public int server_port;
Public socket createclientsocket ()
{
// Use the above IP address and port to establish a connection to the server.
// The corresponding socket is returned successfully.
// If it fails, null is returned;
}
Public bool connect ()
{
// Call the above connection method here
M_socket = NULL;
M_socket = createclientsocket ();
If (m_socket = NULL)
{
Return false;
}
Return true;
}
Publice bool reconnect ()
{
// Call that method as above
}
/// Method used to send data
Publice int senddata ()
{
// Here, call the following function to check the socket status before sending data
Bool ret = true;
Bool isconnected = false;
Bool isread = false;
Bool iswrite = false;
Bool iserror = false;
Int avalsize = 0;
Ret = checksocket (ref isconnected, ref isread, ref iswrite, ref iserror, ref avalsize );
If (ret = false
| Isconnected = false
| Iserr = true
| (Isread = true & availablesize = 0
)
{
// Call the reconnection
Reconnect ();
}
// Check again after completion. Check whether the connection is complete.
Ret = checksocket (ref isconnected, ref isread, ref iswrite, ref iserror, ref avalsize );
If (ret = false
|