Hi all,
I am working on a project that will send packets from a Tiva-C board (TM4C129NCPDT) over TCP to a computer. To accomplish this, I am using TI RTOS with the NDK. When the Tiva-C obtains an IP address from a router, the following task is triggered:
void netIPAddrHook(void) { static bool task_created = false; Task_Handle taskHandle; Task_Params taskParams; Error_Block eb; if(!task_created) { Error_init(&eb); Task_Params_init(&taskParams); taskParams.stackSize = 1024; taskParams.priority = 1; taskHandle = Task_create((Task_FuncPtr)TcpHandler, &taskParams, &eb); if (taskHandle == NULL) { System_printf("netIPAddrHook: Failed to create TcpHandler Task\n"); } System_flush(); task_created = true; } }
This task creates another task which will handle the TCP connection. The TCP task should attempt to create the socket, connect to the server running on the computer, and send packets to the server whenever they become available. If something goes wrong, it should keep trying to reconnect.
void TcpHandler(void) { int16_t sockfd; struct sockaddr_in server_info; struct sPktInfo pkt; // Fill structure with info on server to connect to memset(&server_info, 0, sizeof(server_info)); server_info.sin_family = AF_INET; inet_pton(AF_INET, SERVER_IP, &(server_info.sin_addr)); server_info.sin_port = htons(SERVER_PORT); for(;;) { // Create a new socket if(sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP) != -1) { // If socket creation is successful attempt to connect to server if(connect(sockfd, (struct sockaddr *)&server_info, sizeof(server_info)) != -1) { // If connection is successful, wait to send data for(;;) { // Wait until data has been received via the radio Mailbox_pend(pkt_mbx, &pkt, BIOS_WAIT_FOREVER); // Send data, attempt to reconnect if it fails if(TcpSendAll(sockfd, pkt.data, PACKET_LEN) == -1) break; } } else { DbgPrintf(DBG_INFO, "connect failed: error = %d\n", errno); System_flush(); } // Catch exception from connection or send fails close(sockfd); } else { DbgPrintf(DBG_INFO, "socket creation failed: error = %d\n", errno); System_flush(); } } }
So my here's my issue: once it gets to this task, it creates a socket and tries to connect seven times (I haven't figured out why the connect is failing yet... there are a lot of places this could have gone wrong, and not just in the Tiva-C code, but that's an issue for another day). However, after the seventh time, it is unable to create a socket. I start getting heap warnings telling me that it is out of memory - so perhaps there is a memory leak? Does anyone have thoughts on what may be causing this? I'm pretty new to network programming, so I may be doing something blatantly wrong.
Finally, here is the output to the debugger console:
Using MAC address in flash
Service Status: DHCPC : Enabled : : 000
Service Status: DHCPC : Enabled : Running : 000
Network Added: If-1:192.168.1.101
Service Status: DHCPC : Enabled : Running : 017
00003.501 connect failed: error = 9
00003.501 connect failed: error = 9
00003.501 connect failed: error = 9
00003.501 connect failed: error = 9
00003.502 connect failed: error = 9
00003.502 connect failed: error = 9
00003.502 connect failed: error = 9
ti.sysbios.heaps.HeapMem: line 307: out of memory: handle=0x2001154c, size=1032
00003.503 mmBulkAlloc(): could not allocate memory.
00003.503 out of memory: handle=0x0, size=536873180
00003.503 SBNew: Buffer OOM
00003.503 socket creation failed: error = 12