通过Uip WebClient 实现中应用DNS解析

通过Uip WebClient 实现中应用DNS解析UipWebClient 实现作者 Changing 发表时间 07 2700 42 分类 电子相关 2Comments 前一篇 Uip Stm32 移植问题总结后一篇 UipWebServer 实现 UipWebClient 实现的功能是接入互联网 通过 http 协议访问某个网站 HTTP 是一种应用层协议 基于 TCP IP nbsp nbsp TCP IP 作为传输层协

Uip WebClient 实现

作者:Changing发表时间:07-27 00:42分类:电子相关2 Comments

前一篇:Uip + Stm32移植问题总结

后一篇:Uip WebServer 实现

Uip WebClient 实现的功能是接入互联网,通过http协议访问某个网站。HTTP是一种应用层协议。基于TCP/IP。  TCP/IP作为传输层协议解决数据如何在网络中传输,HTTP作为应用层协议,解决如何包装数据。默认的HTTP访问端口为80端口。
 
Uip + stm32 的移植参见  Uip + Stm32移植问题总结 
 
相关文件:
Apps/resolv.c 文件实现的是DNS,动态域名解析等。
Apps/webclient.c主要实现HTTP的协议的解析。
 
首先需要修改User/uip-con.h配置文件:
#define UIP_CONF_LOGGING 0 //logging off //typedef int uip_tcp_appstate_t; //出错可注释 typedef int uip_udp_appstate_t; //出错可注释 /*#include "smtp.h"*/ /*#include "hello-world.h"*/ /*#include "telnetd.h"*/ /*#include "webserver.h"*/ /*#include "dhcpc.h"*/ /*#include "resolv.h"*/ #include "webclient.h" //包含WebClient 文件 #include "app_call.h" //加入一个Uip的数据接口文件
修改User/mainc  调用相关WebClient函数 配置DNS以及设定页面地址

