Determine address family of an unbound socket
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 su开发者_如何转开发pported 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);
)
Since I am using Winsock I did search the MSDN for a short while and found WSADuplicateSocket.
WSAPROTOCOL_INFO proto;
WSADuplicateSocket(Socket, GetCurrentProcessId(), &proto);
SocketAF = proto.iAddressFamily;
Works fine for windows machines...
精彩评论