How can I obtain the IP address of a specified interface in C?

Source: Internet
Author: User

How can I obtain the IP address of a specified interface in C?

The description of winsocket is as follows:

5.1.6 getsockname ()
Brief description:
Obtain the local name of a set of interfaces.

# Include <Winsock. h>;

Int Pascal far getsockname (socket S, struct sockaddr far * Name,
Int far * namelen );

S: The description of a bundled interface.
Name: The address (name) of the receiver interface ).
Namelen: name of the buffer length.

Note:
The getsockname () function is used to obtain the name of a set of interfaces. It is used for a bundled or connected interface S, and the local address will be returned. This call is particularly applicable to the following situations: connect () is called without calling BIND (). Only the getsockname () call can obtain the local address in the system. When returned, the namelen parameter contains the actual number of bytes of the name.
If an interface is bound with inaddr_any, that is, the interface can be connected to any host address, getsockname () unless you call connect () or accept () no information about the Host IP address is returned. Unless the interface set is connected, the Windows interface application should not assume that the IP address will change from inaddr_any to another address. This is because, in multiple host environments, the IP address used by this interface is unknown unless the set interface is connected.

Return Value:
If no error occurs, getsockname () returns 0. Otherwise, the socket_error is returned. The application can obtain the error code through wsagetlasterror.

Error code:
Wsanotinitialised: before using this API, you must successfully call wsastartup ().
Wsaenetdown: A Windows interface is used to detect the failure of the network subsystem.
Wsaefault: The namelen parameter is not large enough.
Wsaeinss SS: A blocked Windows interface call is running.
Wsaenotsock: The description is not a set of interfaces.
Wsaeinval: the set of interfaces are not bound with BIND.

See:
BIND (), socket (), getpeername ().

5.1.5 getpeername ()
Brief description:
Obtain the end address connected to the set interface.

# Include <Winsock. h>;

Int Pascal far getpeername (socket S, struct sockaddr far * Name,
Int far * namelen );

S: The description of a connected interface.
Name: Name structure of the acceptor address.
Namelen: a pointer to the name structure.

Note:
The getpeername () function is used to obtain the port name bound to it from port S and store it in the sockaddr type name structure. It is applicable to datagram or stream class interface.

Return Value:
If no error occurs, getpeername () returns 0. Otherwise, socket_error is returned. The application can use wsagetlasterror () to obtain the corresponding error code.

Error code:
Wsanotinitialised: before using this API, you must successfully call wsastartup ().
Wsaenetdown: A Windows interface is used to detect the failure of the network subsystem.
Wsaefault: The namelen parameter is not large enough.
Wsaeinss SS: A blocked Windows interface call is running.
Wsaenotconn APIs are not connected.
Wsaenotsock: The description is not a set of interfaces.

See:
BIND (), socket (), getsockname ().

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.