#include "stm32f10x.h" #include "stdio.h" #include "string.h" #include "uip.h" #include "uip_arp.h" #include "tapdev.h" #include "timer.h" #include "ENC28J60.h" #include "SPI.h" #define PRINTF_ON 1 #define BUF ((struct uip_eth_hdr *)&uip_buf[0]) #ifndef NULL #define NULL (void *)0 #endif /* NULL */ static unsigned char mymac[6] = {0x04,0x02,0x35,0x00,0x00,0x01}; void RCC_Configuration(void); void GPIO_Configuration(void); void USART_Configuration(void); int main(void) { int i; uip_ipaddr_t ipaddr; struct timer periodic_timer, arp_timer; RCC_Configuration(); GPIO_Configuration(); USART_Configuration(); SPInet_Init(); timer_set(&periodic_timer, CLOCK_SECOND / 2); timer_set(&arp_timer, CLOCK_SECOND * 10); SysTick_Config(72000); //以太网控制器驱动初始化 tapdev_init(mymac); //Uip 协议栈初始化 uip_init(); 
 uip_ipaddr(ipaddr, 192, 168, 1, 15); //配置Ip uip_sethostaddr(ipaddr); uip_ipaddr(ipaddr, 192, 168, 1, 1); //配置网关 uip_setdraddr(ipaddr); uip_ipaddr(ipaddr, 255, 255, 255, 0); //配置子网掩码 uip_setnetmask(ipaddr); webclient_init(); resolv_init(); uip_ipaddr(ipaddr, 8,8,8,8); //DNS server ,Google DNS Server resolv_conf(ipaddr); resolv_query("www.ichanging.org"); while(1){ uip_len = tapdev_read(); //从网卡读取数据 if(uip_len > 0) { //如果数据存在则按协议处理 if(BUF->type == htons(UIP_ETHTYPE_IP)) { //如果收到的是IP数据,调用uip_input()处理 uip_arp_ipin(); uip_input(); /* If the above function invocation resulted in data that should be sent out on the network, the global variable uip_len is set to a value > 0. */ if(uip_len > 0) { uip_arp_out(); tapdev_send(); } }else if(BUF->type == htons(UIP_ETHTYPE_ARP)){ //如果收到的是ARP数据,调用uip_arp_arpin处理 uip_arp_arpin(); /* If the above function invocation resulted in data that should be sent out on the network, the global variable uip_len is set to a value > 0. */ if(uip_len > 0) { tapdev_send(); } } }else if(timer_expired(&periodic_timer)){ //查看0.5s是否到了,调用uip_periodic处理TCP超时程序 timer_reset(&periodic_timer); for(i = 0; i < UIP_CONNS; i++) { uip_periodic(i); /* If the above function invocation resulted in data that should be sent out on the network, the global variable uip_len is set to a value > 0. */ if(uip_len > 0) { uip_arp_out(); tapdev_send(); } } for(i = 0; i < UIP_UDP_CONNS; i++) { uip_udp_periodic(i); //处理udp超时程序 /* If the above function invocation resulted in data that should be sent out on the network, the global variable uip_len is set to a value > 0. */ if(uip_len > 0) { uip_arp_out(); tapdev_send(); } } /* Call the ARP timer function every 10 seconds. */ //10s到了就处理ARP if(timer_expired(&arp_timer)) { timer_reset(&arp_timer); uip_arp_timer(); } } } } /*WebClient Set*/ void resolv_found(char *name, u16_t *ipaddr) //DNS 找到对应服务器IP { //u16_t *ipaddr2; if(ipaddr == NULL) { printf("Host '%s' not found.\n", name); } else { printf("Found name '%s' = %d.%d.%d.%d\n", name, htons(ipaddr[0]) >> 8, htons(ipaddr[0]) & 0xff, htons(ipaddr[1]) >> 8, htons(ipaddr[1]) & 0xff); if(webclient_get("www.ichanging.org", 80, "/index.php")) { printf("the connection was initiated"); }else{ printf("the host name could not be found in the cache or TCP connection could not be created."); } } } void webclient_closed(void) { //printf("Webclient: connection closed\n"); } void webclient_aborted(void) { //printf("Webclient: connection aborted\n"); } void webclient_timedout(void) { //printf("Webclient: connection timed out\n"); } void webclient_connected(void) { //printf("Webclient: connected, waiting for data...\n"); } void webclient_datahandler(char *data, u16_t len) { //printf("Webclient: got %d bytes of data.\n", len); } /*Stm32 Set*/ void GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA , &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA , &GPIO_InitStructure); } void RCC_Configuration(void) { /* 定义枚举类型变量 HSEStartUpStatus */ ErrorStatus HSEStartUpStatus; /* 复位系统时钟设置*/ RCC_DeInit(); /* 开启HSE*/ RCC_HSEConfig(RCC_HSE_ON); /* 等待HSE起振并稳定*/ HSEStartUpStatus = RCC_WaitForHSEStartUp(); /* 判断HSE起是否振成功,是则进入if()内部 */ if(HSEStartUpStatus == SUCCESS) { /* 选择HCLK(AHB)时钟源为SYSCLK 1分频 */ RCC_HCLKConfig(RCC_SYSCLK_Div1); /* 选择PCLK2时钟源为 HCLK(AHB) 1分频 */ RCC_PCLK2Config(RCC_HCLK_Div1); /* 选择PCLK1时钟源为 HCLK(AHB) 2分频 */ RCC_PCLK1Config(RCC_HCLK_Div2); /* 设置FLASH延时周期数为2 */ FLASH_SetLatency(FLASH_Latency_2); /* 使能FLASH预取缓存 */ FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); /* 选择锁相环(PLL)时钟源为HSE 1分频,倍频数为9,则PLL输出频率为 8MHz * 9 = 72MHz */ RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9); /* 使能PLL */ RCC_PLLCmd(ENABLE); /* 等待PLL输出稳定 */ while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET); /* 选择SYSCLK时钟源为PLL */ RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); /* 等待PLL成为SYSCLK时钟源 */ while(RCC_GetSYSCLKSource() != 0x08); } /* 打开APB2总线上的GPIOA时钟*/ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1, ENABLE); } void USART_Configuration(void) { USART_InitTypeDef USART_InitStructure; USART_ClockInitTypeDef USART_ClockInitStructure; USART_ClockInitStructure.USART_Clock = USART_Clock_Disable; USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low; USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge; USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable; USART_ClockInit(USART1 , &USART_ClockInitStructure); USART_InitStructure.USART_BaudRate = 9600; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx|USART_Mode_Tx; USART_Init(USART1,&USART_InitStructure); USART_Cmd(USART1,ENABLE); } #if PRINTF_ON int fputc(int ch,FILE *f) { USART_SendData(USART1,(u8) ch); while(USART_GetFlagStatus(USART1,USART_FLAG_TC) == RESET); return ch; } #endif

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/179328.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月26日 下午3:50
下一篇 2026年3月26日 下午3:51


