本文主要是介绍libevent 多线程调用event_base,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
/***********************************************************************************************************
,编译程序 g++ -Wall -g event-server.c -o server -levent -lpthread
*************************************************************************************************************/
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <string.h>
#include <errno.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <errno.h>
#include "event.h"
#include <stdlib.h>
#include <pthread.h>
#include <fcntl.h>#define ERR_EXIT(m) \do\{ \perror(m); \exit(EXIT_FAILURE); \} while(0)\void send_fd(int sock_fd, int send_fd)
{int ret;struct msghdr msg;struct cmsghdr *p_cmsg;struct iovec vec;char cmsgbuf[CMSG_SPACE(sizeof(send_fd))];int *p_fds;char sendchar = 0;msg.msg_control = cmsgbuf;msg.msg_controllen = sizeof(cmsgbuf);p_cmsg = CMSG_FIRSTHDR(&msg);p_cmsg->cmsg_level = SOL_SOCKET;p_cmsg->cmsg_type = SCM_RIGHTS;p_cmsg->cmsg_len = CMSG_LEN(sizeof(send_fd));p_fds = (int *)CMSG_DATA(p_cmsg);*p_fds = send_fd; // 通过传递辅助数据的方式传递文件描述符msg.msg_name = NULL;msg.msg_namelen = 0;msg.msg_iov = &vec;msg.msg_iovlen = 1; //主要目的不是传递数据,故只传1个字符msg.msg_flags = 0;vec.iov_base = &sendchar;vec.iov_len = sizeof(sendchar);ret = sendmsg(sock_fd, &msg, 0);if (ret != 1)ERR_EXIT(
这篇关于libevent 多线程调用event_base的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!