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的基本开发,并且写了一个非常简单的组件,这篇文章我们将要学会编写多个组件并且有主从关系 现在,假设我们要做 ...
随机推荐
- Vim复制一整行和复制多行
1.复制 1)单行复制 在命令模式下,将光标移动到将要复制的行处,按“yy”进行复制: 2)多行复制 在命令模式下,将光标移动到将要复制的首行处,按“nyy”复制n行:其中n为1.2.3…… 2.粘贴 ...
- Maven设置snapshot无法在远程仓库下载的问题解决
检查步骤如下: 1.检查nexus是否纳入public版本中: 2.配置中是否启用snapshots功能.以下方法两种设置都可以,任选一个即可. 一种是在项目pom.xml使用: <reposi ...
- delphi设计浮动窗口
delphi设计浮动窗口 用过Photoshop的朋友一定对它的那些方便的浮动面板记忆犹新,其实这些面板就是一个个的小窗体,但这些小窗体都放在Photoshop的主窗体上 (不是存在主窗体中),有自己 ...
- MySql中的concat()相关函数
concat 函数的基本应用一: SQL CONCAT函数用于将两个字符串连接起来,形成一个单一的字符串.试试下面的例子: SQL> SELECT CONCAT('FIRST ', 'SECON ...
- delphi 接口Interface
学习 delphi 接口 一切都是纸老虎!!! 第四章 接口 前不久,有位搞软件的朋友给我出了个谜语.谜面是“相亲”,让我猜一软件术语.我大约想了一分钟,猜 出谜底是“面向对象”.我 ...
- http://blog.sina.com.cn/s/blog_62e1faba010147k4.html
http://blog.sina.com.cn/s/blog_62e1faba010147k4.html
- Python连接MySQL乱码(中文变问号)
#coding=utf-8 import MySQLdb db = MySQLdb.connect("IP","用户名","密码",&quo ...
- css - border-radius
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 解决C#使用Microsoft.Office.Interop.Excel操作Excel后进程一直存在的问题
This resolved the issue for me. Your code becomes: public Excel.Application excelApp = new Excel.App ...
- mysqli 预处理语句
预处理语句用于执行多个相同的 SQL 语句,并且执行效率更高. <?php // 设置编码格式 header('content-type:text/html;charset=utf-8'); / ...