确定未绑定套接字的地址族
当我尝试绑定我的套接字时,我需要在 addrinfo/sockaddr 结构中指定地址族,我传入对 bind() 的调用,否则将收到一个 winsock 错误 10047(协议族不支持地址族。)
如果我在其他地方创建了一个不知道其地址族的套接字怎么办? 我怎样才能获得该套接字的地址族,以便我可以绑定它?
getsockname 仅适用于绑定套接字:/...
我不想强制函数的调用者将地址族传递给我的函数,特别是如果她/他/它在创建她/他的套接字时使用 AF_UNSPEC/PF_UNSPEC
(例如 SOCKET sock = socket(AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP); )
When I try to bind my socket I need to specify the address family in the addrinfo/sockaddr-structure I pass in to the call to bind(), or one will get an winsock error 10047 (Address family not supported by protocol family.)
What if I created a socket elswhere not knowing its address family...
How could I get the address family for that socket, so i can bind it?
getsockname only works for bound sockets :/...
I don´t want to force the caller of my function to pass the address family to my functions, especially if she/he/it uses AF_UNSPEC/PF_UNSPEC when creating her/his socket
(e.g. SOCKET sock = socket(AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP);
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于我使用的是 Winsock,所以我在 MSDN 上搜索了一会儿,发现了 WSADuplicateSocket。
适用于 Windows 机器...
Since I am using Winsock I did search the MSDN for a short while and found WSADuplicateSocket.
Works fine for windows machines...