Author Topic: TCP connection get terminated after 20 minuts  (Read 13418 times)

Offline saurabh

  • Newbie
  • *
  • Posts: 10
    • View Profile
TCP connection get terminated after 20 minuts
« on: September 25, 2009, 03:59:27 PM »
Dear all,

I have made TCP listner on port 502,  my PC based TCP client is sending/getting data at each 1.5 sec,
interval, it works well initially, but after some 20 minuts (modscan32)PC client lost TCP connection with
board, and then it can get modscan32(tcp client) connected to my TCP server only after manually making
connection again.

What should I  do to keep my socket alive 24x7?
I learnt from docs and threads that if socket timeout set to 0xffff then it can be made keep alive alltime but
somehow i am still losing connectivity.
Am I missing something?


I have made Max user TCP connections to 10
I have disabled modbus TCP , (As i am using my Modbus routines)
 
Code of my app as below
-------------------------------------------
   myTask
   {
   -------------
    ------------
      if(!Init)   // Init once
      {
          --------------
          --------------
          fnUserTcp();
          -------------
          -------------
           
          Init=1;
      }
      else            // run time
      {
                    -----------------
                    ----------------
                    ----------------
      }




   }
--------------------------------------------------

#define TEST_BUFFER_LENGTH 100
typedef struct stTCP_MESSAGE
{
    TCP_HEADER     tTCP_Header;     // reserve header space
    unsigned char   ucTCP_Message[TEST_BUFFER_LENGTH];
} TCP_MESSAGE;
----------------------------------------------------------

static int fnTestListener(USOCKET Socket, unsigned char ucEvent, unsigned char *ucIp_Data, unsigned short usPortLen);
static unsigned short usTestPort = 502;
static USOCKET test_socket= -1,test_socket1= -1,test_socket2 = -1;

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

extern void fnUserTcp(void)
{   
                               // declare a static socket variable (the value could also be fixed)

    test_socket = fnGetTCP_Socket(TOS_MAXIMISE_RELIABILITY, 0xffff, fnTestListener);    
    fnTCP_Listen(test_socket, usTestPort, 0);
   
}
---------------------------------------------------------------------------------

static int fnTestListener(USOCKET Socket, unsigned char ucEvent, unsigned char *ucIp_Data, unsigned short usPortLen)
{
   int n,i;
   unsigned char TP[30];
   TCP_MESSAGE test_message;

    switch (ucEvent)
   {
    case TCP_EVENT_CONREQ:                                             
    case TCP_EVENT_CONNECTED:
      if(ucEvent==TCP_EVENT_CONNECTED)
         {
         fnDebugMsg("\r\n####Socket Connected........#####");
         sprintf(TP,"\r\nConn-SockID=%d",Socket);
         fnDebugMsg(TP);
         }
    case TCP_EVENT_CLOSE:
    case TCP_EVENT_ACK:
    case TCP_EVENT_ARP_RESOLUTION_FAILED:
    case TCP_EVENT_PARTIAL_ACK:
        break;
    case TCP_EVENT_REGENERATE:
    case TCP_EVENT_DATA:
         
                         
      
                                DecodeTCPQuery(ucIp_Data);                        // My Function
                               
                                if (fnSendTCP(Socket, ResponceData, ResponceLen, TCP_FLAG_PUSH) > 0)    
                                 {
                return APP_SENT_DATA;
                    }
            
      break;
    case TCP_EVENT_ABORT:
        if(ucEvent==TCP_EVENT_ABORT)
        {
           fnDebugMsg("\r\n########## Aborted........##########");
          sprintf(TP,"\r\nAbrt-SockID=%d",Socket);
         fnDebugMsg(TP);
        }
    case TCP_EVENT_CLOSED:
      if(ucEvent==TCP_EVENT_CONNECTED)
      {   
         fnDebugMsg("\r\n########## CLOSED........##########");
         sprintf(TP,"\r\nClose-SockID=%d",Socket);
         fnDebugMsg(TP);
      }
       
      fnTCP_Listen(Socket, usTestPort, 0);                    // go back to listening state on next port number
        break;
    }
    return APP_ACCEPT;
}   


Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3238
    • View Profile
    • uTasker
