当前位置:网站首页>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 】:

边栏推荐
- 未来大气污染变化模拟
- Why is the value represented by a negative number greater than an integer by 1?
- “双碳”目标下资源环境中的可计算一般均衡(CGE)模型实践技术
- 测试用例评审如何开展
- ALV program collection
- Mathematical modeling and optimization analysis based on general optimization software gams
- 推荐必读:测试人员如何快速熟悉新业务?
- 提高shuffle操作中的reduce并行度
- CMD operation command
- JVM Lecture 2: class loading mechanism
猜你喜欢

ALV program collection

Okaleido上线聚变Mining模式,OKA通证当下产出的唯一方式

Embedded sharing collection 20

Recommend 12 academic websites for free literature search, and suggest to like and collect!
C语言详解系列——函数的认识(4)函数的声明与定义,简单练习题

DOM事件流 事件冒泡-事件捕获-事件委托

Textfield and password input box that are more flexible and easy to use in compose

mysql函数汇总之日期和时间函数

测试必备工具之Fiddler,你真的了解吗?

security权限管理详解
随机推荐
Bash shortcut key to improve command line efficiency [Full Version]
面试之请详细说下synchronized的实现原理以及相关的锁
Please elaborate on the implementation principle of synchronized and related locks
C语言实现发牌功能基本方法
嵌入式开发小记,实用小知识分享
Getting started with ALV
CMD操作命令
嵌入式分享合集21
第二讲 初识SLAM
35. Search the insertion position
OD-Paper【1】:Rich feature hierarchies for accurate object detection and semantic segmentation
OD-Paper【2】:Fast R-CNN
Embedded sharing collection 21
攻防世界-FlatScience
[Luogu] p3919 [template] persistent segment tree 1 (persistent array)
Redis过期删除策略和内存淘汰策略
Practical technology of SWAT Model in simulation of hydrology, water resources and non-point source pollution
Why is the value represented by a negative number greater than an integer by 1?
推荐必读:测试人员如何快速熟悉新业务?
Real scientific weight loss
http://makerschool.mikecrm.com/f4wjYBB