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.

CCS/TM4C1294NCPDT: TCPHandler Send and Receive Task

Part Number: TM4C1294NCPDT

Tool/software: Code Composer Studio

Please find the attached two different task meant for tcpHandler (Send) Port No: 5000, and tcpHandler2 (Receiving) Port No: 5001from Server.

Can you please go through that and suggest us best recommendations.

Using tcpHandler task, I am sending data to server on event based and closing socket successfully. When ever I am sending data to server then only it's connecting to Server and sending data otherwise it's waiting for 2 minutes to establish connection with the server, Can you please recommend us how to establish connection when ever request from Server without any delay. 

Usinf tcpHandler2 task, I am receiving data from Server and Closing socket, After closing this socket connection not getting established with the Server until reset the device, Can you please suggest us how to resolve this issue.

Our aim is to Send data to Server and Close Socket, Receive data from Server and Close Socket.

Void tcpHandler(UArg arg0, UArg arg1)
{
    SOCKET lSocket;
    int    status;
    struct sockaddr_in sLocalAddr;
    int    clientfd;

    int temp,temp1;

    int txbytes;
    int txlen;
    int                optval;
    int                optlen = sizeof(optval);

    unsigned long rc;
    struct linger lingerConfig;

    WD_Flag ^= true;


    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(Server_IP_Address);//inet_addr("10.7.55.85");////inet_addr("192.168.0.125");//htonl(INADDR_ANY);
    sLocalAddr.sin_port = htons(arg0);

    sprintf (TCP_Send_Buffer, "%c", '$');
    sprintf (TCP_Send_Buffer+1, "%c", ',');
    usprintf(TCP_Send_Buffer+2, "%d.%d.%d.%d", Ip_Address_Static & 0xff, (Ip_Address_Static >> 8) & 0xff,
                (Ip_Address_Static >> 16) & 0xff, (Ip_Address_Static >> 24) & 0xff);

    sprintf (TCP_Send_Buffer+strlen(TCP_Send_Buffer), "%c", ',');

    temp = (char)(sLocalAddr.sin_port >> 8) & 0xff;
    temp1 = (char)(sLocalAddr.sin_port & 0xff);
    temp = (int)(temp1 << 8) | temp;
    usprintf(TCP_Send_Buffer+strlen(TCP_Send_Buffer), "%d",  temp);//,

    sprintf (TCP_Send_Buffer+strlen(TCP_Send_Buffer), "%c", ',');
    Frame_len = strlen(TCP_Send_Buffer);

    while(clientfd = (connect(lSocket, (struct sockaddr *)&sLocalAddr, sizeof(sLocalAddr)) < 0)){
        SysCtlDelay(g_ui32SysClock/100/3);
        System_printf("Connection failed.\n%d", errno);
    }

    System_flush();

    

    optval = 1;
    if (setsockopt(lSocket, SOL_SOCKET, SO_RCVTIMEO, &optval, optlen) < 0) {
        System_printf("Error: setsockopt failed\n");
    }

    while (true) {

      MAP_GPIOPinWrite(GPIO_PORTN_BASE, GPIO_PIN_5,
                            (MAP_GPIOPinRead(GPIO_PORTN_BASE, GPIO_PIN_5) ^
                                            GPIO_PIN_5));
        if (Semaphore_getCount(semHandle) == 0) {
            System_printf("Sem blocked in TCP Sender \n");
        }
        Semaphore_pend(semHandle, BIOS_NO_WAIT);

        WD_Flag ^= true;

        if((recv_comp == 1)&&(tcp_connected == 1)&&(tx_start == 1)&&(Power_Reset == 0)){
            txbytes = send(lSocket, (char *)TCP_Send_Buffer,strlen(TCP_Send_Buffer),0);
            if(txbytes == strlen(TCP_Send_Buffer)){
              Tare_Key = 0;
              memset(T_Weight,0x00,10);
              f2 = 0;
              txbytes = 0;
              close(clientfd);
            }
            else;
        }
        else{
            if(Power_Reset == 0){
                memset(TCP_Config_transmit_buf,0x00,150);
                usprintf (TCP_Config_transmit_buf, "%c%c%s%c%s%c%c%c%c",
                         '$',',',Device_IP_Address, ',',"Connection_Check_with_Server",',','\r',',','\n');

                txbytes = send(lSocket, (char *)TCP_Config_transmit_buf,strlen(TCP_Config_transmit_buf),0);
                if(txbytes == strlen(TCP_Config_transmit_buf)){
                    send_server_ip = 0;
                }
                else;
                close(clientfd);
            }
            else;
        }


        if (connect(lSocket, (struct sockaddr *) &sLocalAddr, sizeof(sLocalAddr)) < 0){
          System_printf("DID NOT CONNECT \n");
          System_printf("%d\n", fdError());
        } else {
          System_printf("CONNECTED \n");
        }

        resource = 0;
        Semaphore_post(semHandle);
        Task_sleep(1000);
    }
}



tcpHandler2.c