# FreeRTOS\_accept with STM32H757 - LAN8274 TCP IP Server

**URL:** <https://forums.freertos.org/t/freertos-accept-with-stm32h757-lan8274-tcp-ip-server/19832>\
**Category:** Libraries\
**Created:** [April 18, 2024, 1:33pm UTC](https://forums.freertos.org/t/freertos-accept-with-stm32h757-lan8274-tcp-ip-server/19832 "2024-04-18T13:33:05Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![zugo83](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/zugo83/32/3133_2.png) [@zugo83](https://forums.freertos.org/u/zugo83)\
**Post date:** [April 18, 2024, 1:33pm UTC](https://forums.freertos.org/t/freertos-accept-with-stm32h757-lan8274-tcp-ip-server/19832/1 "2024-04-18T13:33:05Z")

</div>

Hello everyone,

I have an issue with FreeRTOS\_accept with STM32H757 and LAN8274 PHY.  
I’m using the network driver provided with FreeRTOS-PLUS-TCP v4.1.0

It successfully accepts one incoming connection from a client, then the client closes the connection. At the second connection attempt, FreeRTOS\_accept does not return a socket. It remains blocked.

I assured that the task is still running by adding a timeout to FreeRTOS\_accept and check that the accept call periodically returns. But it still does not return when there is a new incoming connection.

I can see incoming data when the second connection happens in

```auto
        case eNetworkRxEvent:

```

but prvAcceptWaitClient remains suspended.

I also want to mention that this specific code works in another product with the same MCU where we use a different PHY (in that case it a switch KSZ9563)

Any idea what could go wrong or how can I further debug the issue?

Thank you

Here is the code:

```auto
      struct freertos_sockaddr client;
      struct freertos_sockaddr bind_address;
      Socket_t listening_socket;

      socklen_t client_size = sizeof(client);
      static const TickType_t receive_timeout = portMAX_DELAY;

      WinProperties_t xWinProps;

      /* Fill in the buffer and window sizes that will be used by the socket. */
      xWinProps.lTxBufSize = 4 * ipconfigTCP_TX_BUFFER_LENGTH;
      xWinProps.lTxWinSize = 4 * 2;
      xWinProps.lRxBufSize = ipconfigTCP_RX_BUFFER_LENGTH;
      xWinProps.lRxWinSize = 2;

      /* Attempt to open the socket. */
      listening_socket =
          FreeRTOS_socket(FREERTOS_AF_INET, FREERTOS_SOCK_STREAM, FREERTOS_IPPROTO_TCP);
      configASSERT(listening_socket != FREERTOS_INVALID_SOCKET);

      /* Set a time out so accept() will just wait for a connection. */
      FreeRTOS_setsockopt(
          listening_socket, 0, FREERTOS_SO_RCVTIMEO, &receive_timeout, sizeof(receive_timeout));

      /* Set the window and buffer sizes. */
      FreeRTOS_setsockopt(
          listening_socket, 0, FREERTOS_SO_WIN_PROPERTIES, (void*)&xWinProps, sizeof(xWinProps));

      bind_address.sin_port = FreeRTOS_htons(server->port);
      FreeRTOS_bind(listening_socket, &bind_address, sizeof(bind_address));
      FreeRTOS_listen(listening_socket, max_connections);

      for (;;) {
        server->connected_socket = FreeRTOS_accept(listening_socket, &client, &client_size);
        configASSERT(server->connected_socket != FREERTOS_INVALID_SOCKET);
        xTaskCreate(ConnectionTask,
                    "ConnectionTask",
                    2048,
                    (void*)server,
                    LumTasksPriorities::kNormal,
                    NULL);
      }

```

---

<div class="post-metadata">

**Author:** ![RAc](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rac/32/1389_2.png) [@RAc](https://forums.freertos.org/u/RAc)\
**Post date:** [April 18, 2024, 1:56pm UTC](https://forums.freertos.org/t/freertos-accept-with-stm32h757-lan8274-tcp-ip-server/19832/2 "2024-04-18T13:56:08Z")

</div>

what is the code for your connection task? Do you have a wireshark trace?

---

<div class="post-metadata">

**Author:** ![zugo83](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/zugo83/32/3133_2.png) [@zugo83](https://forums.freertos.org/u/zugo83)\
**Post date:** [April 18, 2024, 2:08pm UTC](https://forums.freertos.org/t/freertos-accept-with-stm32h757-lan8274-tcp-ip-server/19832/3 "2024-04-18T14:08:16Z")

</div>

Connection Task:

```auto
void Server::ConnectionTask(void *parameters) {
  static const TickType_t rx_timeout = pdMS_TO_TICKS(1);
  static const TickType_t tx_timeout = pdMS_TO_TICKS(5000);
  static const TickType_t tcp_echo_shutdown_delay = pdMS_TO_TICKS(5000);

  TickType_t time_on_shutdown;
  uint8_t rx_buffer[ipconfigTCP_MSS];

  Server *server = (Server *)parameters;

  FreeRTOS_setsockopt(server->connected_socket, 0, FREERTOS_SO_RCVTIMEO,
                      &rx_timeout, sizeof(rx_timeout));
  FreeRTOS_setsockopt(server->connected_socket, 0, FREERTOS_SO_SNDTIMEO,
                      &tx_timeout, sizeof(tx_timeout));

  for (;;) {
    memset(rx_buffer, 0x00, ipconfigTCP_MSS);
    BaseType_t bytes_received = FreeRTOS_recv(server->connected_socket,
                                              rx_buffer, sizeof(rx_buffer), 0);
    if (bytes_received > 0) {
      // ToDo(ugo): Move process data to dedicated function
      // Process data
      for (uint16_t index = 0; index < bytes_received; index++) {
        if (server->in_pkt.Parse(rx_buffer[index])) {
          PktRouter::Route(*server->routing_table_, server->in_pkt,
                           &server->out_pkt);
          FreeRTOS_send(server->connected_socket, server->out_pkt.GetBytes(),
                        server->out_pkt.GetSizeInUse(), 0);
        }
      }
    } else if (bytes_received == 0) {
      // Socket timeout, used to push data to connected client
    } else {
      // Socket closed by client or socket error
      break;
    }
  }
  FreeRTOS_shutdown(server->connected_socket, FREERTOS_SHUT_RDWR);

  // Wait for the shutdown to take effect, indicated by FreeRTOS_recv()
  // returning an error.
  time_on_shutdown = xTaskGetTickCount();
  do {
    if (FreeRTOS_recv(server->connected_socket, rx_buffer, ipconfigTCP_MSS, 0) <
        0) {
      break;
    }
  } while ((xTaskGetTickCount() - time_on_shutdown) < tcp_echo_shutdown_delay);

  FreeRTOS_closesocket(server->connected_socket);

  // Delete the task
  vTaskDelete(NULL);
}

```

[rtos-accept-fails.zip](https://forums.freertos.org/uploads/short-url/xinq9vMxS5LYy31NbtMFnmeB2Nx.zip) (614.9 KB)

---

<div class="post-metadata">

**Author:** ![RAc](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rac/32/1389_2.png) [@RAc](https://forums.freertos.org/u/RAc)\
**Post date:** [April 18, 2024, 2:13pm UTC](https://forums.freertos.org/t/freertos-accept-with-stm32h757-lan8274-tcp-ip-server/19832/4 "2024-04-18T14:13:27Z")

</div>

If you re-use the connected\_socket member of your server socket, you have two tasks partying on the same socket concurrently. That calls for trouble.

---

<div class="post-metadata">

**Author:** ![zugo83](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/zugo83/32/3133_2.png) [@zugo83](https://forums.freertos.org/u/zugo83)\
**Post date:** [April 18, 2024, 2:35pm UTC](https://forums.freertos.org/t/freertos-accept-with-stm32h757-lan8274-tcp-ip-server/19832/5 "2024-04-18T14:35:44Z")

</div>

Thank you @RAc for the reply.  
I agree with you that this design does not support multiple connections. max\_connections = 1.

With max\_connections = 1 FreeRTOS\_accept will not accept another connection until the first one is closed. So I should be able to reuse connected\_socket for the second incoming connection.

In any case, FreeRTOS\_accept does not return ever again after the first client disconnection. So reusing connected\_socket is not the issue.

---

<div class="post-metadata">

**Author:** ![RAc](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rac/32/1389_2.png) [@RAc](https://forums.freertos.org/u/RAc)\
**Post date:** [April 18, 2024, 2:46pm UTC](https://forums.freertos.org/t/freertos-accept-with-stm32h757-lan8274-tcp-ip-server/19832/6 "2024-04-18T14:46:49Z")

</div>

No, this is not true. Your accept loop will overwrite the connected\_socket member as soon as a second client will attempt to connect to your end point. At that point, your first task may still be using that member.

Try replacing the xTaskCreate in your listener task with a direct call to the task function. My suspicion is that you will then see orderly communication.

---

<div class="post-metadata">

**Author:** ![zugo83](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/zugo83/32/3133_2.png) [@zugo83](https://forums.freertos.org/u/zugo83)\
**Post date:** [April 19, 2024, 1:20pm UTC](https://forums.freertos.org/t/freertos-accept-with-stm32h757-lan8274-tcp-ip-server/19832/7 "2024-04-19T13:20:04Z")

</div>

@RAc I tried and it is as I say. With

```auto
FreeRTOS_listen(listening_socket, 1);

```

FreeRTOS\_accept does not return a second time until the first socket is closed. Anyway it is good you pointed out so I’ll change it when we will support multiple connections.

The problem was with xApplicationGetRandomNumber.  
I didn’t initialized properly the HW so the function would not return a random number, so the second time the client would try to connect, ulInitialSequenceNumber would be 0.

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [April 21, 2024, 9:46am UTC](https://forums.freertos.org/t/freertos-accept-with-stm32h757-lan8274-tcp-ip-server/19832/8 "2024-04-21T09:46:07Z")

</div>

Thank you for reporting back!
