libevent将信号封装为socket通知的核心代码
2024-08-25 15:03:39
#include"stdafx.h"
#include"iostream"
#include "algorithm"
#include"string"
#include"vector"
#include"set"
#include"map"
#include "iostream"
#include "thread"
#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>
#include "rapidxml.hpp"
#include "rapidxml_utils.hpp"
#include <winsock2.h>
#include <windows.h>
#define MAX_PATH 256
#pragma comment( lib, "ws2_32.lib") int
evutil_socketpair(int family, int type, int protocol, int fd[])
{
/* This code is originally from Tor. Used with permission. */ /* This socketpair does not work when localhost is down. So
* it's really not the same thing at all. But it's close enough
* for now, and really, when localhost is down sometimes, we
* have other problems too.
*/
int listener = -;
int connector = -;
int acceptor = -;
struct sockaddr_in listen_addr;
struct sockaddr_in connect_addr;
int size;
int saved_errno = -; listener = socket(AF_INET, type, );
if (listener < )
{
return -;
}
memset(&listen_addr, , sizeof(listen_addr));
listen_addr.sin_family = AF_INET;
listen_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
listen_addr.sin_port = ; /* kernel chooses port. */
int err;
if (SOCKET_ERROR == bind(listener, (struct sockaddr *) &listen_addr, sizeof(listen_addr)))
return -;
if (listen(listener, ) == -)
goto tidy_up_and_fail; connector = socket(AF_INET, type, );
if (connector < )
goto tidy_up_and_fail;
/* We want to find out the port number to connect to. */
size = sizeof(connect_addr);
if (getsockname(listener, (struct sockaddr *) &connect_addr, &size) == -)
goto tidy_up_and_fail;
if (size != sizeof(connect_addr))
goto abort_tidy_up_and_fail;
if (connect(connector, (struct sockaddr *) &connect_addr,
sizeof(connect_addr)) == -)
goto tidy_up_and_fail; size = sizeof(listen_addr);
acceptor = accept(listener, (struct sockaddr *) &listen_addr, &size);
if (acceptor < )
goto tidy_up_and_fail;
if (size != sizeof(listen_addr))
goto abort_tidy_up_and_fail;
/* Now check we are talking to ourself by matching port and host on the
two sockets. */
if (getsockname(connector, (struct sockaddr *) &connect_addr, &size) == -)
goto tidy_up_and_fail;
if (size != sizeof(connect_addr)
|| listen_addr.sin_family != connect_addr.sin_family
|| listen_addr.sin_addr.s_addr != connect_addr.sin_addr.s_addr
|| listen_addr.sin_port != connect_addr.sin_port)
goto abort_tidy_up_and_fail;
fd[] = connector;
fd[] = acceptor; return ; abort_tidy_up_and_fail:
saved_errno = WSAECONNABORTED;
tidy_up_and_fail:
return -;
} void main(){
WORD wVersionRequested;
WSADATA wsaData;
int err, iLen;
wVersionRequested = MAKEWORD(, );//create 16bit data
err = WSAStartup(wVersionRequested, &wsaData); //load win socket
int ev_signal_pair[];
evutil_socketpair(
AF_UNIX, SOCK_STREAM, , ev_signal_pair);
send(ev_signal_pair[], "nihao", , );
char c[] = { };
recv(ev_signal_pair[], c, , );
std::cout << c;
system("pause");
}
最新文章
- JS中的decodeURIComponent和encodeURIComponent
- MySQL For Windows修改最大连接数
- JavaScript 写几个简单的知识点
- PAT乙级 1019. 数字黑洞 (20)
- HZAU 17:LCS
- python中的 zip函数详解
- 【BZOJ】【2879】【NOI2012】美食节
- linux ubuntu卸载软件
- C字符串总结+字符串库实现(增,改,删,查):
- win10 uwp 通知列表
- 一:Tomcat 服务器 在45秒内未启动成功
- about:firefox set
- SQL Update
- php用PDO查询mysql数据库结果中文乱码
- easyUI dialog打开对话框,显示列表数据,选取一条数据操作后赋值给父窗口 resultMap声明为全局,生成getset方法
- XRecyclerView上拉刷新下拉加载
- zookeeper-架构设计与角色分工-《每日五分钟搞定大数据》
- js判断是手机端还是pc端访问
- (转)Springboot定时任务
- ubuntu 把软件源修改为国内源和更新