This task will be launched from vApplicationIPNetworkEventHook() on the server side:
void socket_init(void)
{
if (xMustCreateSockets != pdFALSE)
{
xMustCreateSockets = pdFALSE;
/* Attempt to open the socket. */
xListeningSocket = FreeRTOS_socket(PF_INET,
SOCK_STREAM, /* SOCK_STREAM for TCP. */
IPPROTO_TCP);
/* Check the socket was created. */
configASSERT(xListeningSocket != FREERTOS_INVALID_SOCKET);
/* Set a time out so accept() will just wait for a connection. */
FreeRTOS_setsockopt(xListeningSocket,
0,
FREERTOS_SO_RCVTIMEO,
&xReceiveTimeOut,
sizeof(xReceiveTimeOut));
/* Reuse socket for receiving data*/
BaseType_t xReuseSocket = pdTRUE;
FreeRTOS_setsockopt(xListeningSocket,
0,
FREERTOS_SO_REUSE_LISTEN_SOCKET,
(void*)&xReuseSocket,
sizeof(xReuseSocket));
/* Set the listening port to 7312. */
xBindAddress.sin_port = (uint16_t)7312;
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 (;; )
{
/* Wait for incoming connections. */
xConnectedSocket = FreeRTOS_accept(xListeningSocket, &xClient, &xSize);
printf("CLIENT CONNECTED\n");
configASSERT(xConnectedSocket != FREERTOS_INVALID_SOCKET);
/* Spawn a RTOS task to handle the connection. */
xTaskCreate(prvEchoClientRxTask,
"EchoServer",
65530,
(void*)xConnectedSocket,
3,
NULL);
}
}
}
The prvEchoClientRxTask() function’s content is the same as https://freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_TCP/TCP_Networking_Tutorial_Receiving_TCP_Data.html albeit with long buffer instead of char.
The client side also have a similar task, but I’m keeping the socket as global variable, not sure if anything is affected. I expect only 1 client to connect to 1 server.
void socket_init(void)
{
if (xMustCreateSockets != pdFALSE)
{
xMustCreateSockets = pdFALSE;
/* Set the IP address (192.168.56.234) and port (7312) of the remote socket
to which this client socket will transmit. */
xRemoteAddress.sin_port = FreeRTOS_htons(7312);
xRemoteAddress.sin_addr = FreeRTOS_inet_addr_quick(192, 168, 56, 234);
//while (xTasksAlreadyCreated == pdFALSE) printf("still false");
/* Create a socket. */
xSocket = FreeRTOS_socket(FREERTOS_AF_INET,
FREERTOS_SOCK_STREAM,/* FREERTOS_SOCK_STREAM for TCP. */
FREERTOS_IPPROTO_TCP);
configASSERT(xSocket != FREERTOS_INVALID_SOCKET);
/* Connect to the remote socket. The socket has not previously been bound to
a local port number so will get automatically bound to a local port inside
the FreeRTOS_connect() function. */
if (FreeRTOS_connect(xSocket, &xRemoteAddress, sizeof(xRemoteAddress)) == 0)
{
printf(("CONNECTED TO SERVER, BEGIN SENDING DATA\n"));
xTaskCreate(vTCPSend,"Sending",65530,NULL,3,NULL);
}
}
}