How can I use TCP Keep Alive function?

When TCP connection is established, if SEND_KEEP command is provided to command register, 1byte data is transmitted to the peer.
If connection is closed, and no response is received, timeout interrupt occurs.
The period for sending TCP Keep Alive packet, can be decided at the firmware.

thanks :slight_smile:

Auto keepalive function should be placed after Socket open and before connection.

ex)

void SetAutoKeepAlive(sn, time) // time > 0
{
    setSn_KPALVTR(sn, time);
    printf("Sn:%d - kpalvtime:%dsec\r\n",sn, 5*getSn_KPALVTR(sn));
}
int32_t loopback_tcps(uint8_t sn, uint8_t* buf, uint16_t port)
{
    int32_t ret;
    uint16_t size = 0, sentsize=0;
    switch(getSn_SR(sn)) {
        case SOCK_ESTABLISHED :
            if(getSn_IR(sn) & Sn_IR_CON) {
                printf("%d:Connected\r\n",sn);
                setSn_IR(sn,Sn_IR_CON);
            }
            if((size = getSn_RX_RSR(sn)) > 0) {
                if(size > DATA_BUF_SIZE) size = DATA_BUF_SIZE;
                //Data recieve
                ret = recv(sn,buf,size);
                if(ret <= 0) return ret;
                sentsize = 0;
                while(size != sentsize) {
                    //Data send
                    ret = send(sn,buf+sentsize,size-sentsize);
                    //If error occured
                    if(ret < 0) {
                    	close(sn);
                        return ret;
                    }
                    sentsize += ret; // Don't care SOCKERR_BUSY, because it is zero.
                }
            }
            break;
        case SOCK_CLOSE_WAIT :
            printf("%d:CloseWait\r\n",sn);
            if((ret=disconnect(sn)) != SOCK_OK) return ret;
            printf("%d:Closed\r\n",sn);
            break;
        case SOCK_INIT :
            printf("%d:Listen, port [%d]\r\n",sn, port);
            if( (ret = listen(sn)) != SOCK_OK) return ret;
            break;
        case SOCK_CLOSED:
            printf("%d:LBTStart\r\n",sn);
            if((ret=socket(sn,Sn_MR_TCP,port,0x00)) != sn)
                return ret;
            printf("%d:Opened\r\n",sn);

            SetAutoKeepAlive(sn, 2); // set Auto keepalive 10sec(2*5)

            break;
        default:
        break;
    }
    return 1;
}

thank you! :slight_smile:

hello Irina. I have issue about detecting disconnect.

  1. i set socket 0 to TCP server
  2. i set socket 0 to LISTEN.
  3. according to you, i set the auto keep alive timer in socket register 0 (Sn_KPALVTR) to 1. so i think W5500 will send keep alive packet every 5 seconds, right?
  4. remote tcp client connected to W5500. the socket status then become 0x17 (SOCK_ESTABLISHED).
  5. i unplugged the LAN cable
  6. i was hoping that the keep alive timer will always send keep alive packet every 5 secs, and if there was no reply, the i thought that W5500 will trigger TIMEOUT interrupt. but it didnt happened at all.
    what did i do wrong here?
    my purpose is, i want to know if the LAN cable unplugged or not.

hello again.
i found something weird.

TESTING 1

  1. set socket 0 to TCP server.
  2. socket 0 OPEN
  3. set keep alive timer to 1
  4. socket 0 LISTEN
  5. remote TCP Client connecting to W5500
  6. connected, socket status become 0x17
  7. TCP client send some data, received by W5500
  8. i unplugged the LAN cable
  9. after some time, socket TIMEOUT occurs (as expected)

TESTING 2

  1. set socket 0 to TCP server.
  2. socket 0 OPEN
  3. set keep alive timer to 1
  4. socket 0 LISTEN
  5. remote TCP Client connecting to W5500
  6. connected, socket status become 0x17
  7. W5500 sent some data, received by TCP client
  8. i unplugged the LAN cable
  9. after some time, socket TIMEOUT occurs (as expected)

TESTING 3

  1. set socket 0 to TCP server.
  2. socket 0 OPEN
  3. set keep alive timer to 1
  4. socket 0 LISTEN
  5. remote TCP Client connecting to W5500
  6. connected, socket status become 0x17
  7. do nothing
  8. i unplugged the LAN cable
  9. i waited, but no timeout at all

i am confused about TESTING 3. do i have to send/receive data first, so the timeout will occur?

1 Like

Keep Alive sends the last 1 Byte from the last transmitted Data. So,Data bigger than 1 Byte must be transmitted before Keep Alive.

when debug, everything is good and working. but If I shut down the computer then I cannot connect to the processor again. When you enter the ethernet settings from the control panel and turn the ethernet off and on, it works normally again. how to solve this error. can you help me. my mcu is working tcp server mode. ı am using tcp comunication.

I think the firewall or other network (wifi, virtual machine) might be turned on.
Before TCP connection, you can execute the cmd windows in PC for ping test.
→ This is to confirm that it is the same network.

I am experiencing test number 3.
When the connection is established, I send 0x00
but after a certain few hours the connection drops again
If I send data, the connection to the other computer becomes live again.
so keep alive works so weird and pointless
but If I set 000 instead of PMODE 111, everything goes back to normal.
i think w5500 is not behaving normally when Auto Negotiation is on

so your solution is set PMODE to 000?
until now, i haven’t found the solution for W5500 keep alive settings.
I did another way

  1. if W5500 set as server, the remote client must send some data periodically to W5500 to maintain connection. If W5500 doesnt reply, then remote server can assume the connection is lost and tries to reconnect.
  2. If W5500 set as client, W5500 must send data periodically to remote server to maintain connection. If remote connection doesnt reply, then W5500 can assume the connection is lost and tries to reconnect.