static void TcpServerTask( void *pvParameters ) { FreeRTOS_printf( ("TcpServerTask created\n" ) ); struct freertos_sockaddr xClient, xBindAddress; Socket_t xListeningSocket, xConnectedSocket; socklen_t xSize = sizeof( xClient ); static const TickType_t xReceiveTimeOut = portMAX_DELAY; const BaseType_t xBacklog = 20; #define BUFFER_SIZE 1500 char *cRxedData = ( char * ) pvPortMalloc( BUFFER_SIZE ); BaseType_t lBytesReceived; ( void ) pvParameters; /* Attempt to open the socket. */ xListeningSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_STREAM, /* SOCK_STREAM for TCP. */ FREERTOS_IPPROTO_TCP ); configASSERT( cRxedData != NULL ); /* Check the socket was created. */ configASSERT( xListeningSocket != FREERTOS_INVALID_SOCKET ); /* If FREERTOS_SO_RCVBUF or FREERTOS_SO_SNDBUF are to be used with FreeRTOS_setsockopt() to change the buffer sizes from their default then do it here!. (see the FreeRTOS_setsockopt() documentation. */ /* If ipconfigUSE_TCP_WIN is set to 1 and FREERTOS_SO_WIN_PROPERTIES is to be used with FreeRTOS_setsockopt() to change the sliding window size from its default then do it here! (see the FreeRTOS_setsockopt() documentation. */ /* Set a time out so accept() will just wait for a connection. */ FreeRTOS_setsockopt( xListeningSocket, 0, FREERTOS_SO_RCVTIMEO, &xReceiveTimeOut, sizeof( xReceiveTimeOut ) ); /* Set the listening port to 1337. */ xBindAddress.sin_port = ( uint16_t ) 1337; xBindAddress.sin_port = FreeRTOS_htons( xBindAddress.sin_port ); /* Bind the socket to the port that the client RTOS task will send to. */ FreeRTOS_bind( xListeningSocket, &xBindAddress, sizeof( xBindAddress ) ); /* Set the socket into a listening state so it can accept connections. The maximum number of simultaneous connections is limited to 20. */ FreeRTOS_listen( xListeningSocket, xBacklog ); for(;;) { const TickType_t xTimeOut_10_seconds = pdMS_TO_TICKS( 10000U ); /* Wait for incoming connections. */ xConnectedSocket = FreeRTOS_accept( xListeningSocket, &xClient, &xSize ); configASSERT( xConnectedSocket != FREERTOS_INVALID_SOCKET ); FreeRTOS_printf( ("TcpServerTask accepted\n" ) ); /* Set a time out so accept() will just wait for a connection. */ FreeRTOS_setsockopt( xConnectedSocket, 0, FREERTOS_SO_RCVTIMEO, &( xTimeOut_10_seconds ), sizeof( xTimeOut_10_seconds ) ); /* Receive another block of data into the cRxedData buffer. */ //lBytesReceived = FreeRTOS_recv( xListeningSocket, cRxedData, BUFFER_SIZE, 0 ); lBytesReceived = FreeRTOS_recv( xConnectedSocket, cRxedData, BUFFER_SIZE, 0 ); FreeRTOS_printf( ( "TcpServerTask recv %ld\n", lBytesReceived ) ); if( lBytesReceived > 0 ) { BaseType_t xAlreadyTransmitted = 0, xBytesSent = 0; size_t xLenToSend; /* Data was received, process it here. */ /* Keep sending until the entire buffer has been sent. */ while( xAlreadyTransmitted < lBytesReceived ) { /* How many bytes are left to send? */ xLenToSend = lBytesReceived - xAlreadyTransmitted; xBytesSent = FreeRTOS_send( xConnectedSocket, /* The socket being sent to. */ &( cRxedData[ xAlreadyTransmitted ] ), /* The data being sent. */ xLenToSend, /* The remaining length of data to send. */ 0 ); /* ulFlags. */ FreeRTOS_printf( ("TcpServerTask xBytesSent rc %ld\n", xBytesSent ) ); if( xBytesSent >= 0 ) { /* Data was sent successfully. */ xAlreadyTransmitted += xBytesSent; } } } /* Attempt graceful shutdown. */ FreeRTOS_shutdown( xConnectedSocket, FREERTOS_SHUT_RDWR ); while( ( lBytesReceived = FreeRTOS_recv( xConnectedSocket, cRxedData, lBytesReceived, 0 ) ) >= 0 ) { FreeRTOS_printf( ("TcpServerTask flushed %ld\n", lBytesReceived ) ); /* Wait for shutdown to complete. If a receive block time is used then this delay will not be necessary as FreeRTOS_recv() will place the RTOS task into the Blocked state anyway. */ /* Note – real applications should implement a timeout here, not just loop forever. */ } FreeRTOS_closesocket( xConnectedSocket ); FreeRTOS_printf( ("TcpServerTask socket closed\n" ) ); } /* for(;;) */ vPortFree( cRxedData ); /* Shutdown is complete and the socket can be safely closed. */ FreeRTOS_closesocket( xListeningSocket ); FreeRTOS_printf( ("TcpServerTask closed\n" ) ); /* Must not drop off the end of the RTOS task – delete the RTOS task. */ vTaskDelete( NULL ); }