网络学习小记5

2024.12.18

1、使用writev函数实现服务器上的集中写

服务器端程序:

#include <arpa/inet.h>
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/uio.h>


#define BUFFER_SIZE 1024
//定义两种http状态码和状态信息
static const char *status_line[2] = {"200 OK", "500 Internal server error"};

int main(int argc, char *argv[]) {
  assert(argc == 4);
  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 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]; //用于保存http应答的状态行,头部字段和一个空行的缓冲区
    memset(header_buf, '\0', BUFFER_SIZE);
    char *file_buf;        //用于存放目标文件内容的应用程序缓存
    struct stat file_stat; //用于获取目标文件的属性
    bool valid = true;     //记录目标文件是否是有效文件
    int len = 0;           //缓存区header_buf目前已经使用多少空间

    if (stat(file_name, &file_stat) < 0) { //目标文件不存在  stat函数用来获取目标文件信息并保存在一个stat结构体中
      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 + 1) < 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-len-1, "%s","\r\n");
        //利用writev将header_buf和file_buf的内容一起写出
        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 = strlen(file_buf);
        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-len-1, "%s","\r\n");
        send(connfd,header_buf,strlen(header_buf),0);
    }
    close(connfd);
    delete[] file_buf;
  }

  close(sock);
  return 0;
}

a.txt中内容为abc123

客户端运行结果:

2、使用sendfile函数改进简化上述程序

sendfile效率更高,为零拷贝

服务器端程序:

#include <arpa/inet.h>
#include <assert.h>
#include <cstddef>
#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/uio.h>
#include <sys/sendfile.h>


#define BUFFER_SIZE 1024
//定义两种http状态码和状态信息
static const char *status_line[2] = {"200 OK", "500 Internal server error"};

int main(int argc, char *argv[]) {
  assert(argc == 4);
  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 fileid = open(file_name,O_RDONLY);
  assert(fileid>0);
  struct stat stat_buf;
  fstat(fileid,&stat_buf);//和stat区别在于第一个形参传入文件描述符

  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 {
    sendfile(connfd, fileid, NULL, stat_buf.st_size);//NULL表示从默认起始位置开始传输
    close(connfd);
  }

  close(sock);
  return 0;
}

其他同上。

3、splice函数和tee函数

splice函数用于在两个文件描述符之间移动数据,有一个参数必须是管道文件描述符。

tee函数用于在两个管道文件描述符之间复制数据,注意是复制,所以源文件描述符上的数据仍然可以用于后续的读操作。

利用tee函数和splice函数,实现了Linux 下tee程序(同时输出数据到终端和文件的程序,不要和tee函数混淆)的基本功能。

#include <assert.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>

int main(int argc, char *argv[])
{
	if (argc != 2)
	{
		printf("usage: %s <file>\n", argv[0]);
		return 1;
	}
	int filefd = open(argv[1], O_CREAT | O_WRONLY | O_TRUNC, 0666);
	assert(filefd > 0);

	int pipefd_stdout[2];
	int ret = pipe(pipefd_stdout);
	assert(ret != -1);

	int pipefd_file[2];
	ret = pipe(pipefd_file);
	assert(ret != -1);

	ret = splice(STDIN_FILENO, NULL, pipefd_stdout[1], NULL, 32768, SPLICE_F_MORE | SPLICE_F_MOVE);
	assert(ret != -1);
	ret = tee(pipefd_stdout[0], pipefd_file[1], 32768, SPLICE_F_NONBLOCK);
	assert(ret != -1);
	ret = splice(pipefd_file[0], NULL, filefd, NULL, 32768, SPLICE_F_MORE | SPLICE_F_MOVE);
	assert(ret != -1);
	ret = splice(pipefd_stdout[0], NULL, STDOUT_FILENO, NULL, 32768, SPLICE_F_MORE | SPLICE_F_MOVE);
	assert(ret != -1);

	close(filefd);
	close(pipefd_stdout[0]);
	close(pipefd_stdout[1]);
	close(pipefd_file[0]);
	close(pipefd_file[1]);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值