For a connected bufferevent(bev)
,
shutdown(fd, SHUT_WR);
BEV_EVENT_EOF
evbuffer_get_length(bufferevent_get_output(bev)) > 0
, then call bufferevent_free(bev)
What will do ? send all data and free or just give up data and free?
I will appreciate it if o help me.
libevent will give up data.
test as the blow:
shutdown(fd, SHUT_WR);
read
data until EOF
result:
send 10M
send: 10485760
read: 10485760
send 100M
send: 104857600
read: 29593600
so server will give up data
The following is test code
:
echo server: https://github.com/nmathewson/libevent-book/blob/master/examples_R8/R8_echo_server.c
echo client:
#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;
}