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.
Tool/software: TI C/C++ Compiler
We have successfully implemented TCP Client and it is getting connected to Server and sending data in regular intervals.
Now we are looking for TCP Server in the same application to receive data from Server.
Port numbers for Sending and Receiving are different.
Can you please suggest how to implement TCP Client and Server in one application.
Please find the attached code for reference, tcpHandler as Client, tcpHandler2 as Server.
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); sLocalAddr.sin_port = htons(arg0); while(clientfd = (connect(lSocket, (struct sockaddr *)&sLocalAddr, sizeof(sLocalAddr)) < 0)){ SysCtlDelay(g_ui32SysClock/100/3); } System_flush(); status = bind(lSocket, (struct sockaddr *)&sLocalAddr, sizeof(sLocalAddr)); if (status == -1) { System_printf("Error: bind failed.\n"); } 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; tcp_connected = 1; connstatuscntr = 0; if((recv_comp == 1)&&(tcp_connected == 1)){ txbytes = send(lSocket, (char *)TCP_Send_Buffer,strlen(TCP_Send_Buffer),0); if(txbytes == strlen(TCP_Send_Buffer)){ recv_comp = 0; Tare_Key = 0; memset(T_Weight,0x00,10); f2 = 0; recv_comp = 0; } else; } 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; // /* Unlock resource */ // Semaphore_post(semHandle); Task_sleep(1); } } Void tcpWorker(UArg arg0, UArg arg1) { int clientfd = (int)arg0; int bytesRcvd; int bytesSent; char buffer[256]; System_printf("tcpWorker: start clientfd = 0x%x\n", clientfd); while ((bytesRcvd = recv(clientfd, buffer, 256, 0)) > 0) { bytesSent = send(clientfd, buffer, bytesRcvd, 0); if (bytesSent < 0 || bytesSent != bytesRcvd) { System_printf("Error: send failed.\n"); break; } } System_printf("tcpWorker stop clientfd = 0x%x\n", clientfd); } Void tcpHandler2(UArg arg0, UArg arg1) { int socketl; int status; struct sockaddr_in sLocalAddr; Error_Block eb; int clientfd; int nbytes; int optval; int optlen = sizeof(optval); char ip_len=0; // int iCnt,jCnt; char trecvbuffer[20]; int c = 0; char B_Rate[10]; unsigned int recv_ip;//,sIP; char dlen,Receive_Fram_Len; char *eptr; uint32_t e2size,e2block,returnCode; int server; struct sockaddr_in localAddr; struct sockaddr_in clientAddr; socklen_t addrlen = sizeof(clientAddr); Task_Handle taskHandle; Task_Params taskParams; WD_Flag ^= true; fdOpenSession(TaskSelf()); 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 = 5001;//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; } while ((clientfd = accept(server, (struct sockaddr *)&clientAddr, &addrlen)) != -1) { System_printf("tcpHandler: Creating thread clientfd = %d\n", clientfd); /* Init the Error_Block */ Error_init(&eb); /* Initialize the defaults and set the parameters. */ Task_Params_init(&taskParams); taskParams.arg0 = (UArg)clientfd; taskParams.stackSize = 1280; taskHandle = Task_create((Task_FuncPtr)tcpWorker, &taskParams, &eb); if (taskHandle == NULL) { System_printf("Error: Failed to create new Task\n"); close(clientfd); } /* addrlen is a value-result param, must reset for next accept call */ addrlen = sizeof(clientAddr); } System_printf("Error: accept failed.\n"); }
Hi,
veeraswamy kakita8 said:Now we are looking for TCP Server in the same application to receive data from Server.
I'm a bit confused. Your application is an TCP server (the tcpHandler2) and you want to connect to another "server"? Or do you mean to say that you want external clients to connect to your TCP server (your application tcpHandle2)? Normally, a server waits for incoming connections from the clients. I don't know how can two servers communicate with each other if both are waiting to "accept" from the clients. Once you elaborate your intention, I can forward you question to our NDK expert if what you are looking is possible.
Our aim is to build application software as TCP Client and as well TCP Server with two different port number.
TCP Client used to push data to central server on port no 5000 using tcpHandler task and is working fine
TCP Server used to receive data from Central Server on port no 5001, tcpHandler2 created to get data from server but it's not accepting connection return -1
Please suggest us how to configure socket in these two scenarios
Hi,
Does the TCP server (tcpHandler2) on your application work alone without the client (tcpHandler)?
As suggested, disabled tcpHandler task and tested with tcpHandler2 task, it's not accepting connection from client or not establishing connection with Server.
Individually tcpHandler task sending data to server continuously, after some idle (>8mins) time, this task is not running. At this point of time device not even pinging
Can you please suggest us how to resolve above two issue
Hi,
veeraswamy kakita8 said:As suggested, disabled tcpHandler task and tested with tcpHandler2 task, it's not accepting connection from client or not establishing connection with Server.
Have you tried the tcpEcho example which is a server application. Can you try this example and see if your external client can connect to it?
veeraswamy kakita8 said:Individually tcpHandler task sending data to server continuously, after some idle (>8mins) time, this task is not running. At this point of time device not even pinging
I don't know what is the problem that the application only runs 8min? Do you have enough stack and heap? I will need to forward your question to our NDK expert. In the meantime, please use the Wireshark to capture the traffic around the time when it fails.
Hello veeraswamy kakita8,
In addition to the questions Charles asked I would like to know how exactly your tcpHandler2 task is failing with -1. It sounds like it is failing at accept(). Could you modify your code to print the errno value from accept? It would look something like this after the while loop:
System_printf("accept() errno value = %d", errno);
Again please send the wireshark capture of your failure case with tcpHandler failing after 8 minutes.
Regards,
Dalton
connect() errno value = 60
When I am trying with TCP ECHO example file following message printing on console and remains in idle loop
Starting the TCP Echo example
System provider is set to SysMin. Halt the target to view any SysMin contents in ROV.
Service Status: DHCPC : Enabled : : 000
Service Status: DHCPC : Enabled : Running : 000
Hello veeraswamy kakita8,
Thanks for providing that information, but I think I misunderstood something. 60 translates into ETIMEDOUT which makes sense for connect(), but not accept(). On top of that I thought you were only getting a -1 error in tcpHandler2(). I don't see any connect() calls there in the code you provided me. Could you help clear up this confusion for me?
As for why connect() would get that error code in the first place, I think we will need to inspect the wireshark logs to figure out why the server isn't responding.
Regards,
Dalton