Obtain the IP address of a computer

Source: Internet
Author: User
Zookeeper

Internet Protocol address (Internet Protocol address), abbreviated as IP address ). An IP address is a Unified IP address format provided by the IP protocol. It allocates a logical address to each network and each host on the Internet to shield the differences between physical addresses.


You can use the ipconfig name in cmd to obtain the IP address in the computer.

Open cmd


Enter the ipconfig command


In programming, you can use the Winsock library to obtain IP addresses.


Several functions are required:


Wsastartup Function

Function Name: wsastartup

Function prototype: int wsastartup (word wversionrequested, lpwsadata );

Function function: Initialize the Winsock database parameter: wversionrequested: receives a word-type Integer. The low value indicates the main version number, and the high value indicates the sub-version number lpwsadata: wsadata structure pointer, saving the Winsock information obtained by the function.
The sockaddr_in struct is used to save the customer's address struct sockaddr_in {short sin_family // address family, winsock must be af_inet unsigned short sin_port // network byte port number struct in_addr // network byte IP address char sin_zero [8] // useless, only to increase the length of 8 bytes}
The length of the ADDR struct is the same as that of the ADDR struct.
Scokaddr_in is used to store host address information, but the IP address and port of the host must be in the network byte sequence. Generally, the IP address "192.168.0.10" and port 80 are in the host byte sequence.
Conversion between network byte sequence and host byte sequence
Short Integer Conversion notohs to htonsu_short notohs (// convert the network byte order u_short to the host byte order u_short netshort // u_short to be converted );
U_short htons (// convert the host's byte u_short to the network byte u_short host short // u_short to be converted );
IP address translation: inet_addr and inet_ntoaunsingned long inet_addr (// convert string (for example, "192.168.0.10") to u_long const char far * CP );

Char far * inet_ntoa (// convert sin_addr in the sockaddr_in struct to struct in_addr in );
Program code
# Include <stdio. h ># include <winsock2.h> // connect to the Winsock library # pragma comment (Lib, "ws2_32.lib") void main () {// call wsatartup to initialize the Winsock library wsadata ;:: wsastartup (makeword (2.2), // version 256 & wsadata); char szhost [256]; // buffer for storing host names: gethostname (szhost ); // obtain the local host name hostent * phost =: gethostbyname (szhost ); // obtain the address information through the host name // a host may have multiple NICs, multiple IP addresses, and the following code outputs all IP addresses in_addr ADDR; For (INT I = 0 ;; I ++) {// obtain the address (Network byte) char * P = phost-> h_addr_list [I]; If (null = P) {break; // exit the loop} // copy the address to the in_addr struct memcpy (& ADDR. s_un.s_addr, P, phost-> h_length); // convert in_addr to host byte char * strip =: inet_ntoa (ADDR ); // print the IP address printf ("local IP [% d]: % s \ n", I + 1, strip);} // print the host name printf ("Host Name: % s \ n ", szhost); System (" pause ");}

Execution result

Obtain the IP address of a computer

Related Article

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.