Obtains the IP address of a computer.

Source: Internet
Author: User

Obtains the IP address of a computer.
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 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 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

Which of the following methods can a computer obtain an IP address?

Dynamic and Static.
Dynamic means that the local server will automatically pick one from the idle IP address pool when dialing each time.

Static means that the local server keeps the same IP address for your device. The IP address is allocated to you every time you log on (this is also DHCP in theory ). Even if you go offline, the IP address will not be used by others. Another static method is to allow you to set an IP address on the machine you dial. Of course, this IP address is provided by the local authority to tell you which IP address you can use. Then, the IP address will not be put into the IP address pool by the local IP address and will not be occupied by other users.

How to obtain the IP address of your computer

1. Click Start ==> run ==> cmd ==> press enter and enter ipconfig/all in the DOS window that appears.
2. right-click "Network Neighbor", select "attribute", select the network adapter you want to know the IP address, double-click, and click the support tab. if you still want to know about DNS, click "details ".

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.