我尝试在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发布于 2017-06-01 15:26:07
这是意料之中的。
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);
}
}
}你看,格式化代码非常容易
发布于 2017-06-01 17:33:34
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);
}
}
}
}https://stackoverflow.com/questions/44300923
复制相似问题