libevent将信号封装为socket通知的核心代码

#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[2])
{
    /* 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 = -1;
    int connector = -1;
    int acceptor = -1;
    struct sockaddr_in listen_addr;
    struct sockaddr_in connect_addr;
    int size;
    int saved_errno = -1;


    listener = socket(AF_INET, type, 0);
    if (listener < 0)
    {
        return -1;
    }
    memset(&listen_addr, 0, sizeof(listen_addr));
    listen_addr.sin_family = AF_INET;
    listen_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
    listen_addr.sin_port = 0;    /* kernel chooses port.     */
    int err;
    if (SOCKET_ERROR == bind(listener, (struct sockaddr *) &listen_addr, sizeof(listen_addr)))
        return -1;
    if (listen(listener, 1) == -1)
        goto tidy_up_and_fail;

    connector = socket(AF_INET, type, 0);
    if (connector < 0)
        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) == -1)
        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)) == -1)
        goto tidy_up_and_fail;

    size = sizeof(listen_addr);
    acceptor = accept(listener, (struct sockaddr *) &listen_addr, &size);
    if (acceptor < 0)
        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) == -1)
        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[0] = connector;
    fd[1] = acceptor;

    return 0;

abort_tidy_up_and_fail:
    saved_errno = WSAECONNABORTED;
tidy_up_and_fail:
    return -1;
}

void main(){
    WORD    wVersionRequested;
    WSADATA wsaData;
    int     err, iLen;
    wVersionRequested = MAKEWORD(2, 2);//create 16bit data  
    err = WSAStartup(wVersionRequested, &wsaData); //load win socket 
    int ev_signal_pair[2];
    evutil_socketpair(
        AF_UNIX, SOCK_STREAM, 0, ev_signal_pair);
    send(ev_signal_pair[0], "nihao", 6, 0);
    char c[100] = { 0 };
    recv(ev_signal_pair[1], c, 100, 0);
    std::cout << c;
    system("pause");
}

 

原文链接: https://www.cnblogs.com/wangshaowei/p/8762540.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    libevent将信号封装为socket通知的核心代码

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/392003

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年3月31日 上午10:52
下一篇 2023年3月31日 上午10:52

相关推荐