Here is my server code:
static void prvSimpleServerTask( void *pvParameters )
{
#if (INFOINSHARK == 1)
Socket_t xSharkWinSocket;
struct freertos_sockaddr xSharkWinAddress;
#endif
Socket_t xThisServerSocket, xThatClientSocket, xThisChildSocket;
struct freertos_sockaddr xThisServerSocketAddress, xThatClientSocketAddress;
static const TickType_t xTimeOut = pdMS_TO_TICKS( 200 );
static const TickType_t xListingTimeOut = pdMS_TO_TICKS( 2000 );
uint32_t ulCount = 0UL;
int32_t status, lBytesReceived;
#if( ipconfigUSE_TCP_WIN == 1 )
WinProperties_t xWinProps;
xWinProps.lTxBufSize = ipconfigTCP_TX_BUFFER_LENGTH;
xWinProps.lTxWinSize = 2;
xWinProps.lRxBufSize = ipconfigTCP_RX_BUFFER_LENGTH;
xWinProps.lRxWinSize = 2;
#endif /* ipconfigUSE_TCP_WIN */
#if (INFOINSHARK == 1)
xSharkWinSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP);
configASSERT( xSharkWinSocket != FREERTOS_INVALID_SOCKET );
memset( &xSharkWinAddress, 0, sizeof(xSharkWinAddress) );
xSharkWinAddress.sin_address.ulIP_IPv4 = FreeRTOS_inet_addr( "192.168.0.3" );
xSharkWinAddress.sin_family = FREERTOS_AF_INET4;
xSharkWinAddress.sin_port = FreeRTOS_htons( 10000 );
#endif
vTaskDelay( pdTICKS_TO_MS( 1000 ) );
for( ;; )
{
xThisServerSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_STREAM, FREERTOS_IPPROTO_TCP );
configASSERT( xThisServerSocket != FREERTOS_INVALID_SOCKET );
FreeRTOS_setsockopt( xThisServerSocket, 0, FREERTOS_SO_RCVTIMEO, &xListingTimeOut, sizeof( xListingTimeOut ) );
FreeRTOS_setsockopt( xThisServerSocket, 0, FREERTOS_SO_WIN_PROPERTIES, ( void * ) &xWinProps, sizeof( xWinProps ) );
memset( &xThisServerSocketAddress, 0, sizeof(xThisServerSocketAddress) );
xThisServerSocketAddress.sin_address.ulIP_IPv4 = FreeRTOS_inet_addr( "192.168.0.2" );
xThisServerSocketAddress.sin_family = FREERTOS_AF_INET4;
xThisServerSocketAddress.sin_port = 10000;
xThisServerSocketAddress.sin_port = FreeRTOS_htons( xThisServerSocketAddress.sin_port );
FreeRTOS_bind( xThisServerSocket, NULL, sizeof(xThisServerSocketAddress));
#if (INFOINSHARK == 1)
strcpy( WinInfoString, "FreeRTOS_bind xThisServerSocket");
FreeRTOS_sendto(xSharkWinSocket, WinInfoString, strlen (WinInfoString), 0, &xSharkWinAddress, sizeof(xSharkWinAddress));
#endif
volatile status = FreeRTOS_listen(xThisServerSocket, 5);
if (status == 0)
{
#if (INFOINSHARK == 1)
strcpy( WinInfoString, "FreeRTOS_listen without error");
FreeRTOS_sendto(xSharkWinSocket, WinInfoString, strlen (WinInfoString), 0, &xSharkWinAddress, sizeof(xSharkWinAddress));
#endif
}
else
{
#if (INFOINSHARK == 1)
strcpy( WinInfoString, "FreeRTOS_listen with error");
FreeRTOS_sendto(xSharkWinSocket, WinInfoString, strlen (WinInfoString), 0, &xSharkWinAddress, sizeof(xSharkWinAddress));
#endif
}
do
{
xThisChildSocket = FreeRTOS_accept(xThisServerSocket, &xThatClientSocketAddress, sizeof( xThatClientSocketAddress ));
configASSERT( xThisChildSocket != FREERTOS_INVALID_SOCKET );
if (xThisChildSocket == 0)
{
#if (INFOINSHARK == 1)
strcpy( WinInfoString, "Timeout FreeRTOS_accept");
FreeRTOS_sendto(xSharkWinSocket, WinInfoString, strlen (WinInfoString), 0, &xSharkWinAddress, sizeof(xSharkWinAddress));
#endif
}
else
{
#if (INFOINSHARK == 1)
strcpy( WinInfoString, "!!! xThisChildSocket have been got !!!");
while(1){
FreeRTOS_sendto(xSharkWinSocket, WinInfoString, strlen (WinInfoString), 0, &xSharkWinAddress, sizeof(xSharkWinAddress));
vTaskDelay( pdTICKS_TO_MS( 1000 ) );
}
#endif
}
vTaskDelay( pdTICKS_TO_MS( 50 ) );
}
while (xThisChildSocket == 0);
#if (INFOINSHARK == 1)
strcpy( WinInfoString, "xThisChildSocket = FreeRTOS_accept");
FreeRTOS_sendto(xSharkWinSocket, WinInfoString, strlen (WinInfoString), 0, &xSharkWinAddress, sizeof(xSharkWinAddress));
#endif
for(;;)
{
volatile lBytesReceived = FreeRTOS_recv( xThisChildSocket, &cString, 5000, 0 );
if (lBytesReceived > 0)
{
#if (INFOINSHARK == 1)
strcpy( WinInfoString, "FreeRTOS_recv > 0");
FreeRTOS_sendto(xSharkWinSocket, WinInfoString, strlen (WinInfoString), 0, &xSharkWinAddress, sizeof(xSharkWinAddress));
#endif
}
if (lBytesReceived == 0)
{
#if (INFOINSHARK == 1)
strcpy( WinInfoString, "FreeRTOS_recv == 0");
FreeRTOS_sendto(xSharkWinSocket, WinInfoString, strlen (WinInfoString), 0, &xSharkWinAddress, sizeof(xSharkWinAddress));
#endif
}
if (lBytesReceived < 0)
{
#if (INFOINSHARK == 1)
strcpy( WinInfoString, "FreeRTOS_recv < 0");
FreeRTOS_sendto(xSharkWinSocket, WinInfoString, strlen (WinInfoString), 0, &xSharkWinAddress, sizeof(xSharkWinAddress));
#endif
break;
}
}
FreeRTOS_shutdown( xThisChildSocket, FREERTOS_SHUT_RDWR );
#if (INFOINSHARK == 1)
strcpy( WinInfoString, "FREERTOS_SHUT_RDWR");
FreeRTOS_sendto(xSharkWinSocket, WinInfoString, strlen (WinInfoString), 0, &xSharkWinAddress, sizeof(xSharkWinAddress));
#endif
while( FreeRTOS_recv( xThisChildSocket, cString, 5000, 0 ) >= 0 )
{
vTaskDelay( pdTICKS_TO_MS( 250 ) );
}
FreeRTOS_closesocket( xThisChildSocket );
#if (INFOINSHARK == 1)
strcpy( WinInfoString, "FreeRTOS_closesocket xThisChildSocket");
FreeRTOS_sendto(xSharkWinSocket, WinInfoString, strlen (WinInfoString), 0, &xSharkWinAddress, sizeof(xSharkWinAddress));
#endif
vTaskDelay( pdTICKS_TO_MS( 1000 ) );
}
}