程序设计
- HTTP响应报文通常包含1个状态行、多个头部字段、1个空行和文档。其中,前3部分的内容可能被Web服务器放置在一个内存中,而文档的内容则通常被读入到另一块单独的内存中(通过read或者mmap函数),我们并不需要将这部分的内容拼接到一起在发送,而可以使用writev函数将它们同时写出,代码如下
//ser_writrv.c
#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 <libgen.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;
int valid = 1;
int len = 0;
if( stat( file_name, &file_stat ) < 0 )
{
valid = 0;
}
else
{
if( S_ISDIR( file_stat.st_mode ) )
{
valid = 0;
}
else if( file_stat.st_mode & S_IROTH )
{
int fd = open( file_name, O_RDONLY );
file_buf = (char*)malloc(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 = 0;
}
}
else
{
valid = 0;
}
}
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 );
free(file_buf);
}
close( sock );
return 0;
}
运行程序
- 在我们的服务端根目录有一个Hello.txt文件,文件中有一串字符串“HelloWorld”。我们运行程序,并将Hello.txt作为参数传入

- 运行telent程序接连到我们的程序,就可以收到我们程序发送过来的响应报文(报文中含有Hello.txt文件的内容)
