I want to write a C++ wrapper for the Linux Socket API. In the socket() ctor the adress family (AF) is requested. I dont want to require the AF in the connect() signature again, becuase it has been already given in the constructor of socket. So how do I get the AF from an existing socket?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Damn. Searched about half an hour. Now after posting this question I found immiediately the answer.
getsockopt([...]) with option SO_DOMAIN (see socket options)
回答2:
POSIX-standard getsockname will help:
int getsockname(int socket, struct sockaddr *restrict address,
socklen_t *restrict address_len);
it will fill in given struct sockaddr (upon success):
struct sockaddr {
unsigned short sa_family; // <- that's what you looking for
char sa_data[14];
}