目录

懒人链接:

需求分析

链表实现:

Http实现

tcp实现


懒人链接:

点我下载:https://download.csdn.net/download/qq_25490573/11834524

需求分析

1.实现LinkList双向链表

2.链表管理

3.加载数据库的项

功能实现,嵌入式中智能网关网络代理程序

能够连接串口,wifi(tcp通信,http通信,udp通信)

运行截图:

链表实现:

linklist.h

#ifndef _H_LINKLIST
#define _H_LINKLIST#include "stdio.h"
#include "stdlib.h"
#include "errno.h"extern int errno;   //linux下的全局变量            需要包含头文件errno.htypedef unsigned int u32;
typedef unsigned short u16;
typedef unsigned char u8;#define ID_HTTP            0x01
#define ID_HTTPS        0x02
#define ID_SOCK_TCP     0x03
#define ID_SOCK_UDP     0x04
#define ID_TTY          0x05#define ENABLE          0X01
#define DISABLE         0x00#define BUFFER_SIZE     2048typedef struct node_data    //节点中的数据
{u8 id;     //节点类型u8 flay;    //节点状态int sock_fd;u8 *send_buffer;int (* Init)(struct node_data * data);int (* SendData)(struct node_data * data,void * databuffer);
}node_data;
typedef node_data * node_data_ptr;typedef struct node_t     //链表中的节点
{void *             data;struct node_t *        prev;struct node_t *        next;
}node_t;
typedef node_t * node_ptr;typedef struct _T_list        //双向链表
{u32            limit_size;node_ptr     head;node_ptr   last;
}_T_list;
typedef _T_list* list_ptr;list_ptr Create_list(void);       //创建链表
#ifdef __cplusplus
//extern "C"{
#endif
int list_insert_before(list_ptr * list_head,int num,void * new_node_data);  //在链表前面插入
int list_insert_back(list_ptr * list_last,int num,void * new_node_data);        //在链表后面插入
int list_append(list_ptr * list_name,void * new_node_data);                 //文尾追加
int list_delete(list_ptr * list_head,int begin,int number);                 //从某一个位置删除多少个
int list_delete_one(list_ptr * list_head,int begin);                                                          //删除某一个
int list_set_data(list_ptr * list_name,int num,void * new_node_data);           //设置某个节点数据
void * list_get_data(list_ptr * list_name,int num);                         //得到某个节点数据
void list_travel(list_ptr * list_name,void (*cmd_function)(void *));            //对数据进行一个函数的操作
int list_search(list_ptr * list_name,void * find_value,int(*campare)(void *,void *));//查找基于一个函数规则的节点
#ifdef __cplusplus
//}
#endif
#endif

LinkList.c

