libubox组件(1)——usock
一:相关API介绍
1.相关源码文件:usocket.h usocket.c
2.类型标志
1: #define USOCK_TCP 0
2: #define USOCK_UDP 1
3: #define USOCK_SERVER 0x0100
4: #define USOCK_NOCLOEXEC 0x0200
5: #define USOCK_NONBLOCK 0x0400
6: #define USOCK_NUMERIC 0x0800
7: #define USOCK_IPV6ONLY 0x2000
8: #define USOCK_IPV4ONLY 0x4000
9: #define USOCK_UNIX 0x8000
3.接口函数
/**
* 创建一个新的网络sock
*
* @param type - 类型标志
* @param host - 作为server表示绑定本地地址;作为client表示需连接的地址
* @param service - 端口
* @return - sock fd > 0; 错误 < 0
*/
int usock(int type, const char *host, const char *service);
二:实例
server代码
1: #include <stdio.h>
2: #include <stdlib.h>
3: #include <string.h>
4: #include <sys/types.h> /* See NOTES */
5: #include <sys/socket.h>
6: #include <netinet/in.h>
7: #include <arpa/inet.h>
8: #include <libubox/usock.h>
9:
10: int main()
11: {
12: struct sockaddr_in cli_addr;
13: socklen_t len = sizeof(struct sockaddr);
14: int type = USOCK_TCP | USOCK_SERVER | USOCK_NOCLOEXEC | USOCK_IPV4ONLY;
15: const char *host = "CarRadio";
16: const char *service = "8000";
17: char recv_buf[1024] = {0};
18: int connect_fd, u_fd = usock(type, host, service);
19: if (u_fd < 0) {
20: perror("usock");
21: return -1;
22: }
23: while (1) {
24: connect_fd = accept(u_fd, (struct sockaddr *)(&cli_addr), &len);
25: if (connect_fd < 0) {
26: perror("accept");
27: return -1;
28: }
29: printf("client_addr: %s\n", inet_ntoa(cli_addr.sin_addr));
30: recv(connect_fd, recv_buf, 1024, 0);
31: printf("recv %s\n", recv_buf);
32: close(connect_fd);
33: }
34:
35: return 0;
36: }
37:
1: #include <stdio.h>
2: #include <stdlib.h>
3: #include <string.h>
4: #include <sys/types.h> /* See NOTES */
5: #include <sys/socket.h>
6: #include <libubox/usock.h>
7:
8: int main()
9: {
10: struct sockaddr cli_addr;
11: socklen_t len = sizeof(struct sockaddr);
12: int type = USOCK_TCP | USOCK_NOCLOEXEC | USOCK_IPV4ONLY;
13: const char *host = "CarRadio";
14: const char *service = "8000";
15: char recv_buf[1024] = {0};
16: int c_fd = usock(type, host, service); /* create a linker socket*/
17: if (c_fd < 0) {
18: perror("usock");
19: return -1;
20: }
21: send(c_fd, "helloworld", 10, 0);
22: sleep(10);
23: close(c_fd);
24: return 0;
25: }
1: include $(TOPDIR)/rules.mk
2:
3: PKG_NAME:=usocket
4: PKG_RELEASE:=1.0.0
5:
6: PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)
7:
8: include $(INCLUDE_DIR)/package.mk
9:
10: define Package/$(PKG_NAME)
11: CATEGORY:=Utilities
12: SUBMENU:=Demo
13: DEPENDS:=+libubox
14: TITLE:= usocket test project.
15: endef
16:
17: define Package/$(PKG_NAME)/description
18: If you can't figure out what this program does, you're probably
19: brain-dead and need immediate medical attention.
20: endef
21:
22:
23: define Build/Prepare
24: mkdir -p $(PKG_BUILD_DIR)
25: $(CP) ./src/* $(PKG_BUILD_DIR)/
26: endef
27:
28: define Package/$(PKG_NAME)/install
29: $(INSTALL_DIR) $(1)/bin
30: $(INSTALL_BIN) $(PKG_BUILD_DIR)/userver $(1)/bin/
31: $(INSTALL_BIN) $(PKG_BUILD_DIR)/uclient $(1)/bin/
32: endef
33:
34: $(eval $(call BuildPackage,$(PKG_NAME)))
35:
36:
libubox组件(1)——usock的更多相关文章
- libubox组件(3)——uloop
一:uloop概述 uloop有三个功能: 文件描述符触发事件的监控, timeout定时器处理, 当前进程的子进程的维护 二: uloop的整体框架 1: /** 2: * 初始化事件循环 3: ...
- openWrt libubox组件之uloop原理分析
1. libubox概述 libubox是openwrt新版本中的一个基础库,有很多应用是基于libubox开发的,如uhttpd,netifd,ubusd等. libubox主要提供以下两种功 ...
- libubox组件(2)——blob/blobmsg (转载 https://segmentfault.com/a/1190000002391970)
一:blob相关接口 1.数据结构 1: struct blob_attr { 2: uint32_t id_len; /** 高1位为extend标志,高7位存储id, 3: * 低24位存储dat ...
- libubox
lbubox是openwrt的一个核心库,封装了一系列基础实用功能,主要提供事件循环,二进制格式处理,linux链表实现和一些JSON辅助处理. 它的目的是以动态链接库方式来提供可重用的通用功能,给其 ...
- libubox-uloop
参考:libubox组件(3)——uloop uloop是提供事件驱动机制接口,类似libevent事件框架,基于epoll接口来实现的. uloop三大功能:事件管理(uloop_fd).超时管理( ...
- ExtJS 4.2 评分组件
上一文章是扩展ExtJS自带的Date组件.在这里将创建一个评分组件. 目录 1. 介绍 2. 示例 3. 资源下载 1. 介绍 代码参考的是 Sencha Touch 2上的一个RatingStar ...
- react组件的生命周期
写在前面: 阅读了多遍文章之后,自己总结了一个.一遍加强记忆,和日后回顾. 一.实例化(初始化) var Button = React.createClass({ getInitialState: f ...
- react-router 组件式配置与对象式配置小区别
1. react-router 对象式配置 和 组件式配置 组件式配置(Redirect) ----对应---- 对象式配置(onEnter钩子) IndexRedirect -----对应-- ...
- Angular2入门系列教程3-多个组件,主从关系
上一篇 Angular2项目初体验-编写自己的第一个组件 好了,前面简单介绍了Angular2的基本开发,并且写了一个非常简单的组件,这篇文章我们将要学会编写多个组件并且有主从关系 现在,假设我们要做 ...
随机推荐
- Google开源C++单元测试框架Google Test
1.玩转Google开源C++单元测试框架Google Test系列(gtest)之一 - 初识gtest 2.玩转Google开源C++单元测试框架Google Test系列(gtest)之二 - ...
- weblogic下同域不同端口下的跨域问题解决
环境:同一台服务器,同一个Weblogic应用程序,分别建两个域,两个域IP一样,端口不同.一个域里放Web应用A,一个放Web应用B. 操作:用户访问A程序的时候,A程序会返回一个链接,让用户去 ...
- WebLogic Server 12.1.2后的字符型安装模式
weblogic Server 12.1.1全部都可以用原来方式. WebLogic Server 12.1.2后已经取消了console安装模式,目前只有gui和静默安装模式.并且安装方式下也有很大 ...
- 'dict_values' object does not support indexing, Python字典dict中由value查key
Python字典dict中由value查key 众所周知,字典dict最大的好处就是查找或插入的速度极快,并且不想列表list一样,随着key的增加越来越复杂.但是dict需要占用较大的内存空间,换句 ...
- react使用引入svg的icon;svg图形制作
由于手头的icon有限,需要使用更多的图标,就得找外援: 1.react安装icon插件,使用插件里已经有的图标 https://react-icons.netlify.com/#/ React Ic ...
- adb logcat通过包名过滤(dos命令find后跟变量)
adb命令中似乎没有直接通过报名来过滤的功能,可是能够通过过滤进程的pid来过滤该应用的日志 过滤条件:该app在执行 实现原理: 1.获取该app执行时的pid 2.通过find命令,过滤pid的日 ...
- 使用x64dbg分析微信聊天函数并实现发信息
1.引言 我们知道微信现在不光在手机上很常用,在电脑也是非常常用的,尤其是使用微信联系客户和维护群的人,那这个时候每天都会定期发送一些信息,如果人工操作会很累,所以自动化工具是王道,本节就使用x64d ...
- jquery怎样获得父级窗体的大小
方法例如以下: $(window.parent.window).width() 注意: window能够省略.如:$(parent).width(),parent能够有多级,比方:$(parent.p ...
- 倍福TwinCAT(贝福Beckhoff)应用教程12.2 TwinCAT控制松下伺服 NC初步
在前面我们已经学会了使用贝福自带的调试软件完成试运行,接下来是使用TWINCAT PLC实现这个功能,右击PLC添加一个PLC项目 在VISUs上右击添加一个HMI人机界面 目前PLC程序和人 ...
- iOS怎样找到自己的沙盒
iOS怎样找到自己的沙盒 在ios开发我们会用到沙盒,因为自己对沙盒理解的不够,所以找不到沙盒文件在哪里,当然要知道路径了 比如我的路径 NSString* cachepath = [NSHomeDi ...