Java Inetaddress.getlocalhost () implemented in Linux __linux

Source: Internet
Author: User
Inetaddress.getlocalhost in Java ()

The final call in Inetaddress.getlocalhost () is Inet6addressimpl.java (depending on whether you use IPv4 or IPv6) Getlocalhostname code in native

Finally, in the native code,

Jniexport jstring jnicall
java_java_net_inet6addressimpl_getlocalhostname (jnienv *env, jobject this) {
    char HOSTNAME[NI_MAXHOST+1];

    Hostname[0] = ' the ';
    if (Jvm_gethostname (hostname, Maxhostnamelen)) {/
	* Something went wrong, maybe networking is not setup? */
	strcpy (hostname, "localhost");
    } else {
       ...
   }}


Macro definition for Jvm_gethostname

Jvm_leaf (int, jvm_gethostname (char* name, int namelen))
  jvmwrapper ("Jvm_gethostname");
  Return Hpi::get_host_name (name, Namelen);
Jvm_end

Functions in Linux

inline int hpi::get_host_name (char* name, int namelen) {return
  :: GetHostName (name, Namelen);

That is, by invoking the GetHostName kernel function in Linux


implementation of GetHostName in Linux

Hostname in Linux is a variable that is implemented in the Shell startup script "/etc/rc.d/rc.sysinit" when the system initializes, mainly reading "/etc/sysconfig/network" the value of the hostname in

Here are a few points to note:

1. If there is no hostname in the file, the default localhost is used

2. If the value of hostname is found to be localhost or localhost.localdomain, find the hostname of this IP in/etc/hosts according to their actual IP.

3. If not, use localhost or localhost.localdomain


You can use the command

hostname xxx

Modify the hostname and do not need to reboot.


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.