如何从Windows中的套接字获取IP地址

时间:2021-01-04 07:12:57

I have the DWORD socket in windows. I need to know if it is a connection that goes out to the internet or if it is a local connection, to some form of localhost. Is there a good way to get the address that the socket is connected to in windows from just the socket? Or is there a better way to tell if the connection is local or not?

我在Windows中有DWORD套接字。我需要知道它是一个连接到互联网,或者它是本地连接,是某种形式的localhost。有没有一种很好的方法可以从socket中获取套接字连接到windows的地址?或者有更好的方法来判断连接是否是本地连接?

1 个解决方案

#1


You probably want to call getpeername(). Using it is pretty basic, you pass a sockaddr pointer and a length and it fills in the data for you.

你可能想调用getpeername()。使用它是非常基本的,你传递一个sockaddr指针和一个长度,它为你填充数据。

As far as determining if the connection is local, getaddrinfo() can give you a list of all available local addresses. You would compare the result of getpeername() to the local address list.

至于确定连接是否是本地连接,getaddrinfo()可以为您提供所有可用本地地址的列表。您可以将getpeername()的结果与本地地址列表进行比较。

#1


You probably want to call getpeername(). Using it is pretty basic, you pass a sockaddr pointer and a length and it fills in the data for you.

你可能想调用getpeername()。使用它是非常基本的,你传递一个sockaddr指针和一个长度,它为你填充数据。

As far as determining if the connection is local, getaddrinfo() can give you a list of all available local addresses. You would compare the result of getpeername() to the local address list.

至于确定连接是否是本地连接,getaddrinfo()可以为您提供所有可用本地地址的列表。您可以将getpeername()的结果与本地地址列表进行比较。