Skip to main content
Visitor II
May 7, 2024
Solved

LWIP

  • May 7, 2024
  • 1 reply
  • 1283 views

Hi,

how can I check if my client in connected to pc server?

--------------------------

/* init code for LWIP */

MX_LWIP_Init();

/* USER CODE BEGIN 5 */

LOCK_TCPIP_CORE();

tcp_setup();

UNLOCK_TCPIP_CORE();

---------------------------

void tcp_setup(void)

{

uint32_t data = 0xdeadbeef;

/* create an ip */

ip4_addr_t ip;

IP4_ADDR(&ip,192,168,1,4); //IP of my PHP server

/* create the control block */

testpcb = tcp_new(); //testpcb is a global struct tcp_pcb

// as defined by lwIP

/* dummy data to pass to callbacks*/

tcp_arg(testpcb, &data);

/* register callbacks with the pcb */

// tcp_recv(testpcb, tcpRecvCallback);

tcp_sent(testpcb, tcpSendCallback);

tcp_err(testpcb, tcpErrorHandler);

/* now connect */

tcp_connect(testpcb, &ip, 8888, connectCallback);

}

---------------------------

this snippet run and is OK.

How can I check if the client is alwais connected to pc server?

Can anyone help me?

Thank you in advance.

 

 

    This topic has been closed for replies.
    Best answer by STea

    Hello @Stefano1 ,

    To check if your client is always connected to the PC server, you can implement a mechanism to periodically send a "heartbeat" or "keep-alive" message from the client to the server. If the server does not receive this message within a certain timeframe, it can assume the client is disconnected. Similarly, the client can check for responses from the server to ensure the connection is still active.

    here is an example:

    #define KEEP_ALIVE_INTERVAL 5000 // 5 seconds
    
    void keepAliveTimerCallback(void *arg) {
     if (testpcb != NULL) {
     // Send a keep-alive message
     tcp_write(testpcb, "KEEP_ALIVE", strlen("KEEP_ALIVE"), TCP_WRITE_FLAG_COPY);
     tcp_output(testpcb);
     }
     // Re-arm the timer
     sys_timeout(KEEP_ALIVE_INTERVAL, keepAliveTimerCallback, NULL);
    }
    void tcp_setup(void) {
    ...
     // Start the keep-alive timer
     sys_timeout(KEEP_ALIVE_INTERVAL, keepAliveTimerCallback, NULL);
    }
    

    Regards

    1 reply

    STeaAnswer
    ST Employee
    July 8, 2024

    Hello @Stefano1 ,

    To check if your client is always connected to the PC server, you can implement a mechanism to periodically send a "heartbeat" or "keep-alive" message from the client to the server. If the server does not receive this message within a certain timeframe, it can assume the client is disconnected. Similarly, the client can check for responses from the server to ensure the connection is still active.

    here is an example:

    #define KEEP_ALIVE_INTERVAL 5000 // 5 seconds
    
    void keepAliveTimerCallback(void *arg) {
     if (testpcb != NULL) {
     // Send a keep-alive message
     tcp_write(testpcb, "KEEP_ALIVE", strlen("KEEP_ALIVE"), TCP_WRITE_FLAG_COPY);
     tcp_output(testpcb);
     }
     // Re-arm the timer
     sys_timeout(KEEP_ALIVE_INTERVAL, keepAliveTimerCallback, NULL);
    }
    void tcp_setup(void) {
    ...
     // Start the keep-alive timer
     sys_timeout(KEEP_ALIVE_INTERVAL, keepAliveTimerCallback, NULL);
    }
    

    Regards