1 pipe 函数
pipe创建一个管道,用户进程间的的通信。
#include<unistd.h>
int pipe(int fd[2]);
输入是 一个容量为2的数组
输出:
成功:0
失败:-1 errno
返回值一个代表写端一个代表读端,fd[1]写段,fd[0]读端。只可以单向通信,假如需要双向,需要创建两个管道。并且这两个文件描述符默认都是阻塞的,但也可以通过fctnl去改成非阻塞。在非阻塞的情况下,read时只有在引用计数为0时,才会返回0;write则是,引发SIGPIPE信号。
管道里面也是传输的字节流,跟tcp里面很相似,但是管道里面的容量是固定的,没有滑动窗口,但也可以通过fcntl修改。
1.2 socketpair
socketpair可以创建一个双向管道,既可以读也可以写。
返回值跟pipe是一样的。
#include<sys/types.h>
#include<sys/socket.h>
int socketpair(int domain,int type,int protocol,int fd[2]);
2 dup函数与dup2函数
dup函数可以实现输入输出的重定向。
#include<unistd.h>
int dup(int file_descriptor);
int dup2(int file_descriptor_one,int file_descriptor_two);
dup函数是形象的来说就是形参中的文件描述符指向的那一部分重新赋给了返回值。
dup2基本原理也是一样的,只不过它指定了一下具体是赋值给哪个文件描述符,而不是像在dup函数里面一样,只是在里面选取一个最小的文件描述符。
3 readv函数与writev函数
readv函数将数据从文件描述符读到分散的内存块中,writev函数则将多块分散的内存数据一并读入文件描述符。
#include<sys/uio.h>
ssize_t readv(int fd,const struct iovec* vector,int couont);
ssize_t writev(int fd,const struct iovec* vector,int count);
iovec是一个结构数组
用途:可以将多块同时写在一块内存中
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <assert.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#define BUFFER_SIZE 1024
static const char* status_line[2] = { "200 OK", "500 Internal server error" };
int main( int argc, char* argv[] )
{
if( argc <= 3 )
{
printf( "usage: %s ip_address port_number filename\n", basename( argv[0] ) );
return 1;
}
const char* ip = argv[1];
int port = atoi( argv[2] );
const char* file_name = argv[3];
struct sockaddr_in address;
bzero( &address, sizeof( address ) );
address.sin_family = AF_INET;
inet_pton( AF_INET, ip, &address.sin_addr );
address.sin_port = htons( port );
int sock = socket( PF_INET, SOCK_STREAM, 0 );
assert( sock >= 0 );
int ret = bind( sock, ( struct sockaddr* )&address, sizeof( address ) );
assert( ret != -1 );
ret = listen( sock, 5 );
assert( ret != -1 );
struct sockaddr_in client;
socklen_t client_addrlength = sizeof( client );
int connfd = accept( sock, ( struct sockaddr* )&client, &client_addrlength );
if ( connfd < 0 )
{
printf( "errno is: %d\n", errno );
}
else
{
char header_buf[ BUFFER_SIZE ];
memset( header_buf, '\0', BUFFER_SIZE );
char* file_buf;
struct stat file_stat;
bool valid = true;
int len = 0;
if( stat( file_name, &file_stat ) < 0 )
{
valid = false;
}
else
{
if( S_ISDIR( file_stat.st_mode ) )
{
valid = false;
}
else if( file_stat.st_mode & S_IROTH )
{
int fd = open( file_name, O_RDONLY );
file_buf = new char [ file_stat.st_size + 1 ];
memset( file_buf, '\0', file_stat.st_size + 1 );
if ( read( fd, file_buf, file_stat.st_size ) < 0 )
{
valid = false;
}
}
else
{
valid = false;
}
}
if( valid )
{
ret = snprintf( header_buf, BUFFER_SIZE-1, "%s %s\r\n", "HTTP/1.1", status_line[0] );
len += ret;
ret = snprintf( header_buf + len, BUFFER_SIZE-1-len,
"Content-Length: %d\r\n", file_stat.st_size );
len += ret;
ret = snprintf( header_buf + len, BUFFER_SIZE-1-len, "%s", "\r\n" );
struct iovec iv[2];
iv[ 0 ].iov_base = header_buf;
iv[ 0 ].iov_len = strlen( header_buf );
iv[ 1 ].iov_base = file_buf;
iv[ 1 ].iov_len = file_stat.st_size;
ret = writev( connfd, iv, 2 );
}
else
{
ret = snprintf( header_buf, BUFFER_SIZE-1, "%s %s\r\n", "HTTP/1.1", status_line[1] );
len += ret;
ret = snprintf( header_buf + len, BUFFER_SIZE-1-len, "%s", "\r\n" );
send( connfd, header_buf, strlen( header_buf ), 0 );
}
close( connfd );
delete [] file_buf;
}
close( sock );
return 0;
}
4 sendfile函数
sendfile函数在两个文件描述符之间直接传递数据(完全在内核中操作),从而避免了内核缓冲区和用户缓存区之间的数据拷贝,效率很高,称之为零拷贝。
#include<sys/sendfile.h>
ssize_sendifle(int out_fd,int in_fd,off_t* offset,size_t count);
输入值:out_fd :待写入的文件描述符
in_fd:待读入内容的文件描述符
offset:读文件流的偏移位置
count:读取的字节数
返回值:成功读取的字节数
in_fd必须是指向真实的文件,不能是socket或者管道,而out_fd则必须是一个socket,所以说sendfile就是专门用作与网络传输。
int filefd = open( file_name, O_RDONLY );
int connfd = accept( sock, ( struct sockaddr* )&client, &client_addrlength );
sendfile( connfd, filefd, NULL, stat_buf.st_size );
5 mmap函数和munmap
mmap申请一段内存,用作于将磁盘上的文件映射在申请好的一段内存里面去。
munmap则用于释放申请好的内存
#include<sys/mman.h>
void* mmap(void* start,size_t length,int prot,int flags,int fd,off_t offset);
int munmap(void* start,size_t length);
6 splice 函数
splice函数用于两个文件描述符之间的移动数据,也就是零拷贝操作。
#include<fcntl.h>
ssize_t splice(int fd_in,loff_t* off_in,int fd_out,loff_t* off_out,size_t len,unsigned int flags);
int pipefd[2];
assert( ret != -1 );
ret = pipe( pipefd );
ret = splice( connfd, NULL, pipefd[1], NULL, 32768, SPLICE_F_MORE | SPLICE_F_MOVE );
assert( ret != -1 );
ret = splice( pipefd[0], NULL, connfd, NULL, 32768, SPLICE_F_MORE | SPLICE_F_MOVE );
assert( ret != -1 );
close( connfd );