This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TM4C1294KCPDT: TCP Client / Server reconnect issue

Part Number: TM4C1294KCPDT


Dear Ti,

I made MCU as a TCP server and it works well , allow in client to connect & data transfer. when data transfer is working, during which i am disconnecting client from server. If want to connect again then Client gives error in connection to server, while ping works attaching part of function here for reconnection of client.

I want reconnection to be happen in this part of code. 

if(status==0 || connection_Stat==0)
{
// clientfd=0;
clientfd = accept(server, (struct sockaddr *)&clientAddr, &addrlen);
}

Void tcpHandler(UArg arg0, UArg arg1)
{
    int                status;


    struct sockaddr_in localAddr;
    struct sockaddr_in clientAddr;
    int                optval;
    int                optlen = sizeof(optval);
    socklen_t          addrlen = sizeof(clientAddr);
    //Task_Handle        taskHandle;
    //Task_Params        taskParams;
    //Error_Block        eb;

    server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (server == -1)
    {
        System_printf("Error: socket not created.\n");
        goto shutdown;
    }


    memset(&localAddr, 0, sizeof(localAddr));
    localAddr.sin_family = AF_INET;
    localAddr.sin_addr.s_addr = htonl(INADDR_ANY);
    localAddr.sin_port = htons(arg0);

    status = bind(server, (struct sockaddr *)&localAddr, sizeof(localAddr));
    if (status == -1) {
        System_printf("Error: bind failed.\n");
        goto shutdown;
    }

    status = listen(server, NUMTCPWORKERS);
    if (status == -1) {
        System_printf("Error: listen failed.\n");
        goto shutdown;
    }

    optval = 1;
    if (setsockopt(server, SOL_SOCKET, SO_KEEPALIVE, &optval, optlen) < 0) {
        System_printf("Error: setsockopt failed\n");
        goto shutdown;
    }


   // Current_init();


    while ((clientfd = accept(server, (struct sockaddr *)&clientAddr, &addrlen)) != -1)
    {
        connection_Stat=1;
        lcd_clear();
        HAL_Delay(2000);
        lcd_put_cur(0, 0);
        lcd_send_string("Remote Mode");
        if(LC_RMT_PIN)
            {
                   MODE=LOCAL;
            }
            while(1)
            {
                if(MODE==LOCAL)
                {
                    Local_Mode_operation();
                    lcd_clear();
                    lcd_put_cur(0, 0);
                    lcd_send_string("Remote Mode");
                    HAL_Delay(2000);
                }
               while(MODE==REMOTE)
               {
                   status = checkLinkState();
                   if(status==0 || connection_Stat==0)
                   {
                      // clientfd=0;
                       clientfd = accept(server, (struct sockaddr *)&clientAddr, &addrlen);
                   }
                   else
                   {
                          uint8_t Eth_Buffer[129];
                          if(LC_RMT_PIN)
                           {
                              lcd_clear();
                              HAL_Delay(10);
                              lcd_put_cur(0, 0);
                              lcd_send_string("Local Mode");
                              HAL_Delay(5000);
                              lcd_clear();
                              MODE=LOCAL;
                              break;
                           }
                          //recv(lSocket,(char *)rcvd_data,10, MSG_DONTWAIT);
                          recv(clientfd,(char *)rcvd_data,10, MSG_DONTWAIT);
                          if(rcvd_data[0]=='C')
                          {
                              channel_no=((rcvd_data[1]-48)*10 + (rcvd_data[2]-48));
                              float val = ((rcvd_data[4]-48)*1000);
                              val += (rcvd_data[6]-48)*100;
                              val += (rcvd_data[7]-48)*10;
                              val += (rcvd_data[8]-48);
                              Set_point[channel_no]=val/1000;
                              memset(rcvd_data,0x00,sizeof(rcvd_data));
                          }
                          Eth_Buffer[0]=0x2A;

                          for(uint8_t i=0;i<32;i++)
                          {
                              Mux_1_2_add(i);
                             // HAL_Delay(10);
                              for(uint8_t j=0;j<5;j++)
                              {
                                  ADCSequenceDataGet(ADC0_BASE, 0, &ADCValues);
                                  volt=(ADCValues*3.3/4096);
                              }

                              volt= volt*1000;
                              uint8_t t=i*4;
                              Eth_Buffer[t+1]=((int)volt >> 24) & 0xFF;
                              Eth_Buffer[t+2]=((int)volt >> 16) & 0xFF;
                              Eth_Buffer[t+3]=((int)volt >> 8) & 0xFF;
                              Eth_Buffer[t+4]=(int)volt & 0xFF;

                          }
                          //stat=send(lSocket, Eth_Buffer,sizeof(Eth_Buffer), 0 );

                          stat=send(clientfd, Eth_Buffer,sizeof(Eth_Buffer), 0 );
                          if(stat<0)
                              connection_Stat=0;
                          else
                              connection_Stat=1;
                   }
               }
            }
    }
    addrlen = sizeof(clientAddr);
    goto restart;


    System_printf("Error: accept failed.\n");

shutdown:
    if (server > 0)
    {
        close(server);
    }
}

Please help

Regards