相关推荐

  • web 打印控件_JS插件

    web 打印控件_JS插件 平常浏览网页和文档的时候,随处可见打印两个字,有时候不小心点到,就会弹出一个打印的页面,如果连接了打印机,可以直接调用到打印机进行真实的打印。做为开发人员我们在网页开发过程中经常会有打印页面的需求,目前我正在做浏览器端采用JS方式实现打印这么一个功能,通过JS来实现的方法有很多,这里我分享一下我自已采用的方法,供大家参考。为了节约开发时间,我采用的是第三方打印软件“老牌打印控件WebPrinter”。新版现在已更名为“智睦云打印”,在原来的基础上增加了云打印机的支持,“智睦云打印”可以应用在本..

    2025年7月1日
    5
  • zuul 网关的用途_Zuul网关

    zuul 网关的用途_Zuul网关Zuul的主要作用:1.路由,进行请求转发。2.鉴权,进行身份认证(安全)。3.限流,限制访问的数量,保证服务器的稳定。除此之外还有以下作用4.负载均衡。5.压力测试。6.监控1.Zuul简单使用1.添加依赖org.springframework.cloudspring-cloud-starter-netflix-zuul2.添加@EnableZuulProxy注解@EnableZuulProx…

    2022年8月15日
    10
  • python八进制表示_八进制python

    python八进制表示_八进制python广告关闭腾讯云 11 11 云上盛惠 精选热门产品助力上云 云服务器首年 88 元起 买的越多返的越多 最高返 5000 元 答案 python2 5 及更早版本 可以使用 int 0 2 python2 5 及更早版本 用 noway 来表示二进制文本 python2 6 测试版 可以这样做 0b 或 0b python2 6 测试版 也允许 0o27 或

    2025年6月25日
    3
  • matlab毕业设计题目_matlab毕业设计论文带有程序

    matlab毕业设计题目_matlab毕业设计论文带有程序毕业设计(论文)`题目:院系机械工程系专业班级学生姓名指导教师二○一年六月Matlab的转子故障振动信号分析摘要随着机械行业的日益发展,转子等旋转机械的故障日渐趋多,转子的故障诊断技术受到越来越多的重视,并在世界范围内取得了长足的进步。作为大型机器中不可或缺的部件,有着举足轻重的作用,但,,,,。对发电机及其转子进行状态监测和信号分析,,转子故障类型主要有四种:转子不平衡、、转子…

    2022年10月15日
    3
  • 2026年Python+AI实战入门:一份面向零基础开发者的高效学习蓝图

    2026年Python+AI实战入门:一份面向零基础开发者的高效学习蓝图

    2026年3月15日
    3
  • Sober算子边缘检测与Harris角点检测1「建议收藏」

    Sober算子边缘检测与Harris角点检测1「建议收藏」此篇文章主要介绍了Sobel算子的底层运算规律,和cvHarris的相关介绍Harrisopencv的对应代码cv2.cornerHarris(src,blockSize,ksize,k[,dst[,borderType]])参数类型src-输入灰度图像,float32类型blockSize-用于角点检测的邻域大小,就是上面提到的窗口的尺寸ksize-用于计算梯

    2022年7月14日
    23

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号