Get host name IP

Source: Internet
Author: User

 

Gethostname ()

Returns the standard Host Name of the local host.

# Include <Winsock2.h>

Int pascal far gethostname (char FAR * name, int namelen );

Name: a buffer pointer pointing to the host name to be stored.

Namelen: the length of the buffer.

This function stores the local host name in the buffer specified by the name parameter. The returned host name is a string ending with NULL. The host name format depends on Windows
Sockets implementation-it may be a simple host name or a domain name. However, the returned names must be used in gethostbyname () and WSAAsyncGetHostByName.

Return Value:

If no error occurs, gethostname () returns0. Otherwise it returnsSocket_error. The application can get a specific error code through WSAGetLastError.

Error code:

The length parameter of the WSAEFAULT name is too small.

WSANOTINTIALISED must successfully call WSAStartup () before applying this API ().

WSAENTDOWN Windows Sockets implementation detected network subsystem errors.

WSAEINPROGRESS a blocked Windows Sockets operation is in progress.

See gethostbyname (), WSAAsyncGetHostByName ()


Gethostbyname () struct hostent * gethostbyname (const char * name );

The input value of this function is a domain name or host name, for example, "www.google.cn. The output value is a hostent structure. If the function call fails, NULL is returned.

Struct hostent
{
Char * h_name;
Char ** h_aliases;
Int h_addrtype;
Int h_length;
Char ** h_addr_list;
# Define h_addr h_addr_list [0]
};

Hostent->H_name
Indicates the specification name of the host. For example, the standard name of www.google.com is actually www.l.google.com.
Hostent->H_aliases
The host alias .www.google.com is Google's own Alias. Sometimes, some hosts may have several aliases, which are used to retrieve multiple names for their websites for ease of user memory.
Hostent->H_addrtype
Indicates the Host IP Address type, whether it is IPv4 (af_inet) or pv6 (af_inet6)
Hostent->H_length
Indicates the length of the Host IP address.
Hostent->H_addr_lisst 
Indicates the IP address of the host. Note that,This is stored in bytes of the network.. Do not directly use printf with the % S parameter to do this. It may be a problem. Therefore, if you really need to print this IP address, you need to call inet_ntop ().

Const char *Inet_ntop(Int af, const void * SRC, char * DST, socklen_t CNT ):
This function isConvert the network address structure src of the type af to the host string format., Stored in a string with the length of CNT. Returns a pointer to DST. If a function call error occurs, the return value is null.

Reference code:

# Include <windows. h ># include <iostream> using namespace STD; # pragma comment (Lib, "ws2_32.lib") int main () {wsadata WSA; If (wsastartup (makeword (2, 3 ), & WSA )! = 0) {cout <"failed to create socket! "<Endl; sleep (3000); exit (-1);} Char host_name [255]; If (gethostname (host_name, sizeof (host_name) = socket_error) {cout <"error % d when getting local host name" <wsagetlasterror (); sleep (3000); exit (-1) ;}cout 


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.