当前位置:网站首页>Embedded development notes, practical knowledge sharing
Embedded development notes, practical knowledge sharing
2022-07-26 05:16:00 【an520_】
Previously, in order to facilitate the testing department to save some test data , Need to temporarily develop a simple nanomsg server, With the board end client Communicate in the LAN , receive client Data and save .
As server End , You need to bind the computer IP, Different computers IP It's different , So it needs to be modified when using different computers IP To use this normally server Program .
Without recompiling the program , There are two ways to meet this need :
hold IP Write it in the configuration file , such as ini In the configuration file in format , then server The program reads IP, Then bind .
server The program automatically obtains IP Address and bind .
Use these two methods respectively :
Read from configuration file IP Address
There are many formats of configuration files , Such as JSON、INI etc. . What I choose here is INI Format of the configuration file ,.ini File is Initialization File Abbreviation , Initialization file .INI Document by section 、 key 、 The value of , Annotations are indicated by semicolons (;). for example :

Here we use inih Parser right INI File parsing .
inih: One C language-written INI File parser .
inih The address of the parser :
GitHub - benhoyt/inih: Simple .INI file parser in C, good for embedded systems

meanwhile ,inih Parsers have also been included in a hodgepodge of resource summary Posts :
EmbedSummary: Embedded hodgepodge resource summary
inih It's easy to use , Let's take a look at .
Downloaded inih The contents are shown in the figure :

hold ini.c And ini.h Just put it under our project . Here we use a test project :

meanwhile , Create a new one ip.ini The documents are stored in the project directory .ip.ini The contents of the document are as follows :

Let's write the code test.c To parse this file :

The analytical method is very simple ( May refer to inih/examples Under the demo, We also imitate this here demo To analyze ):
Construct a configuration structure
configuration, Define a configuration The structure is used to store the data we parse , The members in the structure are what we need to resolve INI The keys in the file . for example , our ip.ini There is... In the document ip_addr、name、num These three keys , The members in the structure represent these three keys .Define a handler Callback function , Used for processing parsing process . The parsing process is also very simple , matching Section Name And Key Name, Then take out the value .
call
ini_parseFunction pair INI File parsing .
among ,handler A... Is called in the function strdup() The function and atoi() function .
strdup() The function is c A string copying function commonly used in languages , In general, and free() Functions appear in pairs ,, because strdup() Called inside malloc() Function allocates memory for variables .
atoi() function (ascii to integer) Is a function that converts a string to an integer .
compile 、 function :

Automatic access to IP Address
We can use getifaddrs() Function to get .getifaddrs() Function is used to obtain network card information , Include IP、 Mask 、 Broadcast address and other information .
getifaddrs() The function prototype :

__ifap To get the network card information .
use getifaddrs() Function to get IP The format is The numerical format (numeric), It needs to be converted into Expression format (presentation).inet_ntop() Function can meet this requirement .
inet_ntop() Function is random IPv6 Functions that appear , about IPv4 Address and IPv6 All addresses apply , Function p and n Represent the Expression format (presentation) and The numerical format (numeric) .IP The expression format of the address is ASCII character string , The numeric format is a binary value stored in the socket address structure . What we need here is ASCII In string form IP Address .
inet_ntop() The function prototype :

family Parameters : It can be either AF_INET(ipv4) It can also be AF_INET6(ipv6).
addrptr Parameters :addrptr Pointer to numeric format IP.
strptr Parameters :strptr Point to the expression format IP, The caller must allocate memory for the target storage unit and specify its size .
len Parameters : Accommodate the length of the expression format .
Return value : If successful, it is a pointer to the expression format , In case of error NULL.
Let's take a look at the example code :
compile 、 function :


That's what we're sharing ~
【 Learning exchange group 769843038】
【 Share some learning tutorials below 】:
Internet of things embedded engineer network foundation
Embedded development - Interprocess communication details
Necessary for embedded engineers Web knowledge
Embedded application lightweight database .
Embedded foundation tamping series C Language function elaboration .
Analysis on the development and current situation of embedded industry
【 Network disk free information package , If you need it, you can collect it yourself 】:

边栏推荐
- ABAP语法学习(ALV)
- C语言实现发牌功能基本方法
- 元宇宙为服装设计展示提供数字化社交平台
- Ansible中常用的模块
- Shell read read console input, use of read
- C语言函数
- MySQL基础学习
- When AQS wakes up the thread, I understand why it traverses from the back to the front
- Okaleido上线聚变Mining模式,OKA通证当下产出的唯一方式
- MySQL eight knowledge points: from getting started to deleting the database
猜你喜欢
随机推荐
DOM事件流 事件冒泡-事件捕获-事件委托
CMD operation command
C语言-指针进阶
ALV入门
Reason for pilot importerror: cannot import name 'pilot_ Version 'from' PIL ', how to install pilot < 7.0.0
I talked with the interviewer about MySQL optimization in five dimensions
35. 搜索插入位置
基于通用优化软件GAMS的数学建模和优化分析
SAP report development steps
Application of remote sensing, GIS and GPS technology in hydrology, meteorology, disasters, ecology, environment and health
Test of countlaunch demo
注解@Autowired如何自动装配
[weekly translation go] how to write your first program with go
未来大气污染变化模拟
Shell的read 读取控制台输入、read的使用
Annotation @autowired how to assemble automatically
使用Ansible中的playbook
嵌入式分享合集20
[acwing] 1268. Simple questions
Seata submits at details in two stages
http://makerschool.mikecrm.com/f4wjYBB








