LWIP的DHCP验证

一、配置DHCP功能

void LwIP_Init( void )
{
  struct ip_addr ipaddr;
  struct ip_addr netmask;
  struct ip_addr gw;

   lwip_init(); 

#if LWIP_DHCP                               //
  ipaddr.addr = 0;
  netmask.addr = 0;
  gw.addr = 0; 
#else                                       //
  IP4_ADDR(&ipaddr, 192, 168, 0, 18);       //
  IP4_ADDR(&netmask, 255, 255, 255, 0);     //
  IP4_ADDR(&gw, 192, 168, 0, 1);            //

#endif


  netif_add(&enc28j60, &ipaddr, &netmask, &gw, NULL, &ethernetif_init, &ethernet_input);


  netif_set_default(&enc28j60);


#if LWIP_DHCP               
  /*  Creates a new DHCP client for this interface on the first call.
  Note: you must call dhcp_fine_tmr() and dhcp_coarse_tmr() at
  the predefined regular intervals after starting the client.
  You can peek in the netif->dhcp struct for the actual DHCP status.*/
  dhcp_start(&enc28j60);    
#endif

  /*  When the netif is fully configured this function must be called.*/
  netif_set_up(&enc28j60); //
}

二、配置lwipopts.h文件

/* ---------- DHCP---------- */

#define LWIP_DHCP               1
 #define DHCP_DOES_ARP_CHECK     1

可以选择开启调试模式

/*
   ----------------------------------------
   ---------- Lwip调试配置----------
   ----------------------------------------
*/
#define  LWIP_DEBUG                    1 //
#define TCP_DEBUG                       LWIP_DBG_OFF
#define DHCP_DEBUG                      LWIP_DBG_ON

三、定时轮询


        #if LWIP_DHCP
  /* Fine DHCP periodic process every 500ms */
  if (localtime - DHCPfineTimer >= DHCP_FINE_TIMER_MSECS)
  {
    DHCPfineTimer =  localtime;
    dhcp_fine_tmr();//请求应答超时处理
  }

  /* DHCP Coarse periodic process every 60s */
  if (localtime - DHCPcoarseTimer >= DHCP_COARSE_TIMER_MSECS)
  {
    DHCPcoarseTimer =  localtime;
    dhcp_coarse_tmr();//地址租用情况到期处理
  }
    #endif

四、以上就是全部的配置,可以把模块连接到路由器上,路由器一般默认会开启DHCP功能。
这里写图片描述
图中的匿名主机就是stm32设备。进一步打开可以看到路由器分配的地址。
这里写图片描述

同时可以看到相应的效果:
在浏览器中输入192.168.0.103,同样可以访问到web服务器。
这里写图片描述
五、工程源码
http://download.csdn.net/detail/crjmail/9875450

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值