如何设置特定的源端口 [英] how to set specific source port
问题描述
我正在使用Spring的RestTemplate
类从组织外部的Web服务获取XML.
I'm using Spring's RestTemplate
class to get XML from a web service outside my organisation.
由于本地防火墙规则的更改,我需要在HTTP请求中指定特定的源端口.现在,我在
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.
这不可能吗?
更一般地说,在在线描述中,我发现使用特定的源端口通常是基于UDP的连接/应用程序完成的.这是TCP通常不做的事情吗?
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);
推荐答案
TCP连接的源端口是从临时端口范围49152-65535中随机选择的. TCP端口范围如下所示: RFC 6056-临时端口
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
由IANA定义的动态端口范围包括49152-65535 范围,并且用于临时端口的选择."
"The dynamic port range defined by IANA consists of the 49152-65535 range, and is meant for the selection of ephemeral ports."
端口选择过程因所使用的操作系统而异.该级别比典型的Java应用程序低得多,因此不在您的应用程序控制范围之内.
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.
从技术上讲,您可以强制OS选择指定的端口,但是正如我所提到的,它特定于OS,并且破坏了Java应用程序的经典可移植性.在超级用户上有一篇很好的帖子谈论了这个主题
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.
这篇关于如何设置特定的源端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!