Khodidas

  • Hi,

    Already read the link you suggested, and found that there is no such file linked with created. Project . I have takes TCP ECHO example as a template and modified it for the required implementation. 
    SO, if any one can help me to implement such solution in TI-RTOS NDK. any help is invitable.

    Thanks & Regards

    Khodidas

  • Hi,

      The mentioned file is located in C:\ti\tirtos_tivac_2_16_00_08\products\tidrivers_tivac_2_16_00_08\packages\ti\drivers\emac\EMACSnow.c. You can just copy this file to your project folder, make the mentioned modification and rebuild it along with the rest of your application files. 

  • Hi,

    Finally I have resoled this issue when TM4C1294 Is working as a client. But My requirement is to implement in server which helps to config PC/Client IP address can be any which will not strictly bound client to specific IP for connecting with device.

    So , as per your suggestion will try it soon and get back here.

    Regards

    Khodidas

  • Hi,

      Glad that your issue is resolved. Will you elaborate what and how you resolve the issue to share with the community? 

    So , as per your suggestion will try it soon and get back here.

    I will close the thread for now. If you have any update, please just write back to this post and the thread will automatically reopen. Also to give you a heads-up, I will be out of office until next Tuesday.

  • Hi,

    Issue  Came again, when i disconnected cable. link status became 0, as I connect cable again , Link status became 1, but fter this i tried to connect with server but, connect function crashes.. attaching snippet of code.

    Regards

    Khodidas

    while(MODE==REMOTE)
               {
                   status = checkLinkState();
                   if(status==0 || connection_Stat==0)
                   {
                       if(status==0)
                       {
                           connection_Stat=0;
                           lcd_put_cur(1, 0);
                           lcd_send_string("connect Ethernet");
                       }
                       else if(connection_Stat==0)
                       {
                         lcd_put_cur(1, 0);
                         lcd_send_string("Server not ready");
                       }
                       if(LC_RMT_PIN)
                          {
                              MODE=LOCAL;
                              goto Local;
                          }
    
                   }
                   if(status && connection_Stat==0)
                      {
    
                             fdClose(lSocket);
                             fdOpenSession(TaskSelf());
                             lSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
                             if (lSocket < 0)
                             {
                                // System_printf("tcpHandler: socket failed\n");
                                 Task_exit();
                                 return;
                             }
    
                             memset((char *)&sLocalAddr, 0, sizeof(sLocalAddr));
                             sLocalAddr.sin_family = AF_INET;
                             sLocalAddr.sin_addr.s_addr = inet_addr("192.168.1.130");//htonl(INADDR_ANY);
                             sLocalAddr.sin_port = htons(arg0);
    
                             connection_Stat=0;
                              while(connect(lSocket, (struct sockaddr *)&sLocalAddr, sizeof(sLocalAddr)) < 0)
                                 {
                                     if(LC_RMT_PIN)
                                     {
                                         MODE=LOCAL;
                                         goto Local;
                                     }
                                     SysCtlDelay(400000);
                                 }
                              connection_Stat=1;
                              lcd_clear();
                              lcd_put_cur(0, 0);
                              lcd_send_string("Remote Mode");
                      }
                   else
                   {
                          uint8_t Eth_Buffer[129];
                          if(LC_RMT_PIN)
                           {
                              lcd_clear();
                              HAL_Delay(10);
                              lcd_put_cur(0, 0);
                              lcd_send_string("Local Mode");
                              HAL_Delay(5000);
                              lcd_clear();
                              MODE=LOCAL;
                              break;
                           }
                           stat=recv(lSocket,(char *)rcvd_data,10, MSG_DONTWAIT);
    
                          if(rcvd_data[0]=='C')
                          {
                              channel_no=((rcvd_data[1]-48)*10 + (rcvd_data[2]-48));
                              float val = ((rcvd_data[4]-48)*1000);
                              val += (rcvd_data[6]-48)*100;
                              val += (rcvd_data[7]-48)*10;
                              val += (rcvd_data[8]-48);
                              Set_point[channel_no]=val/1000;
                              memset(rcvd_data,0x00,sizeof(rcvd_data));
                          }
                          Eth_Buffer[0]=0x2A;
    
                          for(uint8_t i=0;i<32;i++)
                          {
                              Mux_1_2_add(i);
                             // HAL_Delay(10);
                              for(uint8_t j=0;j<5;j++)
                              {
                                  ADCSequenceDataGet(ADC0_BASE, 0, &ADCValues);
                                  volt=(ADCValues*3.3/4096);
                              }
    
                              volt= volt*1000;
                              uint8_t t=i*4;
                              Eth_Buffer[t+1]=((int)volt >> 24) & 0xFF;
                              Eth_Buffer[t+2]=((int)volt >> 16) & 0xFF;
                              Eth_Buffer[t+3]=((int)volt >> 8) & 0xFF;
                              Eth_Buffer[t+4]=(int)volt & 0xFF;
    
                          }
                          stat=send(lSocket, Eth_Buffer,sizeof(Eth_Buffer), 0 );
                          if(stat<0)
                          {
                              connection_Stat=0;
                              lcd_put_cur(1, 0);
                              lcd_send_string("Server not ready");
                          }
                          else
                          {
                              //connection_Stat=1;
                          }
                   }
               }

  • Hi,

      Is the code you posted for the client side or the server side. If it is for the client side then I'm a bit confused. Is sLocalAddr for the server? The variable name sLocalAddr is a bit confusing. Normally, for the client side, you need to set up the server IP address and port so that you can connect to the server using the connect command. I will suggest you change the variable name from sLocalAddr to sServerAddr to avoid confusion. 

    memset((char *)&sLocalAddr, 0, sizeof(sLocalAddr));
    sLocalAddr.sin_family = AF_INET;
    sLocalAddr.sin_addr.s_addr = inet_addr("192.168.1.130");//htonl(INADDR_ANY);
    sLocalAddr.sin_port = htons(arg0);

    connection_Stat=0;
    while(connect(lSocket, (struct sockaddr *)&sLocalAddr, sizeof(sLocalAddr)) < 0)

    Where does it crash? Do you have enough stack and heap memories? Can you use ROV to find out if you have run out of stack or heap? Can you increase them to see if it makes a difference? Earlier you seemed to resolve the issue on the client side. What did you do to resolve the problem?