One: Related API introduction
1. Related source files: usocket.h usocket.c
2. Type Mark
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. Interface functions
/** * Create a new network sock * @param type-Flag * @param host-binds the address as server, and as client represents the address to be connected * @param ser Vice-Port * @return-sock fd > 0; error < 0 * /
int usock (intconstchar,const char *service);
Two: examples
Server code
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:
Ten: int main ()
One : {
: struct sockaddr_in cli_addr;
: sizeof(struct sockaddr);
+ : int type = usock_tcp | Usock_server | usock_nocloexec | Usock_ipv4only;
: constchar"Carradio";
: constchar"8000";
: char recv_buf[1024] = {0};
: int connect_fd, u_fd = Usock (type, host, service);
: if (u_fd < 0) {
: perror ("Usock");
+ : return -1;
: }
: while (1) {
: connect_fd = Accept (u_fd, (struct sockaddr *) (&CLI_ADDR), &len);
: if (connect_fd < 0) {
: perror ("accept");
+ : return -1;
: }
: printf ("client_addr:%s\n", Inet_ntoa (CLI_ADDR.SIN_ADDR));
: recv (CONNECT_FD, RECV_BUF, 1024, 0);
: printf ("recv%s\n", recv_buf);
: Close (CONNECT_FD);
: }
34:
: return 0;
: }
Panax Notoginseng :
Client code:
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:
7:
8: int main ()
9: {
: struct sockaddr cli_addr;
One : sizeof(struct sockaddr);
: int type = usock_tcp | usock_nocloexec | Usock_ipv4only;
: constchar"Carradio";
: constchar"8000";
: char recv_buf[1024] = {0};
: int c_fd = Usock (type, host, service); / * Create a linker socket*/
: if (c_fd < 0) {
: perror ("Usock");
: return -1;
: }
: "HelloWorld", 10, 0);
: Sleep (10);
: Close (C_FD);
: return 0;
25:
Makefile File:
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:
Ten: define package/$ (Pkg_name)
One : category:=utilities
: Submenu:=demo
£ depends:=+libubox
: title:= usocket test Project.
: endef
:
: define package/$ (Pkg_name)/description
: If You can ' t figure out what the This program does, you 're probably
: Brain-dead and need immediate medical attention.
: endef
£ º
:
At : define Build/prepare
: mkdir-p $ (pkg_build_dir)
: $ (CP)./src/* $ (pkg_build_dir)/
Endef :
:
: define package/$ (Pkg_name)/install
: $ (install_dir) $ (1)/bin
: $ (install_bin) $ (pkg_build_dir)/userver $ (1)/bin/
: $ (install_bin) $ (pkg_build_dir)/uclient $ (1)/bin/
: endef
:
: $ (eval $ (call buildpackage,$ (Pkg_name)))
:
£ º
The above code is a simple TCP test code run result: Client_addr:192.168.2.254recv HelloWorld
Libubox components (1)--usock