如何找到 .Net Remoting 分配的端口号?
如果我使用端口零创建 TcpChannel,即允许 .Net Remoting 分配可用端口,是否有办法确定已分配哪个端口号?
我知道我可以在创建通道时指定端口号,但我不想这样做,因为我想在同一 Citrix 服务器上运行侦听应用程序的多个实例 - 每个实例侦听不同的端口。
理想情况下,我不想麻烦地保留一堆端口,然后跟踪哪些端口已被分配。 我只想让端口自动分配 - 但随后我需要能够知道已分配哪个端口号。
If I create a TcpChannel using port zero i.e. allowing .Net Remoting to allocate an available port, is there anyway to then determine which port number has been allocated?
I know that I can specify the port number when creating the channel, however I don't want to do this as I want to run multiple instances of the listening application on the same Citrix server - each instance listening on a different port.
Ideally I don't want to have to go to the trouble of reserving a bunch of ports and then keeping track of which ones have been allocated. I'd just like to let the port be allocated automatically - but then I need to be able to know which port number has been allocated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我对此不太了解,但在 MSDN 上浏览它指出零使用后返回一个
TcpServerChannel
,并且TcpServerChannel
有一个GetChannelUri()方法; 这包括端口号吗? (您可能需要通过
new Uri(s).Port
进行解析)。再次,完成猜测工作。 如果没有,只需说 ;-p
由 AakashM 编辑来添加 这是正确的方法。 可以通过以下方式
检索包含的服务器通道的动态分配的帖子。
丑陋的转换是必要的,因为
TcpChannel.ChannelData
属性被键入为object
...I don't know much about this, but browsing at MSDN it states that post zero usage returns a
TcpServerChannel
, and aTcpServerChannel
has aGetChannelUri()
method; does that include the port number? (you might need to parse, vianew Uri(s).Port
).Again, complete guess-work. If not, just say ;-p
edit by AakashM to add This is the correct approach. Following
the dynamically-allocated post of the contained server channel can be retrieved with
The ugly cast is necessary because the
TcpChannel.ChannelData
property is typed asobject
...我的解决方案如下:
使用以下代码来识别客户端应用程序的每个实例的未使用端口:
将未使用的端口号存储在客户端应用程序中。
通过命令行参数将存储的端口号传递给主机应用程序,以便在设置 TcpChannel 和调用 Activator.GetObject 时使用。
通过命令行参数
在传递给 Activator.GetObject 的 URL 中使用客户端应用程序中存储的端口号。
My solution was as follows:
Use the following code to identify an unused port for each instance of the client application:
Store the unused port number in the client application.
Pass the stored port number to the host application via a command line parameter for use when setting up the TcpChannel and calling Activator.GetObject.
Use the stored port number in the client application in the URL passed to Activator.GetObject.