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:   
 
 
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
posted @ 2016-06-24 10:53  夕相待  阅读(4854)  评论(0编辑  收藏  举报