LWIP怎么重新设置ip

irishehe 2013-06-06 02:32:41
我的板子是作为服务器的程序,移植的lwip 我想能够在程序运行过程中重新修改服务器自身的ip地址,可是试了几次还是不行,首先我直接把程序中的 IP4_ADDR(&ipaddr, 192, 168, 1, 18); //设置网络接口的ip地址
IP4_ADDR(&netmask, 255, 255, 255, 0); //子网掩码
IP4_ADDR(&gw, 192, 168, 1, 1); //网关
IP改了,然后 执行tcp_close(iris_pcb);
然后再执行LwIP_Init();
CMD_init();
结果作为client的PC根本就没办法再连上作为服务器的板子了(不管是新设的ip还是以前老的IP)。请各位大虾指点迷津 O(∩_∩)O谢谢
下面是两个函数的具体代码:
void LwIP_Init( void )
{
struct ip_addr ipaddr;
struct ip_addr netmask;
struct ip_addr gw;

/*调用LWIP初始化函数,
初始化网络接口结构体链表、内存池、pbuf结构体*/
lwip_init();

#if LWIP_DHCP //若使用DHCP协议
ipaddr.addr = 0;
netmask.addr = 0;
gw.addr = 0;
#else //
/* //by iris 2013.6.6 //
IP4_ADDR(&ipaddr, 192, 168, 0, 18); //设置网络接口的ip地址
IP4_ADDR(&netmask, 255, 255, 255, 0); //子网掩码
IP4_ADDR(&gw, 192, 168, 0, 1); //网关
*/ //by iris 2013.6.6 //
IP4_ADDR(&ipaddr, 192, 168, 1, 18); //设置网络接口的ip地址
IP4_ADDR(&netmask, 255, 255, 255, 0); //子网掩码
IP4_ADDR(&gw, 192, 168, 1, 1); //网关


#endif

/*初始化enc28j60与LWIP的接口,参数为网络接口结构体、ip地址、
子网掩码、网关、网卡信息指针、初始化函数、输入函数*/
netif_add(&enc28j60, &ipaddr, &netmask, &gw, NULL, ðernetif_init, ðernet_input);

/*把enc28j60设置为默认网卡 .*/
netif_set_default(&enc28j60);


#if LWIP_DHCP //若使用了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); //启动DHCP
#endif

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



struct tcp_pcb *pcb; //定义一个tcp控制块

/* Create a new TCP control block */
pcb = tcp_new(); //给tcp控制块分配内存空间

/* Assign to the new pcb a local IP address and a port number */
/* Using IP_ADDR_ANY allow the pcb to be used by any local interface */
tcp_bind(pcb, IP_ADDR_ANY, 23); //把PCB控制块绑定到本机的所有IP地址,端口为23


/* Set the connection to the LISTEN state */
pcb = tcp_listen(pcb); //监听该端口

/* Specify the function to be called when a connection is established */
tcp_accept(pcb, CMD_accept); //监听的端口接通后调用的函数HelloWorld_accept

iris_pcb=pcb; //the mathod is wrong //by iris 2013.6.3
}
...全文
1291 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
songzi2018 2013-07-16
  • 打赏
  • 举报
