TCP/IP Connection monitoring based on Network 3.5.7.0 Library

Singh
2020-09-04
2020-09-18
  • Singh

    Singh - 2020-09-04

    Hello forum,
    i am using a Raspberry Pi (with Codesys 3.5.16.0) as a TCP IP server and almost 80 Raspberry Pi (with Codesys 3.5.16.0) as TCP IP clients. I make an Array of 80 NBS.Connections and let the clients make connections. The server allocates one element of the aforesaid Array randomly. The whole communication is working.

    i have following questions:
    1. The client makes connection then on server side the Server/NBS.connection.Active[x] goes true but it stays true even if the client goes physically offline. Is it so or i made an error? right now i send the timestamp from client to server and as long as it is changing, i know the connection is ok, else i reconnect. Is there any elegent way to solve this.
    2. Can i decide from server side that Server/NBS.connection.Active[x] is only for Client IP xxx.xxx.xxx.xxx
    3. How can i decide from client side that the server Offline is?

    any help is appreciated!
    Thanks in advance

     
  • nothinrandom

    nothinrandom - 2020-09-18

    Probably the easiest approach to this is to implement a heartbeat.

    • Server sends client a dummy message (e.g. "HI") every X seconds to keep connection alive (when you are not regularly sending data to client).
    • Client also sends server a dummy message (e.g. "HI") every X seconds to keep connection alive (when you are not regularly sending data to server).
    • If either side does not receive this dummy message within Y intervals of X seconds, then close connection.

    In each client/server function block, have a boolean input called hearbeat_check from an external timer TON that runs every X+1 seconds... or have a dedicated TON for each function block, but that would be a waste of space. Anyway, for each function block have two bytes: one called current and the other called missed_heartbeat. This variable current is set to 1 if TCP receive buffer has new message. On the rising edge of hearbeat_check, increment missed_heartbeat if current is equal to 0. Else, reset missed_heartbeat to 0. If missed_heartbeat exceeds some Y value, then close connection.

     

Log in to post a comment.