Get IP address from socket descriptor?

后端 未结 4 1154
轻奢々
轻奢々 2021-01-19 03:06

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_p         


        
4条回答
  •  不思量自难忘°
    2021-01-19 03:53

    You can't use the socket to get the server's address before a client connects, because it isn't known.

    In principle, a host may have multiple IPs. The IP used for a connection to a server is the one belonging to the interface, through which the connection arrived. Until a connection arrives, it isn't known.
    Even if you have only one IP, connections may arrive from within the machine, in which case the address would be 127.0.0.1.

    So the listening socket has no information about the IP.
    You'll need to find what interfaces the machine has, and what's their IPs.

提交回复
热议问题