小编写这篇博客主要是面向liunx sockfd的天气获取学习由于下载到开发板上时开发板并没有这些库用纯 C 套接字实现 HTTP GET不依赖任何外部库就可以在开发板上运行代码实现天气的获取。获取天气信息的链接https://open-meteo.com/一程序思路DNS解析将域名api.open-meteo.com转换成IP地址。创建socket建立本地的网络通信端点。配置服务器地址填充sockaddr_in结构体。建立TCP连接主动连接到远程服务器。构建HTTP请求报文符合HTTP/1.1规范的GET请求。发送请求通过socket发送数据。接收响应循环读取直到服务器关闭连接。解析JSON从HTTP响应体中提取温度、风速和天气代码。天气代码转文本根据WMO标准代码翻译成中文。关闭连接释放资源二代码中需要注意的部分在构建http请求报文中sprintf(request, GET /v1/forecast?latitude18.8093longitude110.017current_weathertrue HTTP/1.1\r\n Host: api.open-meteo.com\r\n Connection: close\r\n\r\n );代码当中latitude18.8093longitude110.017改成自己需要的地点其他的小编没有看到什么需要修改的。三完整的代码可以直接编译#include stdio.h #include sys/socket.h #include sys/types.h #include netinet/in.h #include arpa/inet.h #include string.h #include stdlib.h #include netdb.h #include unistd.h #define HOST api.open-meteo.com//该网站不需要api key #define PORT 80 //翻译:天气代码 const char* get_weather_desc(int code) { switch(code) { case 0: return 晴天; case 1: return 大部晴; case 2: return 多云; case 3: return 阴天; case 45: return 雾; case 51: return 小雨; case 53: return 中雨; case 55: return 大雨; case 61: return 小雨; case 63: return 中雨; case 65: return 大雨; case 71: return 小雪; case 73: return 中雪; case 75: return 大雪; default: return 未知天气; } } //获取json的解析包 char *get_json_body(char *response) { char *p strstr(response, \r\n\r\n); if(!p)return NULL; return p 4; } //获取json中的温度/风速的代码 float get_float_value(char *json, const char *key) { char *p strstr(json, key); if( !p ) return -1; p strchr(p, :); if(!p)return -1; return atof(p1); } int get_int_value(char *json, const char *key) { char *p strstr(json, key); if( !p ) return -1; p strchr(p, :); if(!p)return -1; return atof(p1); } int main() { //1.DNS域名解析 struct hostent *host; host gethostbyname(HOST); if(host NULL) { printf(DNS域名解析失败\n); return -1; } //2.创建套接字 int sockfd socket(AF_INET, SOCK_STREAM, 0);//(1)ipv4协议族用于tcp,(2)流式套接字,(3)一般默认值为0 if(sockfd 0) { printf(创建sockfd失败); } //3.配置服务器地址 struct sockaddr_in server_addr; server_addr.sin_family AF_INET;//ipv4协议族 server_addr.sin_port htons(PORT);//网络字节序 server_addr.sin_addr *((struct in_addr*)host-h_addr);//解析得到的ip地址 memset(server_addr.sin_zero, 0, sizeof(server_addr.sin_zero));//(1)目标的起始地址(2)填充的目标值3填充的字节数 //4.建立TCP连接 int ret connect(sockfd, (struct sockaddr*)server_addr, sizeof(server_addr));//tcp客户端才需要(1)套接字描述符(2)服务器地址(3)服务器地址的字节数 if(ret 0) { perror(连接失败); return -1; } printf(连接成功\n); //5.构建http请求报文 char request[1024]; sprintf(request, GET /v1/forecast?latitude18.8093longitude110.017current_weathertrue HTTP/1.1\r\n Host: api.open-meteo.com\r\n Connection: close\r\n\r\n ); //6.发送请求 send(sockfd, request, strlen(request), 0); //7.接收数据 printf(接收数据- - - - - - - -\n); char response[8192]; int total 0; int len 0; while ((len recv(sockfd, response total, sizeof(response) - total - 1, 0)) 0) { total len; } response[total] \0; //8.解析数据 // printf(\n响应数据:\n%s, response); char *json get_json_body(response); // printf(%s\n, json); //8.1 解析current_weather对象 char* cw strstr(json, \current_weather\); cw strchr(cw, {); float temp get_float_value(cw, \temperature\); float wind get_float_value(cw, \windspeed\); int code get_int_value(cw, \weathercode\); //解析天气代码 const char *weather get_weather_desc(code); printf(\n 解析结果 \n); printf(\n 长沙当前天气 \n); printf(温度: %.2f\n, temp); printf(风速: %.2f\n, wind); printf(天气: %s\n, weather); //9.关闭套接字 close(sockfd); return 0; }编译的结果是连接成功 接收数据- - - - - - - - 解析结果 长沙当前天气 温度: 22.70 风速: 7.60 天气: 小雨小编使用的配置环境是vscode 和 ubuntu小编只是一个初学者目前需要学的东西还有很多。