Recently, with the stm32 + LWIP protocol, the network cannot be reconnected when the network is unplugged. I found a lot of methods on the Internet, but it was really depressing! Later, I accidentally saw a solution to this problem temporarily. Although it was not so perfect, it was the best solution. Share with you.
Problem: LWIP creates a TCP server, and the data can be sent and received normally. But the client is abnormally disconnected-if the client loses power, the client's network cable is suddenly unplugged, and then the server is reconnected, the connection will fail.
Cause: lwip_close (), in the case of abnormal disconnection, I do not know what is still occupying the memory and cannot release resources normally. If you know how to release the instance, give me some advice.
My method is to use the keep alive feature provided by the protocol stack to automatically detect whether the network is offline and release resources. The LWIP callback mechanism is used to call the acept function every time the connection is established successfully, and a flag is added to the function to determine whether the connection is successful.
Add
// Network disconnection Detection
If (eth_readphyregister (phy_address, phy_bsr) & phy_linked_status) = 0x00) | (Listen 45_counter ))
{
Listen 45_counter = 1; // dropped flag
}
In
Static err_t tcpsvrpoll (void * Arg, struct tcp_pcb * PCB)
{
// Add
If (distinct 45_counter)
{
Tcp_keepalive (PCB );
}
}