how to set keep alive timeout for socket(FRDMk64)

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

how to set keep alive timeout for socket(FRDMk64)

1,437 Views
sudhakarp
Contributor V

Hi,

     i am working on FRDMk64 controller. i am using KDS 2.0 and KSDK1.1.0 IDE.

can you tel how to change or set KEEP_ALIVE_TIMEOUT for socket. if you tell API function

its helpful to me.

thanks ang regards,

sudhakar p

0 Kudos
9 Replies

878 Views
Carlos_Musich
NXP Employee
NXP Employee

Hi Sudhakar,

You must use setsockopt() and the option is OPT_KEEPALIVE.

You can find it in Reference Manual:

You can find it in installation path depending if you use classic MQX or MQX for KSDK:

C:\Freescale\Freescale_MQX_4_1\doc\rtcs

C:\Freescale\KSDK_1.2.0\doc\tcpip\mqx_rtcs

Best regards,

Carlos

0 Kudos

878 Views
sudhakarp
Contributor V

hi,

     actually i have two socket. i am listening 7727 and 7728 port. so i want to set different timeout for each socket.

is possible? if possible how to set different time in following API function.

"retval = setsockopt(client_sock, SOL_TCP,OPT_KEEPALIVE, &option, sizeof(option));

using above function how can i set different keeplive.

thanks and regards,

sudhakar p

0 Kudos

878 Views
Carlos_Musich
NXP Employee
NXP Employee

Hi,

when you use function socket() to create a new socket it returns a Socket handle which is the first parameter used by setsockopt(). So you need to save this each socket handle in different variables to use them with setsockopt() and set a different value in each case.

0 Kudos

878 Views
sudhakarp
Contributor V

hi carlos,

               i am using different socket handle only. but my question was, just assume i am passing any one parameter below,

#define OPT_CONNECT_TIMEOUT      0x01

#define OPT_RECEIVE_NOWAIT          0x09

#define OPT_KEEPALIVE                      0x10

#define OPT_TIMEWAIT_TIMEOUT      0x17

and also default time values also defined in rtcs.h file.like Connect Timeout

(OPT_CONNECT_TIMEOUT) 8minute like that.  so how it will take corresponding time?is 0x09 or 0x10 or 0x17 have relationship  with anyother?.

i gone throught RTCS document also

Option name: OPT_KEEPALIVE

Protocol level :SOL_TCP

Values                  • Zero (RTCS does not probe the remote endpoint).

                             • Non-zero (if the connection is idle, RTCS periodically probes the

                             remote endpoint, an action that detects, whether the remote

                            endpoint is still present).

Default value :     Zero minutes

Change:               Before bound

Socket type :       Stream

Comments :        The option is not a standard feature of the TCP/IP specification and

generates unnecessary periodic network traffic.

thanks and regards,

sudhakar p

0 Kudos

878 Views
Carlos_Musich
NXP Employee
NXP Employee

Hi Sudhakar,

I am not sure if I got your question, but you need to set OPT_KEEPALIVE macro in the third parameter which is indicates the name of the option and in the 4th parameter which is option value is where you set the amount of minutes you want.

Regards,

Carlos

0 Kudos

878 Views
sudhakarp
Contributor V

hi,

i tried following steps

client_sock= accept(conn_sock, &remote_sin, &remote_addrlen);

     option =60000;//for 1 minute

          retval = setsockopt(client_sock, SOL_TCP, OPT_KEEPALIVE,&option, sizeof(option));    

          if (retval != RTCS_OK)

          {

              fputs("Fatal Error: Unable to set socket options.", stderr);

              _task_block();

          }

but after 1MINUTE its not disconnect the connection. still connection was there. why?. actually i want after some time the connection should shutdown if no data transfer between particular time. if i am doing anything wrong just correct me.

regards,

sudhakar p

0 Kudos

878 Views
Carlos_Musich
NXP Employee
NXP Employee

Hi Sudhakar,

keepalive option does the opposite of what you need. It send zero length packets to keep connection alive.

You may check Timewait Timeoutand close/shut the socket manually after validating what this function returned.

As I told before ou can use function setsockopt() to manage sockets timeouts. These timeouts are declared in tcp_prv.h on RTCS project.

#define TCP_MSL               120000L  /* Maximum Segment Lifetime; the

                                          longest time that a packet can

                                          travel in the Internet (2 min) */

#define TCP_WAITTIMEOUT (2 * TCP_MSL)  /* timeout for TIME_WAIT state, defined

                                          as 2 * MSL (4 min) */

#define TCP_SENDTIMEOUT_MIN   100000L  /* as per RFC1122 4.2.3.5 */

#define TCP_OPENTIMEOUT_MIN   180000L  /* as per RFC1122 4.2.3.5 */

But you can override these values with setsockopt().

Regards,

Carlos

0 Kudos

878 Views
sudhakarp
Contributor V

hi,

     thanks for your reply.

          but i dnt know which Timeout i want to use exactly. actually i want after some time(whatever configured time)if there is no data transfer between socket ,its should be automatically close the connection. so how to do that one. please mention with example (only setsockopt() API example). i gone through tcp_prv.h file also.

thanks and regards,

sudhakar p

0 Kudos

878 Views
sudhakarp
Contributor V

hi,

thank you.i have one doubt,here

     setsockopt(client_sock, SOL_TCP,OPT_KEEPALIVE, &option, sizeof(option));

in above API function instead of OPT_KEEPALIVE can i directly pass 60000 or 30000 (for 1-minute and 30 sec)

values.

regards,

sudhakar p

0 Kudos