回复
借花献佛:luminary的lwip接口处理:位于lwiplib.c文件中,摘取出对你有用的部分,实测过没有任何问题 //***************************************************************************** // //! Change the configuration of the lwIP network interface. //! //! \param ulIPAddr is the new IP address to be used (static). //! \param ulNetMask is the new network mask to be used (static). //! \param ulGWAddr is the new Gateway address to be used (static). //! \param ulIPMode is the IP Address Mode. \b IPADDR_USE_STATIC 0 will force //! static IP addressing to be used, \b IPADDR_USE_DHCP will force DHCP with //! fallback to Link Local (Auto IP), while \b IPADDR_USE_AUTOIP will force //! Link Local only. //! //! This function will evaluate the new configuration data. If necessary, the //! interface will be brought down, reconfigured, and then brought back up //! with the new configuration. //! //! \return None. // //***************************************************************************** void lwIPNetworkConfigChange(unsigned long ulIPAddr, unsigned long ulNetMask, unsigned long ulGWAddr, unsigned long ulIPMode) { // // Check the parameters. // #if LWIP_DHCP && LWIP_AUTOIP ASSERT((ulIPMode == IPADDR_USE_STATIC) || (ulIPMode == IPADDR_USE_DHCP) || (ulIPMode == IPADDR_USE_AUTOIP)); #elif LWIP_DHCP ASSERT((ulIPMode == IPADDR_USE_STATIC) || (ulIPMode == IPADDR_USE_DHCP)); #elif LWIP_AUTOIP ASSERT((ulIPMode == IPADDR_USE_STATIC) || (ulIPMode == IPADDR_USE_AUTOIP)); #else ASSERT(ulIPMode == IPADDR_USE_STATIC); #endif // // Save the network configuration for later use by the private network // configuration change. // g_ulIPAddr = ulIPAddr; g_ulNetMask = ulNetMask; g_ulGWAddr = ulGWAddr; // // Complete the network configuration change. The remainder is done // immediately if not using a RTOS and it is deferred to the TCP/IP // thread's context if using a RTOS. // #if NO_SYS lwIPPrivateNetworkConfigChange((void *)ulIPMode); #else tcpip_callback(lwIPPrivateNetworkConfigChange, (void *)ulIPMode); #endif } //***************************************************************************** // // Completes the network configuration change. This is directly called when // not using a RTOS and provided as a callback to the TCP/IP thread when using // a RTOS. // //***************************************************************************** static void lwIPPrivateNetworkConfigChange(void *pvArg) { unsigned long ulIPMode; struct ip_addr ip_addr; struct ip_addr net_mask; struct ip_addr gw_addr; // // Get the new address mode. // ulIPMode = (unsigned long)pvArg; // // Setup the network address values. // if(ulIPMode == IPADDR_USE_STATIC) { ip_addr.addr = htonl(g_ulIPAddr); net_mask.addr = htonl(g_ulNetMask); gw_addr.addr = htonl(g_ulGWAddr); } #if LWIP_DHCP || LWIP_AUTOIP else { ip_addr.addr = 0; net_mask.addr = 0; gw_addr.addr = 0; } #endif // // Switch on the current IP Address Aquisition mode. // switch(g_ulIPMode) { // // Static IP // case IPADDR_USE_STATIC: { // // Set the new address parameters. This will change the address // configuration in lwIP, and if necessary, will reset any links // that are active. This is valid for all three modes. // netif_set_addr(&g_sNetIF, &ip_addr, &net_mask, &gw_addr); // // If we are going to DHCP mode, then start the DHCP server now. // #if LWIP_DHCP if(ulIPMode == IPADDR_USE_DHCP) { dhcp_start(&g_sNetIF); } #endif // // If we are going to AutoIP mode, then start the AutoIP process // now. // #if LWIP_AUTOIP if(ulIPMode == IPADDR_USE_AUTOIP) { autoip_start(&g_sNetIF); } #endif // // And we're done. // break; } // // DHCP (with AutoIP fallback). // #if LWIP_DHCP case IPADDR_USE_DHCP: { // // If we are going to static IP addressing, then disable DHCP and // force the new static IP address. // if(ulIPMode == IPADDR_USE_STATIC) { dhcp_stop(&g_sNetIF); netif_set_addr(&g_sNetIF, &ip_addr, &net_mask, &gw_addr); } // // If we are going to AUTO IP addressing, then disable DHCP, set // the default addresses, and start AutoIP. // #if LWIP_AUTOIP else if(ulIPMode == IPADDR_USE_AUTOIP) { dhcp_stop(&g_sNetIF); netif_set_addr(&g_sNetIF, &ip_addr, &net_mask, &gw_addr); autoip_start(&g_sNetIF); } #endif break; } #endif // // AUTOIP // #if LWIP_AUTOIP case IPADDR_USE_AUTOIP: { // // If we are going to static IP addressing, then disable AutoIP and // force the new static IP address. // if(ulIPMode == IPADDR_USE_STATIC) { autoip_stop(&g_sNetIF); netif_set_addr(&g_sNetIF, &ip_addr, &net_mask, &gw_addr); } // // If we are going to DHCP addressing, then disable AutoIP, set the // default addresses, and start dhcp. // #if LWIP_DHCP else if(ulIPMode == IPADDR_USE_DHCP) { autoip_stop(&g_sNetIF); netif_set_addr(&g_sNetIF, &ip_addr, &net_mask, &gw_addr); dhcp_start(&g_sNetIF); } #endif break; } #endif } // // Save the new mode. // g_ulIPMode = ulIPMode; }
can123dao 2013-07-16
  • 打赏
  • 举报
回复
引用 1 楼 shij520163 的回复:
楼主,你好,请问目前问题解决了吗?
你也在做LWIP通信么
shij520163 2013-07-03
  • 打赏
  • 举报
回复
楼主,你好,请问目前问题解决了吗?

21,597

社区成员

发帖
与我相关
我的任务
社区描述
硬件/嵌入开发 驱动开发/核心开发
社区管理员
  • 驱动开发/核心开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