#include "linklist.h"list_ptr Create_list(void){list_ptr list_head = NULL;list_head = (list_ptr)malloc(sizeof(_T_list));if(list_head == NULL){errno = ENOMEM; //ENOMEM  linux错误码   没有内存return NULL;}list_head->limit_size = 0;list_head->head = (node_t*)malloc(sizeof(node_t));if(list_head->head == NULL){errno = ENOMEM; //ENOMEM  linux错误码   没有内存return NULL;}list_head->head->data = NULL;list_head->head->next = list_head->head->prev = NULL;list_head->last = list_head->head;return list_head;
}int list_insert_before(_T_list ** list_head,int num,void * new_node_data)
{u32 counter = 0;node_ptr current = NULL;node_ptr new_node = NULL;if(list_head==NULL||(*list_head)==NULL){errno = EINVAL;       //错误的值return -1;}if((*list_head)->limit_size !=0){new_node = (node_ptr)malloc(sizeof(node_ptr));if((*list_head)->head == NULL){errno = ENOMEM; //ENOMEM  linux错误码   没有内存return -1;}new_node->next = new_node->prev = NULL;if(new_node_data == NULL){errno = EINVAL;       //错误的值free(new_node);return -1;}new_node->data = new_node_data;if(num>=0&&num<(*list_head)->limit_size){current = (*list_head)->head;while(counter < num){counter++;current = current->next;}if(counter == 0){(*list_head)->head->prev = new_node;new_node->next = (*list_head)->head;(*list_head)->head = new_node;(*list_head)->limit_size++;return 0;}else if(counter >0){new_node->prev = current->prev;new_node->next = current;new_node->prev->next = new_node;current->prev = new_node;(*list_head)->limit_size++;return 0;}elseerrno = EINVAL;free(new_node);new_node==NULL;return -1;}}else if((*list_head)->limit_size == 0){if(num!=0){errno = EINVAL;return -1;}    (*list_head)->head->data = (void *)new_node_data;(*list_head)->limit_size++;}return 0;
}int list_insert_back(list_ptr * list_last,int num,void * new_node_data)
{u32 counter = 0;node_ptr current = NULL;node_ptr new_node = NULL;if(list_last==NULL||(*list_last)==NULL){errno = EINVAL;       //错误的值return -1;}counter = (*list_last)->limit_size-1;if((*list_last)->limit_size !=0){new_node = (node_ptr)malloc(sizeof(node_ptr));if((*list_last)->head == NULL){errno = ENOMEM; //ENOMEM  linux错误码   没有内存return -1;}new_node->next = new_node->prev = NULL;if(new_node_data == NULL){errno = EINVAL;      //错误的值free(new_node);return -1;}new_node->data = new_node_data;if(num>=0&&num<(*list_last)->limit_size){current = (*list_last)->last;while(counter < num){counter--;current = current->prev;}if(counter == ((*list_last)->limit_size-1)){(*list_last)->last->next = new_node;new_node->prev = (*list_last)->last;(*list_last)->last = new_node;(*list_last)->limit_size++;return 0;}else if(counter >0){new_node->next = current->next;current->next = new_node;new_node->prev = current;new_node->next->prev = new_node;(*list_last)->limit_size++;return 0;}elseerrno = EINVAL;free(new_node);new_node==NULL;return -1;}}else if((*list_last)->limit_size == 0){if(num!=0){errno = EINVAL;return -1;}(*list_last)->head->data = new_node_data;(*list_last)->limit_size++;}return 0;
}int list_append(list_ptr * list_name,void * new_node_data){int a = list_insert_back(list_name,((*list_name)->limit_size-1),new_node_data);return a;
}int list_delete(list_ptr * list_head,int begin,int number)
{int begin_i = 0;node_ptr current = NULL;node_ptr current_h = NULL;node_ptr current_p = NULL;if(list_head==NULL||(*list_head)==NULL){errno = EINVAL;return -1;}if(begin<0||(number+begin)>(*list_head)->limit_size){errno = EINVAL;return -1;}current = (*list_head)->head;current_h = (*list_head)->last;if(begin == 0 && (number+begin)<(*list_head)->limit_size){while(number > 0){current_h = current->next;free(current);current = current_h;number--;}current->prev = NULL;(*list_head)->head = current;(*list_head)->limit_size -= number;return 0;}else if(begin != 0 && (number+begin)==(*list_head)->limit_size){while(number > 0){current = current_h->prev;free(current_h);current_h = current;number--;}current_h->next =NULL;(*list_head)->last = current_h;(*list_head)->limit_size -= number;return 0;}else if(begin != 0 && (number+begin)<(*list_head)->limit_size){while(begin_i < begin){begin++;current = current->next;}current_p = current->prev;while(number > 0){current_h = current->next;free(current);current = current_h;number--;}current_p->next = current;current->prev = current_p;(*list_head)->limit_size -= number;return 0;}else if(begin == 0 && (number+begin)==(*list_head)->limit_size){begin_i=1;while(begin_i<(*list_head)->limit_size){current_h = current->next;free(current);current = current_h;begin_i++;}free((*list_head)->last->data);(*list_head)->last->data = NULL;(*list_head)->last->next = (*list_head)->last->prev = NULL;(*list_head)->head=(*list_head)->last;(*list_head)->limit_size ==0;return 0;}errno = EINVAL;return -1;
}int list_delete_one(list_ptr * list_head,int begin)
{int err = list_delete(list_head,begin,1);return err;
}int list_set_data(list_ptr * list_name,int num,void * new_node_data)
{int begin = 0;node_ptr current = NULL;if(list_name==NULL||(*list_name)==NULL){errno = EINVAL;return -1;}if(begin<0||num>=(*list_name)->limit_size){errno = EINVAL;return -1;}current = (*list_name)->head;while(begin<num){begin++;current = current->next;}free(current->data);current->data = new_node_data;return 0;
}void * list_get_data(list_ptr * list_name,int num)
{int begin = 0;node_ptr current = NULL;if(list_name==NULL||(*list_name)==NULL){errno = EINVAL;return NULL;}  if(begin<0||num>=(*list_name)->limit_size){errno = EINVAL;return NULL;}current = (*list_name)->head;while(begin<num){begin++;current = current->next;}return current->data;
}void list_travel(list_ptr * list_name,void (*cmd_function)(void *))
{node_ptr current = NULL;if(list_name==NULL||(*list_name)==NULL){errno = EINVAL;return ;}if((*list_name)->limit_size==0)return ;current = (*list_name)->head;while(current->next !=NULL){(*cmd_function)(current->data);current = current->next;}(*cmd_function)(current->data);
}int list_search(list_ptr * list_name,void * find_value,int(*campare)(void *,void *))
{int number = 0;node_ptr current = NULL;if(list_name==NULL||(*list_name)==NULL){errno = EINVAL;return -1;}if((*list_name)->limit_size==0)return 0;current = (*list_name)->head;while(current->next !=NULL&&campare(find_value,current->data)){number++;current = current->next;}if(campare(find_value,current->data)){return number;}return -1;
}

Http实现

http.h

#ifndef _HTTP_H_
#define _HTTP_H_#include <stdlib.h>
#include <sys/types.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>
#include "linklist.h"
#include <unistd.h>#define HTTP_PORT 6666
#define HTTP_ADDR "192.168.10.110"int init_http_data(struct node_data * data);
int send_buffer_http_data(struct node_data * data,void * databuffer);void http_init_methor(list_ptr list,void * ndata);
void Init_http(list_ptr list);#endif

http.c

#include "http.h"struct node_data http_data={.id     = ID_HTTP,.flay   = ENABLE,.Init   = init_http_data,.SendData = send_buffer_http_data,
};int init_http_data(struct node_data * data)
{struct sockaddr_in servaddr;   if((data->sock_fd =socket(AF_INET, SOCK_STREAM, 0))<0){printf("create socket error!!!\n");exit(0);}bzero(&servaddr,sizeof(servaddr));servaddr.sin_family=AF_INET;//servaddr.sin_addr.s_addr=htonl(INADDR_ANY);servaddr.sin_port=htons(HTTP_PORT);if(inet_pton(AF_INET,HTTP_ADDR,&servaddr.sin_addr)<=0){printf("inet_pton fail,process stop!\n");exit(0);}if(connect(data->sock_fd,(struct sockaddr *)(&servaddr), sizeof(servaddr)) < 0){printf("connect fail!!! \n");exit(0);}return 0;
}int send_buffer_http_data(struct node_data * data,void * databuffer)
{u8 *http_data =NULL;u8 * length = (u8 *)malloc(128);int len = strlen(databuffer);sprintf(length,"%d",len);http_data = (u8*)malloc(len+1024);memset(http_data,0,sizeof(http_data));strcat(http_data,"POST /webservices/qqOnlineWebSerice.asm/qq");strcat(http_data,"");strcat(http_data,"");strcat(http_data,"");strcat(http_data,length);strcat(http_data,"\r\n");strcat(http_data,databuffer);strcat(http_data,"\r\n\r\n");printf("%s\n",http_data);int ret = write(data->sock_fd,http_data,strlen(http_data));if(ret<0){printf("http send fail!!!,error:%d,result:%s\n",errno,strerror(errno));exit(0);}else{printf("http send sucess!!!,send length:%d\n",len);}free(http_data);free(length);return ret;
}void http_init_methor(list_ptr list,void * ndata)
{if(list==NULL){errno = EINVAL;return ;}list_insert_before(&list,0,ndata);
}
void Init_http(list_ptr list)
{http_init_methor(list,(void *)&http_data);
}

tcp实现

tcp.h

#ifndef _TCP_H_
#define _TCP_H_#include <stdlib.h>
#include <sys/types.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>
#include "linklist.h"
#include <unistd.h>#define TCP_PORT 6666
#define TCP_ADDR "192.168.10.110"int init_tcp_data(struct node_data * data);
int send_buffer_tcp_data(struct node_data * data,void * databuffer);void tcp_init_methor(list_ptr list,void * ndata);
void Init_tcp(list_ptr list);#endif

sock_tcp.c

#include "tcp.h"
static struct node_data tcp_data =
{.id             = ID_SOCK_TCP,.flay   = ENABLE,.Init   = init_tcp_data,.SendData = send_buffer_tcp_data,
};int init_tcp_data(struct node_data * data)
{struct sockaddr_in servaddr;   if((data->sock_fd =socket(AF_INET, SOCK_STREAM, 0))<0){printf("create socket error!!!\n");exit(0);}bzero(&servaddr,sizeof(servaddr));servaddr.sin_family=AF_INET;//servaddr.sin_addr.s_addr=htonl(INADDR_ANY);servaddr.sin_port=htons(TCP_PORT);if(inet_pton(AF_INET,TCP_ADDR,&servaddr.sin_addr)<=0){printf("inet_pton fail,process stop!\n");exit(0);}if(connect(data->sock_fd,(struct sockaddr *)(&servaddr), sizeof(servaddr)) < 0){printf("connect fail!!! \n");exit(0);}return 0;
}int send_buffer_tcp_data(struct node_data * data,void * databuffer)
{printf("%s\n",(char*)databuffer);int ret = write(data->sock_fd,databuffer,strlen(databuffer));if(ret<0){printf("tcp send fail!!!,error:%d,result:%s\n",errno,strerror(errno));exit(0);}else{printf("tcp send sucess!!!,send length:%d\n",(int)strlen(databuffer));}return ret;
}void tcp_init_methor(list_ptr list,void * ndata)
{if(list==NULL){errno = EINVAL;return ;}list_insert_before(&list,0,ndata);
}
void Init_tcp(list_ptr list)
{tcp_init_methor(list,(void *)&tcp_data);
}

最后是main函数

#include "stdio.h"
#include "linklist.h"
#include "http.h"
#include "tcp.h"void node_send_data(void * vdata)
{int ret = 0;node_data_ptr data = (node_data_ptr)vdata;if(data->flay == ENABLE){data->Init(data);ret = data->SendData(data,"hello,world!");printf("type:%d send success!\n",data->id);}}int main(int argc,char*argv[])
{list_ptr remote_list = Create_list();Init_http(remote_list);Init_tcp(remote_list);list_travel(&remote_list, node_send_data);
}

Linux开发——实战(一)LinkList实现智能网关代理相关推荐

  1. 名片识别 php,小程序云开发实战:实现 AI 智能名片识别小程序

    本次分享是针对对小程序·云开发感兴趣的朋友推出的入门课程,内容以基础+实战的方式呈现,通过实战"名片小程序"开发,讲解云开发的功能与开发优势,为开发者提供提高开发效率的方法与灵感. ...

  2. wdm设备驱动程序开发pdf_世界顶级Linux技术大师力作1000页Linux开发实战

    20世纪90年代初,Linux操作系统诞生,随着虚拟化.云计算.大数据.容器技术的出现和人工智能时代的来临,Linux 以迅雷不及掩耳之势飞速发展,占据着整个服务器行业的半壁江山,但同时也面临着巨大的 ...

  3. 小程序·云开发实战:实现 AI 智能名片识别小程序

    开发者在开发小程序时往往需要花费精力进行后端基础设施建设及维护,但利用小程序·云开发开发,则可以跳开这一环节,无需再搭建服务器. 本次分享是针对对小程序·云开发感兴趣的朋友推出的入门课程,内容以基础+ ...

  4. 世界顶级Linux技术大神,耗时一年力作1300页Linux开发实战

    20世纪90年代初,Linux操作系统诞生,随着虚拟化.云计算.大数据.容器技术的出现和人工智能时代的来临,Linux 以迅雷不及掩耳之势飞速发展,占据着整个服务器行业的半壁江山,但同时也面临着巨大的 ...

  5. Linux开发——实战(三)后门木马程序

    目录 端口绑定命令: nc -lvp 6666 木马: 控制台程序 端口绑定命令: nc -lvp 6666 木马: #include "stdio.h" #include &qu ...

  6. springboot+openFeign+nacos+gateway开发实战

    前面说了openFeign整合nacos进行服务之间的调用,本文来说下springboot+openFeign+nacos+gateway开发实战.说下服务网关gateway实战相关的内容. 文章目录 ...

  7. 野火i.MX6ULL Linux开发板资料

    1.配套教程:<i.MX6UL Linux开发实战指南> i.MX6UL Linux开发实战指南在线文档 2.开发板云盘资料(硬件资料.其它软件) 百度云资料链接: https://pan ...

  8. IMX6ULL驱动开发实战连载-01搭建开发环境

    哈喽,大家好.我是小仲.板子在3.31号就收到了,但是,由于最近一直很忙,拖到了现在才开始搭建环境.接下来的一段时间,会基于野火IMX6ULL开发板写一系列教程,主要侧重于驱动和内核的调试技巧.这方面 ...

  9. “百城千园行”活动 四信5G工业智能网关服务园区智能升级

    近日,工信部发布通知,组织开展工业互联网一体化进园区"百城千园行"活动.旨在贯彻落实<国务院关于深化"互联网+先进制造业"发展工业互联网的指导意见> ...

最新文章

  1. centos使用yum快速安装java的方法
  2. java程序运行结果题_(Java程序设计)试题
  3. xcode7 打开工程错误 This Document requires xcode8.0 or later.
  4. OpenGL版本与硬件支持
  5. IE常见的CSS的BUG(一)
  6. 结构体作为STL map的key时需要注意什么? (某公司招聘面试试题)已跪~~~~(_)~~~~
  7. 反激式开关电源中PC817与TL431的配合电路探讨
  8. 中国煤炭工业节能减排现状研究分析及市场前景预测报告2022-2028年版
  9. 基于junit4的关于个人所得税计算的等价类与边界值_《边界值分析》-有这篇就够了...
  10. Quartz使用总结、Cron表达式
  11. drawforeground只有鼠标事件进入才刷新_罗技各系鼠标测评(2020年12月更新)
  12. 2019.03.25 bzoj4539: [Hnoi2016]树(主席树+倍增)
  13. 8.网页找不到服务器
  14. QML 获取当前时间戳 yyyy-MM-dd hh:mm:ss.zzz
  15. [转载] Python 快速入门实战教程
  16. 基于SSM的个人健康管理系统
  17. LeetCode初级算法笔记整理
  18. python dll注入监听_DLL注入和API拦截
  19. 各种深度摄像头的使用经验粗谈
  20. 压缩包修改所属目录Linux,linux文件/目录/压缩解压 操作指令

热门文章

  1. 默然说话20160101
  2. Discuz!论坛教程之修改admin.php防止直接恶意访问
  3. django连接sqlserver的一些坑
  4. JAVA制作弹出小广告的程序_微信小程序实现首页弹出广告
  5. liunx下调整CPU参数获得高性能
  6. 菜鸡教程(1):简易游戏每周推荐小程序制作
  7. 智解京东618——购物狂欢背后的安全暗战
  8. 【STM32】HAL库——定时器溢出中断
  9. ABAP调用启信宝HTTP restful API实例
  10. 孤立词语音识别(3)——计算MFCC系数