C语言 C 套接字从接受返回的文件描述符获取 IP 地址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20472072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C socket get IP address from filedescriptor returned from accept
提问by Aleix
I know this question seems typicaland multiple times answered but I think if you read the details it is not so common (I did not find it).
我知道这个问题似乎很典型并且多次回答,但我认为如果您阅读详细信息,它并不常见(我没有找到)。
The point is that I am developing a unix service in c that opens a socketand waits for connections, when I have a connection I create a new process to treat it, so there can be multiple connections opened at the same time.
关键是我正在用 c开发一个unix 服务,它打开一个套接字并等待连接,当我有一个连接时,我创建一个新进程来处理它,所以可以同时打开多个连接。
int newfd = accept(sockfd, (struct sockaddr *)&clientaddr, (socklen_t*)&clientaddr_size);
Later on (after and inside some other methodsand code) the child processsave the connection information to the BBDD and I need also, in that precise moment, to get the IP addressthat opened that connection being treated.
后来(后和其他一些方法里面和代码)子进程保存连接信息BBDD和我还需要,在那一时刻,以获取IP地址是打开的治疗连接。
As there can be multiple connections at the same timeand the variable struct sockaddr_in clientaddrthat I pass to the accept method is shared for all the processI am not sure that later on is a good idea to get the IP address information from that way because then I could get the IP address from another connection opened.
由于可以同时存在多个连接,并且我传递给 accept 方法的变量struct sockaddr_in clientaddr是为所有进程共享的,因此我不确定以后从这种方式获取 IP 地址信息是否是个好主意,因为那时我可以从另一个打开的连接获取 IP 地址。
I would like to be able to access the IP address from the file descriptor int newfdthat I get from the accept method(the returned integer). Is it possible? Or I misunderstood the file descriptor function?
我希望能够从int newfd我从接受方法(返回的整数)获得的文件描述符中访问 IP 地址。是否可以?还是我误解了文件描述符功能?
回答by Aleix
Ok. Thanks to @alk and @rileyberton I found the correct method to use, the getpeername:
好的。感谢 @alk 和 @rileyberton 我找到了正确的使用方法,getpeername:
int sockfd;
void main(void) {
//[...]
struct sockaddr_in clientaddr;
socklen_t clientaddr_size = sizeof(clientaddr);
int newfd = accept(sockfd, (struct sockaddr *)&clientaddr, &clientaddr_size);
//fork() and other code
foo(newfd);
//[...]
}
void foo(int newfd) {
//[...]
struct sockaddr_in addr;
socklen_t addr_size = sizeof(struct sockaddr_in);
int res = getpeername(newfd, (struct sockaddr *)&addr, &addr_size);
char *clientip = new char[20];
strcpy(clientip, inet_ntoa(addr.sin_addr));
//[...]
}
So now in a different process I can get the IP address (in the "string" clientip) of the client that originated the connection only carrying the file descriptor newfdobtained with the accept method.
所以现在在一个不同的过程中,我可以获得clientip发起连接的客户端的 IP 地址(在“字符串”中),只携带newfd使用 accept 方法获得的文件描述符。
回答by rileyberton
You would use getsockname()(http://linux.die.net/man/2/getsockname) to get the IP of the bound socket.
您可以使用getsockname()( http://linux.die.net/man/2/getsockname) 来获取绑定套接字的 IP。
Also answered before, here: C - Public IP from file descriptor
之前也回答过,在这里:C - 来自文件描述符的公共 IP
回答by user1602
Statefull connection is uniquely identified by two end points Peer(address:port)<=>My(address:port). Both getpeername() and getsockname() are needed to get this information.
全状态连接由两个端点 Peer(address:port)<=>My(address:port) 唯一标识。getpeername() 和 getsockname() 都需要获取此信息。

