#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/epoll.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
static const char* request = "GET http://localhost/index.html HTTP/1.1\r\nConnection: keep-alive\r\n\r\nxxxxxxxxxxxx";
int setnonblocking( int fd )
{
int old_option = fcntl( fd, F_GETFL );
int new_option = old_option | O_NONBLOCK;
fcntl( fd, F_SETFL, new_option );
return old_option;
}
void addfd( int epoll_fd, int fd )
{
epoll_event event;
event.data.fd = fd;
event.events = EPOLLOUT | EPOLLET | EPOLLERR;
epoll_ctl( epoll_fd, EPOLL_CTL_ADD, fd, &event );
setnonblocking( fd );
}
// 向服务器写len个字节数据
bool write_nbytes( int sockfd, const char* buffer, int len )
{
int bytes_write = <
服务器压力测试客户端
最新推荐文章于 2024-04-27 16:55:13 发布
本文介绍了一种使用epoll库在Linux下创建高性能客户端,通过非阻塞I/O和epoll事件驱动模型,实现对多个服务器的TCP连接并发操作,包括发送GET请求和接收响应。重点展示了如何利用epoll进行事件监听和管理,适用于网络编程和高并发场景。
摘要由CSDN通过智能技术生成