wrnb11 发表于 2017-7-12 10:53:33

lwip UDP实验遇到的问题

我用的是STM32F107VCT6的芯片

u8 *replay = "I got a message!\n";

void APP_udp_server_init(void)
{
//创建控制块
if(pAppUdpSvrPcb == NULL)
pAppUdpSvrPcb = udp_new();

/* Bind the upcb to the UDP_PORT port */
   /* Using IP_ADDR_ANY allow the upcb to be used by any local interface */
   udp_bind(pAppUdpSvrPcb, IP_ADDR_ANY, udplocalport);
   
   /* Set a receive callback for the upcb */
   udp_recv(pAppUdpSvrPcb, APP_udp_callback, replay);   //注册回调函数APP_udp_callback,每当接收到网络调试助手发来的信息就会执行回调函数
}

//回调函数,功能是将I got a message!发回网络调试助手
void APP_udp_callback(void *arg, struct udp_pcb *upcb, struct pbuf *p, struct ip_addr *addr, u16_t port)
{
    struct ip_addr destAddr = *addr;

    if(p != NULL)
   {
       /* Tell the client that we have accepted it */
      memset(p->payload,0,p->len);
      memcpy(p->payload,arg,strlen(arg));
      udp_sendto(upcb,p,&destAddr,port);
       /* Free the p buffer */
       pbuf_free(p);
       udp_disconnect(upcb);
   }
}
理想的结果应该是显示I got a message!    然而结果却为I (I和一个空格)。也就是说每次发送都只能显示两个字符
后来我在memset和memcpy中间也加了udp_sendto(upcb,p,&destAddr,port);,结果变成了,先输出一个空格,再输出I got a message!(如图1所示)

请各位大佬给我解释一下,出现这两种结果的原因,拜托了:'(

pingis58 发表于 2017-7-12 10:53:34

你调试助手给STM32发的数据是什么,多少个字节。
从你往发送pbuf数据拷贝看,你并未指定数据大小。需要发送多少数据。这段代码上看,你发送的字节数,是你接收到的字节数。

斜阳__ 发表于 2017-7-12 13:25:35

帮顶   ,换个调试助手试试

wrnb11 发表于 2017-7-12 16:34:56

斜阳__ 发表于 2017-7-12 13:25
帮顶   ,换个调试助手试试

试了,还是不行

wrnb11 发表于 2017-7-13 08:15:12

pingis58 发表于 2017-7-12 17:06
你调试助手给STM32发的数据是什么,多少个字节。
从你往发送pbuf数据拷贝看,你并未指定数据大小。需要发送 ...

你说得对,我用的是回调函数传入的pbuf,其大小取决于我用网络调试助手发送的数据大小,而我发送的是00,因此只显示I和空格。谢谢你的回答~~
页: [1]
查看完整版本: lwip UDP实验遇到的问题