我尝试使用 LwIP netconn API(在 stm32f4 发现板上)建立多个同时连接。所有这些都在自己的线程中并且完美地工作。但由于某种原因,同一时间只能建立一个连接。
我的代码基于ST echo服务器示例,如下所示:
void myTaskStart(void const * argument)
{
struct netconn *conn, *newconn;
err_t err, accept_err;
struct netbuf* buf;
void* data;
u16_t len;
err_t recv_err;
/* Create a new connection identifier. */
conn = netconn_new(NETCONN_TCP);
if (conn != NULL)
{
err = netconn_bind(conn, NULL, <some port>);
if (err == ERR_OK)
{
/* Tell connection to go into listening mode. */
netconn_listen(conn);
while (1)
{
/* Grab new connection. */
accept_err = netconn_accept(conn, &newconn);
/* Process the new connection. */
if (accept_err == ERR_OK)
{
<do stuff here>
netconn_close(newconn);
netconn_delete(newconn);
}
}
}
else
{
netconn_delete(newconn);
printf(" can not bind TCP netconn");
}
}
else
{
printf("can not create TCP netconn");
}
}
所有线程都在监听不同的端口。但是,如果已经建立了使用不同端口的另一个连接,则所有其他线程都会在 netconn_accept
处失败。它返回ERR_ABRT
,这意味着连接已中止:在接受期间超出 pcb 或超出 netconns
。
我在这里想念什么?
最佳答案
好的。我找到了解决方案。 增加 MEMP_NUM_NETBUF 和 MEMP_NUM_NETCONN 使事情正常进行。
关于multithreading - 使用 netconn 同时处理多个 LwIP 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45190941/