0

我对 lwip 堆栈有点陌生。我正在尝试通过 UDP 协议将一些数据从我的开发板发送到我的电脑。并在他们两个之间使用以太网电缆。

我为我的服务器(源板)提供了一个 IP 地址,即 192.168.1.75:88。而我电脑的ip地址是192.168.1.2:90。当我设置这个配置并运行程序时,我无法用wireshark嗅探任何东西,我的意思是根本没有udp包交换。但是当我将所有目标地址更改为 255.255.255.255 或 0.0.0.0 时,我可以嗅探一些包。

为什么我不能将 udp 包发送到我想要的 ip 地址?

主程序

int main(void)
{
   #define dst_port 88
   #define src_port 90

  #ifdef SERIAL_DEBUG
    DebugComPort_Init();
  #endif

  LCD_LED_Init();
  ETH_BSP_Config();
  LwIP_Init();
  IP4_ADDR(&dstaddr,     0, 0, 0, 0);
  IP4_ADDR(&srcaddr,     192, 168, 1, 75);

  pcb = udp_new();

  udp_bind(pcb, &dstaddr, src_port);
  udp_recv(pcb, RecvUTPCallBack, NULL);
  udp_connect(pcb, &dstaddr, dst_port);

  #ifdef USE_DHCP
  /* Start DHCPClient */
  xTaskCreate(LwIP_DHCP_task, "DHCPClient", configMINIMAL_STACK_SIZE * 2, NULL,DHCP_TASK_PRIO, NULL);
  #endif

  /* Start toogleLed4 task : Toggle LED4  every 250ms */
  xTaskCreate(ToggleLed4, "LED4", configMINIMAL_STACK_SIZE, NULL, LED_TASK_PRIO, NULL);
  xTaskCreate(SendUDP, "UDP", configMINIMAL_STACK_SIZE, NULL, LED_TASK_PRIO, NULL);

  /* Start scheduler */
  vTaskStartScheduler();
  for( ;; );
}

发送UDP任务

void SendUDP(void * pvParameters)
{
  while(1)
  {     
    pcb = udp_new();
    udp_bind(pcb, &dstaddr, src_port);
    udp_recv(pcb, RecvUTPCallBack, NULL);
    udp_connect(pcb, &dstaddr, dst_port);

    pb = pbuf_alloc(PBUF_TRANSPORT, sizeof((str)), PBUF_REF);
    pb->payload = str;
    pb->len = pb->tot_len = sizeof((str));

    udp_sendto(pcb, pb, &dstaddr, dst_port);
    udp_disconnect(pcb);
    udp_remove(pcb);
    pbuf_free(pb);  
    vTaskDelay(1000);
  }
}
4

1 回答 1

0

大约一周前我发现了这一点,但我无法在此处发布答案。

首先,在 main.h 中有一个 ip 定义,比如

/*Static IP ADDRESS*/
#define IP_ADDR0   192
#define IP_ADDR1   168
#define IP_ADDR2   1
#define IP_ADDR3   15

并且此配置在 netconf.h 中使用

IP4_ADDR(&ipaddr, IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3);

这就是为什么服务器的 IP 地址总是 192.168.1.15。

第二,我刚开始使用 netconn API 而不是 raw API,这比 raw API 容易得多。这是我的新 SendwithUDP 功能,它运行良好。

void SendwithUDP(uint16_t *veri, uint8_t length)                                                                    
{
  while(1)
    {
        if(((EventFlags.udp) && (1<<0)) == (1<<0)) 
        {
                        STM_EVAL_LEDToggle(LED3);

                        sendconn = netconn_new( NETCONN_UDP ); 
                        netconn_bind(sendconn, IP_ADDR_ANY, src_port );
                        netconn_connect(sendconn, &clientAddr, 150);                                
                        sendbuf = netbuf_new();
                        data =netbuf_alloc(sendbuf, 2*length);
                        memcpy(data, veri, 2*length);
                        netconn_send(sendconn, sendbuf);    
                        netbuf_free(sendbuf);
                        netbuf_delete(sendbuf);
                        netconn_disconnect(sendconn);
                        netconn_delete(sendconn);       
                        vTaskDelay(10);
            }
      } 
}
于 2014-12-24T14:07:54.923 回答