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.

EK-TM4C1294XL: TCP Client send and receive

Part Number: EK-TM4C1294XL

Hi,

We are using EK-TM4C1294XL evaluation kit for TCP Client development, please find the below code for your reference.

Issue: Data Receiving and Transmission not working, Transmission getting enable only after receive data from Port.

Void tcpHandler(UArg arg0, UArg arg1)
{
SOCKET lSocket;
char *buffer;
struct sockaddr_in sLocalAddr;

Error_Block eb;
int clientfd;

int temp,temp1;

buffer = Memory_alloc(NULL, TCPPACKETSIZE, 0, &eb);

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


while(clientfd = (connect(lSocket, (struct sockaddr *)&sLocalAddr, sizeof(sLocalAddr)) < 0)){
SysCtlDelay(g_ui32SysClock/100/3);
}

System_flush();

while (true) {

if(recv_comp == 1){
send(lSocket, (char *)TCP_Send_Buffer,strlen(TCP_Send_Buffer),0);
recv_comp = 0;
}
else;
if(recv_comp == 0){
nbytes = recv(lSocket, (char *)buffer, TCPPACKETSIZE, 0);
if((nbytes > 0)&&(nbytes <= 15)) {
nbytes = 0;
strncpy(Batch_Num, buffer, 15);
}
}
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");

}
Task_sleep(1);
}
}

  • Hi,

    Can you please give more details on what is not working? Are you getting an IP address? How far in the above code do you get?

    Note: you need to initialize the Error_Block also before passing it into Memory_alloc.

    Todd

  • Connection getting established successfully.

    Only transmission in regular intervals is also working fine.

    When ever we call nbytes = recv(lSocket, (char *)buffer, TCPPACKETSIZE, 0);,  it stop transmission.

    After receiving any data from server then only transmission getting enabled.

    Please suggest us how to enable transmission and receiving individually.

     

     

  • recv() will wait for a packet (or timeout). You can control the timeout via setsockopt (with SO_RCVTIMEO).

    Why do you have "else;'"? Did you really want the semi-colon there?

    Todd

  • Void tcpHandler(UArg arg0, UArg arg1)
    {
    SOCKET lSocket;
    char *buffer;
    struct sockaddr_in sLocalAddr;
    Task_Handle taskHandle;
    Task_Params taskParams;
    Error_Block eb;
    int clientfd;

    int nbytes;

    int optval;
    int optlen = sizeof(optval);


    WD_Flag ^= true;

    buffer = Memory_alloc(NULL, TCPPACKETSIZE, 0, &eb);

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


    while(clientfd = (connect(lSocket, (struct sockaddr *)&sLocalAddr, sizeof(sLocalAddr)) < 0)){
    SysCtlDelay(g_ui32SysClock/100/3);
    }


    System_flush();

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

    while (true) {
    if(recv_comp == 1){
    send(lSocket, (char *)TCP_Send_Buffer,strlen(TCP_Send_Buffer),0);
    recv_comp = 0;
    }


    nbytes = recv(lSocket, (char *)recvbuffer, TCPPACKETSIZE, 0);
    if((nbytes > 0)&&(nbytes <= 15)) {
    System_printf(recvbuffer);
    nbytes = 0;
    }

    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");

    }
    Task_sleep(1);

    }
    }

    As suggested we tested with socket receive time out option, it is generating error message as "Error: setsockopt failed"

     

    After change in configuration as   complete data not getting transmitting

     

    Please suggest us how to resolve this issue

     

     

  • Hi Veeraswamy,

    I see you using recv_comp to switch between receiving and sending.  When you send data, you set it to 0, so that you'll receive data on the next loop iteration, but you do not reset it to 1 in the code shown here. Is this done in a separate piece of code? You should ensure that you're actually calling these functions. I would suggest for you to check the return values of the send() and recv() calls before doing anything else. This may help you see the issue right away.

    Please explain more what you mean when you say that "after calling recv() transmission stops". What indicates to you that transmission stops? I also don't see a way for your loop that calls send() and recv() to terminate. That may be an issue at some point - you may already be aware of this.

    To use setsockopt with SO_RCVTIMEO the optval needs to be of type struct timeval. There's some information here, and you can find examples online.

    Best,
    Brandon