从socket描述符获取IP地址?

问题描述:

我已经打开一个TCP套接字服务器(我省略了一些东西,它是从的这里

I've opened a TCP socket server (I've omitted a few stuff, it is taken from here

sockfd = socket(p->ai_family, p->ai_socktype,
            p->ai_protocol))

是否有可能得到的sockfd的服务器的IP地址?如果不是我应该在哪里看?

Is it possible to get the IP address of the server from sockfd? If not where should I look?

编辑:我想知道服务器的地址(这是任何客户端连接之前)

I want to know the address of the server (this is before any client connects).

如果你想知道谁在你的插座的另一端,你可以使用的在Linux中getpeername getsockname 会告诉你,你是谁。你决定你的服务器坐在最初不过,在 时绑定哪个地址。

If you want to know who's at the other end of your socket you can use getpeername in Linux. getsockname will tell you who you are. You decide what address you want your server to sit on initially though, at bind time.

您也可以找到这太问题有用:绑定套接字到网络接口​​>

You may also find this SO question useful: bind socket to network interface

书unix网络编程,第1卷,由理查德·史蒂文斯。

And the book "Unix Network Programming, vol 1", by W. Richard Stevens.