一:相关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:   
 
 

client代码:
   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:  } 

 
 
makefile文件:
   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:   

 
 
以上代码是一个简单的TCP测试代码
运行结果:
client_addr: 192.168.2.254
recv helloworld



libubox组件(1)——usock的更多相关文章

  1. libubox组件(3)——uloop

    一:uloop概述 uloop有三个功能: 文件描述符触发事件的监控,  timeout定时器处理, 当前进程的子进程的维护 二: uloop的整体框架 1: /** 2: * 初始化事件循环 3: ...

  2. openWrt libubox组件之uloop原理分析

    1.    libubox概述 libubox是openwrt新版本中的一个基础库,有很多应用是基于libubox开发的,如uhttpd,netifd,ubusd等. libubox主要提供以下两种功 ...

  3. 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 ...

  4. libubox

    lbubox是openwrt的一个核心库,封装了一系列基础实用功能,主要提供事件循环,二进制格式处理,linux链表实现和一些JSON辅助处理. 它的目的是以动态链接库方式来提供可重用的通用功能,给其 ...

  5. libubox-uloop

    参考:libubox组件(3)——uloop uloop是提供事件驱动机制接口,类似libevent事件框架,基于epoll接口来实现的. uloop三大功能:事件管理(uloop_fd).超时管理( ...

  6. ExtJS 4.2 评分组件

    上一文章是扩展ExtJS自带的Date组件.在这里将创建一个评分组件. 目录 1. 介绍 2. 示例 3. 资源下载 1. 介绍 代码参考的是 Sencha Touch 2上的一个RatingStar ...

  7. react组件的生命周期

    写在前面: 阅读了多遍文章之后,自己总结了一个.一遍加强记忆,和日后回顾. 一.实例化(初始化) var Button = React.createClass({ getInitialState: f ...

  8. react-router 组件式配置与对象式配置小区别

    1. react-router 对象式配置 和 组件式配置    组件式配置(Redirect) ----对应---- 对象式配置(onEnter钩子) IndexRedirect -----对应-- ...

  9. Angular2入门系列教程3-多个组件,主从关系

    上一篇 Angular2项目初体验-编写自己的第一个组件 好了,前面简单介绍了Angular2的基本开发,并且写了一个非常简单的组件,这篇文章我们将要学会编写多个组件并且有主从关系 现在,假设我们要做 ...

随机推荐

  1. Maven多模块项目单独编译子模块项目时报错:Failed to execute goal on project/Could not resolve dependencies for project

    背景:常规的父子项目搭建的工程,参考:http://www.cnblogs.com/EasonJim/p/6863987.html 解决方法: 1.需要把parent工程,也就是package是pom ...

  2. Wait statistics, or please tell me where it hurts

    https://www.sqlskills.com/blogs/paul/wait-statistics-or-please-tell-me-where-it-hurts/ By: Paul Rand ...

  3. zk client获取数据

    获取数据 它返回znode的关联数据和指定znode的元数据.你将获得信息,例如上次修改数据的时间,修改的位置以及数据的相关信息.此CLI还用于分配监视器以显示数据相关的通知. 语法 get /pat ...

  4. RUEI 13.1.1版本在OEL 5.7上的安装

    准备工作 ntp的工作和同步 /sbin/chkconfig --list | grep ntpd ntpd 0:off 1:off 2:off 3:off 4:off 5:off 6:off /sb ...

  5. python--如何操作表

    >>> import MySQLdb >>> conn=MySQLdb.connect(user='admin',passwd='',host='192.168.3 ...

  6. avro序列化详细操作

    Intellij 15.0.3 Maven avro 1.8.0 Avro是一个数据序列化系统. 它提供以下: 1 丰富的数据结构类型 2 快速可压缩的二进制数据形式 3 存储持久数据的文件容器 4 ...

  7. vuex介绍

    Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化.Vuex 也集成到 Vue 的官方调试工具 ...

  8. JQuery日记_5.14 Sizzle选择器(七)

    上篇说道,tokenize方法会把selector切割成一个个selector逻辑单元(如div>a是三个逻辑单元 'div','>','a')并为之片段赋予相应类型的过滤函数. for ...

  9. Python——网络编程,如何避免死锁?

    问题描述:什么是死锁? 死锁发生在当一个服务器和客户端同时试图往一个连接上写东西或同时从一个连接上读的时候.在这种情况下,没有进程可以得到任何数据(如果它们都正在读),因此,如果它们正在写,向外的bu ...

  10. MATLAB读取黑白图像显示却是黑色,24位深转8位深黑白图像解决方法

    1.24位深转8位深: ps将24位深原图.png保存为GIF图256即为8位,再将8位gif图转为需要的.png,即转为8位深png图. 2.MATLAB读取黑白图像显示几乎全为黑色: 这是最近处理 ...