我尝试在 NUCLEO-F746ZG 上创建一个 UDP 回显服务器,但是当我启动客户端时,我的主板只给出一个答案。
这是我的线程代码:
static void udpecho_thread(void *arg)
{
err_t err, recv_err;
LWIP_UNUSED_ARG(arg);
conn = netconn_new(NETCONN_UDP);
if (conn != NULL)
{
err = netconn_bind(conn, '0xc0a8b26f', 8);
if (err == ERR_OK)
{
while (1)
{
recv_err = netconn_recv(conn, &buf);
if (recv_err == ERR_OK)
{
addr = netbuf_fromaddr(buf);
port = netbuf_fromport(buf);
netconn_connect(conn, addr, port);
buf->addr.addr = 0;
netconn_send(conn, buf);
netbuf_delete(buf);
}
}
}
else
{
netconn_delete(conn);
}
}
}
在计算机上工作的客户端:
Hostname 192.168.178.111 resolved as 192.168.178.111
Reply from 192.168.178.111:8, time 46 ms OK
Une connexion existante a dû être fermée par l'hôte distant
Une connexion existante a dû être fermée par l'hôte distant
Une connexion existante a dû être fermée par l'hôte distant
Une connexion existante a dû être fermée par l'hôte distant
Statistics: Received=1, Corupted=0, Lost=0
最佳答案
这是预期的。
err = netconn_bind(conn, '0xc0a8b26f', 8);
仅在线程开始时调用,然后内部是一直有效的 while 循环。例如,只允许执行 1 次。
您可以考虑将此函数重写为类似的内容:
static void udpecho_thread(void *arg) {
err_t err, recv_err;
LWIP_UNUSED_ARG(arg);
while (1) { /* Add this loop */
conn = netconn_new(NETCONN_UDP);
if (conn != NULL) {
err = netconn_bind(conn, '0xc0a8b26f', 8);
if (err == ERR_OK) {
while (1) {
recv_err = netconn_recv(conn, &buf);
if (recv_err == ERR_OK) {
addr = netbuf_fromaddr(buf);
port = netbuf_fromport(buf);
netconn_connect(conn, addr, port);
buf->addr.addr = 0;
netconn_send(conn,buf);
netbuf_delete(buf);
} else {
break; /* Add break to stop inner loop */
}
}
}
netconn_delete(conn);
}
}
}
你看,格式化代码非常容易
关于c - Nucleo STM32F7 上的 UDP 回显,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44300923/