how to set specific source port

谁都会走 提交于 2019-12-13 08:45:41

问题


I'm using Spring's RestTemplate class to get XML from a web service outside my organisation.

Due to a change in local firewall rules, I need to specify a specific source port in my HTTP requests. Now I can't find anything in the api, on stackoverflow or any tutorial on how to do this.

Is this not possible?

More generally, in descriptions online, I have found that using a specific source port was generally done with UDP based connections/applications. Is this something that is not usually done with TCP?

(I really don't think it helps, but here's the code snippet, as asked by a commenter below):

MyRequest request = new MyRequest(); //whole thing done via jackson
RestTemplate templ = new RestTemplate();
this.serviceUrl = String.format("%s:%d", properties.getServiceUrl()
properties.getServicePort());
ExptectedResponse response = templ.postForObject(serviceUrl, request, ExptectedResponse.class);

回答1:


The source port of a TCP connection is chosen at random from the ephemeral port range 49152-65535. TCP port ranges are shown here: RFC 6056 - Ephemeral Ports

"The dynamic port range defined by IANA consists of the 49152-65535 range, and is meant for the selection of ephemeral ports."

The port selection process differs depending on the operating system being used. This is much lower level than a typical Java application and therefore is out of your applications control.

Technically you can could force the OS to select a specified port, but as I mentioned is OS specific and breaks the classic portability of a Java application. There is a good post on Super User that talks about this subject here.

I would suggest talking to the department that made the firewall change and see if they can allow outbound connections from normal port range.



来源:https://stackoverflow.com/questions/48283942/how-to-set-specific-source-port

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!