服务端:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/epoll.h>
#define PORT 8080
#define MAX_EVENTS 10
#define BUFFER_SIZE 1024
// 设置非阻塞套接字
int set_non_blocking(int sockfd) {
int flags = fcntl(sockfd, F_GETFL, 0);
if (flags == -1) {
perror("fcntl get");
return -1;
}
if (fcntl(sockfd, F_SETFL, flags | O_NONBLOCK) == -1) {
perror("fcntl set");
return -1;
}
return 0;
}
int main() {
int listen_sock, conn_sock, epoll_fd;
struct sockaddr_in server_addr, client_addr;
socklen_t addrlen = sizeof(client_addr);
struct epoll_event ev, events[MAX_EVENTS];
char buffer[BUFFER_SIZE];
// 创建监听套接字
if ((listen_sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");

最低0.47元/天 解锁文章
556

被折叠的 条评论
为什么被折叠?



