Richard Stevens 《UNIX® Network Programming Volume 1, Third Edition: The Sockets Networking》 Chapter 6.2 "I/O Models"
We first examine the basic differences in the five I/O models that are available to us under Unix:
blocking I/O
nonblocking I/O
I/O multiplexing (
select
andpoll
)signal driven I/O (
SIGIO
)asynchronous I/O (the POSIX
aio_
functions)There are normally two distinct phases for an input operation:
- Waiting for the data to be ready. This involves waiting for data to arrive on the network. When the packet arrives, it is copied into a buffer within the kernel.
- Copying the data from the kernel to the process. This means copying the (ready) data from the kernel's buffer into our application buffer
基于 TCP 的网络编程开发分为服务器端和客户端两部分,常见的核心步骤和流程如下:
Linux中将 socket 设置为非阻塞模式有三种方法:
(1)创建socket的时候,指定socket是异步的,在type的参数中设置SOCK_NONBLOCK
标志即可。
int socket(int domain, int type, int protocol);
int s = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, IPPROTO_TCP);
(2)使用fcntl
函数:
int flags = fcntl(sfd, F_GETFL, 0);
fcntl(sockfd, F_SETFL, flags | O_NONBLOCK);
(3)使用ioctl
函数:
ioctl(sockfd, FIONBIO, 1); //1:非阻塞 0:阻塞
POSIX AIO及libaio的区别
libaio是原生的 linux aio,行为更为低级;POSXI AIO是在用户空间模拟异步IO的功能,不需要内核的支持。
aio_*系列的调用是glibc提供的,是glibc用线程+阻塞调用来模拟的,性能很差。
- 使用CMake编译
$ mkdir build
$ cd build
$ cmake ..
$ make
- 使用Android NDK编译
需要把ndk-build命令放入系统全局变量中
$ cd jni
$ ndk-build