Here is the scenario where I have TM4C1294 Eval board is acting as a TCP Client. I have multiple TCP Servers connected with the help of the Ethernet Socket. (Block Diagram below)
The TCP Clent will poll data from every server in a sequential pattern. Here the firmware for the TCP client will be in a loop and it creates a socket, connects to the server issues commands and gets the responses and disconnects. Then connects to another server and this continues.
I could iterate till 10 connections after that I am unable to create sockets. socket() returns error. I know something is missed but could not figure it out. Something tin the creation of the sockets and freeing up of the sockets have to be done as this can iterate over many numbers of times.
Here is the code as below, (tcpWorker function). The code is a modified version of the sample TCP Client Echo code available.
Void tcpWorker(UArg arg0, UArg arg1) { int clientfd = (int)arg0; int bytesRcvd; int bytesSent; int count = 0; int countCmd=0; int status; struct sockaddr_in servAddr; Error_Block eb; System_printf("tcpWorker: start clientfd = 0x%x\n", clientfd); System_flush(); // Creating a socket clientfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (clientfd < 0) { System_printf("tcpHandler: socket failed\n"); close(clientfd); } /// //////////////////// Connecting and fetching data from all the servers ///////////////////////// //while(countCmd < MAX_CMD) //while(countCmd < 20) while(true) { if(enablePool) { System_printf("Reading IP Adddress %d ... \n", countCmd); System_flush(); //////// Read the CMD Ring Buffer ////////// readCmd(); //////////// // Condition-1 ; initConnect --> True , statusCmpIP --> False if(initConnect && !(statusCmpIP) ) { //// For DEBUG /// System_printf("*** CONDITION-1 ***\n"); System_flush(); ///////////////// ////// Reading Server IP Address Data /////// // memset((char *) &servAddr, 0, sizeof(servAddr)); servAddr.sin_family = AF_INET; servAddr.sin_port = htons(PORT_NUM); servAddr.sin_addr.s_addr = inet_addr(bufferStringIP); ////////////////////////////////////////////////////////// // Connecting to a server status = connect(clientfd, (struct sockaddr *) &servAddr, sizeof(servAddr)); if (status < 0) { System_printf("Error: client connect failed. (%d)\n",fdError()); System_flush(); close(clientfd); } //Reset the initialConnect Flag to False (0) initConnect = false; } else if(!(initConnect) && !(statusCmpIP)) { ///// For DEBUG ////// System_printf("*** CONDITION-2 ***\n"); System_flush(); ///////////////////// //Close socket close(clientfd); // Creating a socket clientfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (clientfd < 0) { System_printf("tcpHandler: socket failed\n"); close(clientfd); } /// ////// Reading Server IP Address Data /////// // memset((char *) &servAddr, 0, sizeof(servAddr)); servAddr.sin_family = AF_INET; //servAddr.sin_port = htons(ipT[numIPCount].server_port); servAddr.sin_port = htons(PORT_NUM); servAddr.sin_addr.s_addr = inet_addr(bufferStringIP); ////////////////////////////////////////////////////////// // DEBUG MSG : Connecting to a server System_printf("Connecting to %s\n",bufferStringIP); System_flush(); ///// status = connect(clientfd, (struct sockaddr *) &servAddr, sizeof(servAddr)); if (status < 0) { System_printf("Error: client connect failed. (%d)\n",fdError()); System_flush(); close(clientfd); } } else if(!(initConnect) && statusCmpIP ) { //Dont have to do anything except send System_printf("*** CONDITION-3 ***\n"); System_flush(); } else { // Dont do anything } ///// Send a message to the server ///////// //send(clientfd, msg, strlen(msg), 0); //send(clientfd, bufferModTx, strlen(bufferModTx),0); //// DEBUG : Reading the clientfd System_printf("********** CLIENT ID : %d **********\n", clientfd); System_flush(); // Send First read Command send(clientfd, bufferModTx,MODCMD_SIZE,0); //sendto(clientfd, bufferModTx,MODCMD_SIZE,0,(struct sockaddr *) &servAddr, sizeof(servAddr)); // Init the Error_Block // Error_init(&eb); //////////////////////// ///// Reading the response received ///// //while ((bytesRcvd = recv(clientfd, buffer, TCPPACKETSIZE, 0)) > 0) //while ((bytesRcvd = recvfrom(clientfd, buffer, RESP_SIZE, 0,(struct sockaddr *) &servAddr,&addrlen)) > 0) while ((bytesRcvd = recv(clientfd, buffer, RESP_SIZE, 0)) > 0) { ////// DEBUG MSG ////// System_printf("Received data : %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x \r\n", buffer[0], buffer[1], buffer[2], buffer[3], buffer[4],buffer[5],buffer[6],buffer[7],buffer[8],buffer[9],buffer[10]); System_flush(); // Pushing the response received to the RNG BUFFER for being sent to the CO-PRO UART //// RingBufWrite(&psRingBuf, buffer, RESP_SIZE); // Issue new commands // Read from the CMD RNG BUFFER // Check if the IP Address is same if not disconnect and establish new connection // Send the MODBUS COMMAND send(clientfd, bufferModTx,MODCMD_SIZE,0); //sendto(clientfd, bufferModTx,MODCMD_SIZE,0,(struct sockaddr *) &servAddr, sizeof(servAddr)); //////////// /* if (bytesSent < 0 || bytesSent != bytesRcvd) { System_printf("Error: send failed.\n"); System_flush(); break; } */ count = count + 1; if(count > 10) { break; } } //close(clientfd); // This is now taken care in the routine above countCmd = countCmd+1; count = 0; } // Close for if condition of the enablePool } // Close one iteration of the IP Table ////////////////////////// }