[英]How do I find which port number .Net Remoting Allocates?
If I create a TcpChannel using port zero ie allowing .Net Remoting to allocate an available port, is there anyway to then determine which port number has been allocated? 如果我使用端口0创建一个TcpChannel,即允许.Net Remoting分配一个可用端口,那么无论如何都要确定分配了哪个端口号?
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. 我知道我可以在创建频道时指定端口号,但我不想这样做,因为我想在同一Citrix服务器上运行多个侦听应用程序实例 - 每个实例都在另一个端口上侦听。
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.
我只想让端口自动分配 - 但是我需要知道哪个端口号已被分配。
I don't know much about this, but browsing at MSDN it states that post zero usage returns a TcpServerChannel
, and a TcpServerChannel
has a GetChannelUri()
method; 我不太了解这一点,但是在MSDN上浏览它说明零后使用返回
TcpServerChannel
,而TcpServerChannel
有一个GetChannelUri()
方法; does that include the port number? 那包括端口号吗? (you might need to parse, via
new Uri(s).Port
). (您可能需要通过
new Uri(s).Port
进行解析)。
Again, complete guess-work. 再次,完整的猜测工作。 If not, just say ;-p
如果没有,只要说;-p
edit by AakashM to add This is the correct approach. 由AakashM编辑添加这是正确的方法。 Following
以下
var channel = new TcpChannel(0);
the dynamically-allocated post of the contained server channel can be retrieved with 可以使用检索包含的服务器通道的动态分配的帖子
var channelData = (ChannelDataStore)channel.ChannelData;
var port = new System.Uri(channelData.ChannelUris[0]).Port;
The ugly cast is necessary because the TcpChannel.ChannelData
property is typed as object
... 丑陋的
TcpChannel.ChannelData
是必要的,因为TcpChannel.ChannelData
属性被输入为object
...
My solution was as follows: 我的解决方案如下:
Use the following code to identify an unused port for each instance of the client application: 使用以下代码为客户端应用程序的每个实例标识未使用的端口:
IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 0); using (Socket socket = new Socket( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) { socket.Bind(endPoint); IPEndPoint local = (IPEndPoint)socket.LocalEndPoint; return local.Port; }
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. 通过命令行参数将存储的端口号传递给主机应用程序,以便在设置TcpChannel和调用Activator.GetObject时使用。
Use the stored port number in the client application in the URL passed to Activator.GetObject. 在传递给Activator.GetObject的URL中使用客户端应用程序中存储的端口号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.