打印
[STM32F4]

LwIP UDP 传输PC端收不到包,而wireshark能够抓到包

[复制链接]
1440|1
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
yzwang2001|  楼主 | 2017-3-29 11:40 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
使用LwIP UDP 协议实现与PC端通讯。现象为:
如果板卡作为客户端给PC发送报文,PC端应用不能收到包,而用wireshark能够抓到包。

如果板卡作为服务端能够接收到PC端发送的报文,也能够恢复,但PC端应用不能收到,WireShark也能够抓到报文。

哪位大侠有解决思路?

程序部分如下:
发送部分
void udp_connect_send(void)
{
  struct udp_pcb *upcb;
  struct pbuf *p;
  struct ip_addr DestIPaddr;
  struct ip_addr SrcIPaddr;
err_t err;
      
  /* Create a new UDP control block  */
  upcb = udp_new();
  
  if (upcb!=NULL)
  {
    /*assign destination IP address */
    IP4_ADDR( &DestIPaddr, DEST_IP_ADDR0, DEST_IP_ADDR1, DEST_IP_ADDR2, DEST_IP_ADDR3 );
  
    /* configure destination IP address and port */
    err= udp_connect(upcb, &DestIPaddr, UDP_SERVER_PORT);
   
    if (err == ERR_OK)
    {
      sprintf((char*)data, "sending udp client message %d", (int*)message_count);
  
      /* allocate pbuf from pool*/
      p = pbuf_alloc(PBUF_TRANSPORT,strlen((char*)data), PBUF_POOL);
      
      if (p != NULL)
      {
        /* copy data to pbuf */
        pbuf_take(p, (char*)data, strlen((char*)data));
         
        /* send udp data */
        err = udp_send(upcb, p);
        
        /* free pbuf */
        pbuf_free(p);
      }
      else
      {
        /* free the UDP connection, so we can accept new clients */
        udp_remove(upcb);
        #ifdef SERIAL_DEBUG
        printf("\n\r can not allocate pbuf ");
        #endif
      }
    }
    else
    {
      /* free the UDP connection, so we can accept new clients */
      udp_remove(upcb);
      #ifdef SERIAL_DEBUG
       printf("\n\r can not connect udp pcb");
      #endif
    }
  }
  else
  {
    #ifdef SERIAL_DEBUG
     printf("\n\r can not create udp pcb");
    #endif
  }
}


服务部分
void udp_receive_callback(void *arg, struct udp_pcb *upcb, struct pbuf *p, struct ip_addr *addr, u16_t port)
{
  /* Connect to the remote client */
  udp_connect(upcb, addr, UDP_CLIENT_PORT);
   
  /* Tell the client that we have accepted it */
  udp_send(upcb, p);
  /* free the UDP connection, so we can accept new clients */
  udp_disconnect(upcb);

  /* Free the p buffer */
  pbuf_free(p);

listen = 0;   
}

static void StartListen(void)
{
  err_t err;

if(!listen) {
  listen = 1;
  rcvpcb = udp_new();
  
    if (rcvpcb)
    {
      /* Bind the rcvpcb to the UDP_PORT port */
      /* Using IP_ADDR_ANY allow the upcb to be used by any local interface */
      err = udp_bind(rcvpcb, IP_ADDR_ANY, UDP_SERVER_PORT);
      
      if(err == ERR_OK)
      {
        /* Set a receive callback for the upcb */
        udp_recv(rcvpcb, udp_receive_callback, NULL);
      }
      else
      {
        udp_remove(rcvpcb);
        printf("can not bind pcb");
      }
    }
    else
    {
      printf("can not create pcb");
    }
}
}


沙发
li880wert| | 2017-3-29 14:58 | 只看该作者
你这是2个程序都是一次性连接的,UDP client 少了个回调。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

主题

1

帖子

0

粉丝