其中fd为一个文件描述符,vector为一个数组,数组中每个元素都包含了一个地址和一个偏移量,count表示数组的个数;
以下代码:
服务端利用writev()
从一个stadus_line
数组和另外一个本地文件读取文件,并发送到客户端;
#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>
#include<sys/uio.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;
address.sin_port=htons(port);
inet_pton(AF_INET,ip,&address.sin_addr);
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 length=sizeof(client);
int confd=accept(sock,(struct sockaddr*)&client,&length);
if(confd<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 ) //将文件内容保存到file_buf中;
{
valid = false;
}
}
else
{
valid = false;
}
}
if(valid)
{
ret=snprintf(header_buf,BUFFER_SIZE-1,"%s %s\r\n","http1.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(confd,iv,2);
}
else
{
ret=snprintf(header_buf,BUFFER_SIZE-1,"%s %s\r\n","http1.1:",status_line[0]);
len+=ret;
ret=snprintf(header_buf+len,BUFFER_SIZE-1-len,"%s","\r\n");
send(confd,header_buf,strlen(header_buf),0);
}
close (confd);
delete []file_buf;
}
close(sock);
return 0;
}
客户端的输出:
高性能服务器编程