gpt4 book ai didi

c - 如果输出缓冲区有数据,调用 bufferevent_free 后会做什么?

转载 作者:行者123 更新时间:2023-11-30 16:24:25 40 4
gpt4 key购买 nike

对于已连接的 bufferevent(bev)

  1. 客户端调用shutdown(fd, SHUT_WR);
  2. 服务器接收BEV_EVENT_EOF
  3. 如果 evbuffer_get_length(bufferevent_get_output(bev)) > 0,则调用 bufferevent_free(bev)

会做什么?发送所有数据并且免费,或者只是放弃数据并且免费?

如果您能帮助我,我将不胜感激。

最佳答案

libevent 将放弃数据。

测试如下:

  1. 发送 10 M 或 100 M 数据
  2. 调用shutdown(fd, SHUT_WR);
  3. 读取数据直到EOF

结果:

send 10M 
send: 10485760
read: 10485760

send 100M
send: 104857600
read: 29593600

因此服务器将放弃数据

以下是测试代码:

回显服务器:https://github.com/nmathewson/libevent-book/blob/master/examples_R8/R8_echo_server.c

回显客户端:

#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/socket.h>
#include <unistd.h>

void test(int n) {
const char* server_ip = "127.0.0.1";
uint16_t server_port = 9876;

struct sockaddr_in server_addr;
bzero(&server_addr, sizeof server_addr);
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(server_port);
inet_pton(AF_INET, server_ip, &server_addr.sin_addr);

int client_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
connect(client_socket, (struct sockaddr*)(&server_addr), (socklen_t)(sizeof server_addr));

char buf[1024];
int send_count = 0;
int read_count = 0;
for (int i = 0; i != n; ++i)
for (int j = 0; j != 1024; ++j) {
ssize_t ret = write(client_socket, buf, sizeof(buf));
if (ret == -1) {
perror("error");
exit(-1);
}
send_count += ret;
}

shutdown(client_socket, SHUT_WR);

for (;;) {
ssize_t ret = read(client_socket, buf, sizeof(buf) - 1);
if (ret == -1) {
perror("error");
exit(-1);
}
if (ret == 0)
break;
read_count += ret;
}
printf("send: %d\n", send_count);
printf("read: %d\n", read_count);
}

int main() {
printf("send 10M \n");
test(10);
printf("\nsend 100M \n");
test(100);

return 0;
}

关于c - 如果输出缓冲区有数据,调用 bufferevent_free 后会做什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53673160/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com