Obtain the IPv4 address through request. getremoteaddr () in Windows 7.

Source: Internet
Author: User

We often use request. getremoteaddr () to obtain remote IP Addresses During statistics or web development. You may encounter some problems. First, it may be that the value returned by this method is not the IP address we want, but a string of values 0: 0: 0: 0: 0: 1. If you know the TCP/IP protocol, you may know that it is an IPv6 protocol. Unfortunately, we want an IP address like 127.0.0.1. In fact, this situation occurs only when your server and your client are on the same machine.

Method 1:

The reason is that the file c: \ windows \ system32 \ drivers \ etc \ hosts contains the following two lines:

 

Code
  1. #127.0.0.1 localhost
  2. #: 1 localhost

 

These two are actually one for IPv6 and the other for IPv4. If it is win7, IPv6 is used by default. Therefore, the obtained value is 0: 0: 0: 0: 0: 0: 0: 0: 0: 1, XP will not have this problem. If win7 is used, the solution is to delete the line #: 1 localhost, or maintain this line as a comment and cancel #127.0.0.1
# Of the localhost line. Most articles on the Internet say that the solution here is to cancel the comment on this line. If you just cancel the comment, it means that the IPv6 protocol is used for connection, which may lead to an error in your database connection.

Of course, this issue is mainly caused by win7. Because only win7 uses IPv6.

Method 2:

Add -djava.net. preferipv4stack = true to the local Tomcat parameter so that Tomcat only supports IPv4.

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.