Port-forwarding, httpservletrequest, and a bug caused by negligence

Source: Internet
Author: User

Recently I took over a bug. It is strange that the WebService released by the product does not work and the error "connection refused" is returned.

After checking, all the WebService calls reported by the error come from the server's local machine, that is, the WebService released on the local machine is called in the code of the local machine. It's ugly, but it won't report an error.

After some investigation, we found that the access from the client to the server was not allowed directly, but the request for the public server was mapped to the private server through NAT port-forwarding, not only the IP address was mapped, the port is also mapped, that is, the URL: http: // ip: port called by the client is not the IP address of the Real Server or the port of the HTTP listener, these are the IP and port of the public network machine. After receiving the request, the public network machine maps to the private IP and port.

OK, this is OK, because the part that does not involve calling the WebService on the local machine is normal. After checking the WebService calling method of the local machine in the Code, the following information is found:Code:

 
Servletrequest. getserverport ()

What a look at no problem, a careful look really have a problem, the following is the explanation of JAVA-API:

Getserverport public int getserverport () returns the port number to which the request was sent. it is the value of the part after ":" in the Host header value, if any, or the server port where the client connection was accepted on. returns: an integer specifying the port number

The returned value is the port in the request URL of the client. As described above, this port is not actually the port of the server but the port of the public network. Ha, the problem was originally found here! So how to fix it?

 
Getlocalport public int getlocalport () returns the Internet Protocol (IP) Port Number of the interface on which the request was received ed. Returns: an integer specifying the port number since: 2.4

In the absence of port-forwarding, the two APIs return exactly the same time, but this case is quite different, this also explains why the bug was not found at the factory.

This record has been checked and written in disorder. Any comment please free let me know.

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.