Re: TCP connection get terminated after 20 minuts
« Reply #1 on: September 25, 2009, 05:06:27 PM »
Hi

The define INFINITE_TIMEOUT can also be used instead of 0xffff.

With this setting there should never be a timeout of the connection. Also, even when there is a timeout set, the timeout is only valid for idle connections and is restarted each time there is connection activity.

A timeout on idle connection is a close. A connection can however also close due to other reasons, such as one side of the connection failing and the other side giving up its transmission (usually results in an abort).

I would monitor the TCP activity (Wireshark) to be sure that the close is not due to another reason since I don't see that a timeout should be able to occur with this setting.

Check also the following:
- what happens when a transmission from your board is not correctly acknowledged? It looks as though the REGENERATE event will cause the reception routine to be called, which is probably incorrect (it should cause a retransmission of the lost data).
- Does your DecodeTCPQuery() routine always prepare data to be returned? If it doesn't it may be incorrect to always call fnSendTCP().

Regards

Mark

Offline saurabh

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: TCP connection get terminated after 20 minuts
« Reply #2 on: September 26, 2009, 01:32:12 PM »

Thanks mark,

Acutally I able to locate why it was disconnected ..
(It was disconnected due to improper handling of partial Ack case, and event regen case)

I saw when some partail ACK case comes, then such 3-5 paritla ACK, regrenrate case get
executed and then connection get aborted.

I modified listner as below... and working well since morning..:)

Infact with my function DecodeTcpQuery() , I kept validation but for simplicity I had not shown
last time...

Though Now its working, Is below my code all right?
(As per performance and reliabilty conserns)

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

static int fnTestListener(USOCKET Socket, unsigned char ucEvent, unsigned char *ucIp_Data, unsigned short usPortLen)
{
   int n,i;
   unsigned char TP[30];
   TCP_MESSAGE test_message;

    switch (ucEvent)
   {
    case TCP_EVENT_CONREQ:                                       
    case TCP_EVENT_CONNECTED: break;
      if(ucEvent==TCP_EVENT_CONNECTED)
         {
         fnDebugMsg("\r\n####Socket Connected........#####");
         sprintf(TP,"\r\nConn-SockID=%d",Socket);
         fnDebugMsg(TP);
         }
            break;
    case TCP_EVENT_ACK: break;
    case TCP_EVENT_ARP_RESOLUTION_FAILED:break;
    case TCP_EVENT_PARTIAL_ACK:
                return APP_REQUEST_CLOSE;

    case TCP_EVENT_REGENERATE:
                          // SEND OLD Know valid data Call_My_Fun(); followed by fnTcpSend(...)
                         return APP_SENT_DATA;
    case TCP_EVENT_DATA:

                          if(DecodeTCPQuery(ucIp_Data))                        // My Function
                          {     
                                if (fnSendTCP(Socket, ResponceData, ResponceLen, TCP_FLAG_PUSH) > 0)   
                                 {
                                  return APP_SENT_DATA;
                                  }
                           }
           
                            break;

    case TCP_EVENT_ABORT:
        if(ucEvent==TCP_EVENT_ABORT)
        {
           fnDebugMsg("\r\n########## Aborted........##########");
          sprintf(TP,"\r\nAbrt-SockID=%d",Socket);
         fnDebugMsg(TP);
        }
    case TCP_EVENT_CLOSED:
      if(ucEvent==TCP_EVENT_CONNECTED)
      {   
         fnDebugMsg("\r\n########## CLOSED........##########");
         sprintf(TP,"\r\nClose-SockID=%d",Socket);
         fnDebugMsg(TP);
      }
       
      fnTCP_Listen(Socket, usTestPort, 0);                 
        break;
    }
    return APP_ACCEPT;
}

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3238
    • View Profile
    • uTasker
Re: TCP connection get terminated after 20 minuts
« Reply #3 on: September 27, 2009, 02:44:30 PM »
Hi

There are no 'obvious' errors, but I am worried about the fact that you were getting the TCP_EVENT_PARTIAL_ACK event.
This means that you have probably sent more that one transmission without waiting for an ACK to the first (normally the TCP_EVENT_ACK event is used to cause transmission of waiting messages).

Regards

Mark