[英]UDP echo on Nucleo STM32F7
I try to make an UDP echo serveur on NUCLEO-F746ZG but when i start the client, my board make just one answer. 我尝试在NUCLEO-F746ZG上创建UDP回显服务,但是当我启动客户端时,我的开发板仅给出一个答案。
This is my thread code : 这是我的线程代码:
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);
}
}
}
The client side working on the computer: 在计算机上工作的客户端:
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
This is expected. 这是预期的。
err = netconn_bind(conn, '0xc0a8b26f', 8);
is called only at the start of your thread and then inside is while loop which works all the time. 仅在线程开始时调用,然后在while循环内部一直有效。 For example, only 1 execution is allowed.
例如,只允许执行1次。
You might consider rewriting this function to something similar to this: 您可能会考虑将此函数重写为类似于以下内容的代码:
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);
}
}
}
You see, it is very easy to format code 您看,格式化代码非常容易
The solution for an EchoServeur is : EchoServeur的解决方案是:
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_sendto(conn,buf,addr,port);
netbuf_delete(buf);
}
}
} else {
netconn_delete(conn);
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.