gpt4 book ai didi

c - 在 Windows 上为一个 libevent 绑定(bind)创建 3 个套接字?

转载 作者:行者123 更新时间:2023-11-30 14:49:25 29 4
gpt4 key购买 nike

我正在编写一个 BitTorrent 客户端,以了解有关网络的更多信息,并且我正在努力通过 Google 找到一些东西。作为 BT 规范的一部分,我需要一台服务器来监听与我的计算机的对等连接。在 win32 端口中,我有这样的代码来设置我的服务器

struct sockaddr_in saServer;
struct hostent* localHost;
char* localIP;

// Get the local host information
localHost = gethostbyname("");
localIP = inet_ntoa(*(struct in_addr *)*localHost->h_addr_list);

saServer.sin_family = AF_INET;
saServer.sin_addr.s_addr = inet_addr(localIP);
saServer.sin_port = htons(6881);


struct evconnlistener *listener = evconnlistener_new_bind(base, accept_conn_cb, NULL,
LEV_OPT_CLOSE_ON_FREE | LEV_OPT_REUSEABLE, -1, (SOCKADDR*)&saServer, sizeof(saServer));

这似乎有效,但如果我查看 netstat,我会看到以下内容,

BitTorrentClient.exe    6092    TCP hostname    50216   localhost   50217   ESTABLISHED                                     
BitTorrentClient.exe 6092 TCP hostname 50217 localhost 50216 ESTABLISHED
BitTorrentClient.exe 6092 TCP hostname.home 6881 hostname 0 LISTENING

为什么还有另外两个连接,一个来自端口 50216->50217,另一个从 50217->50216 环回?我预计端口 6881 上只有一个监听连接。这是 libevent 的一个怪癖,还是与网络相关的更基本的东西?

我可以搜索什么来了解其他两个端口的用途?

最佳答案

这很可能是 libevent 调用 evutil_make_internal_pipe_ 的结果内部。

Libevent 为 inter-thread communication 创建内部“管道”和 signal delivery使用socketpair(2)在 POSIX 兼容系统上,而在 Windows 上 libevent 必须手动求助 connecting two sockets together相反。

关于c - 在 Windows 上为一个 libevent 绑定(bind)创建 3 个套接字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49609737/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com