如何找到 .Net Remoting 分配的端口号?

发布于 2024-07-11 09:17:27 字数 232 浏览 7 评论 0原文

如果我使用端口零创建 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

睫毛上残留的泪 2024-07-18 09:17:27

我对此不太了解,但在 MSDN 上浏览它指出零使用后返回一个 TcpServerChannel,并且 TcpServerChannel 有一个 GetChannelUri()方法; 这包括端口号吗? (您可能需要通过new Uri(s).Port进行解析)。

再次,完成猜测工作。 如果没有,只需说 ;-p

由 AakashM 编辑来添加 这是正确的方法。 可以通过以下方式

var channel = new TcpChannel(0);

检索包含的服务器通道的动态分配的帖子。

var channelData = (ChannelDataStore)channel.ChannelData;
var port = new System.Uri(channelData.ChannelUris[0]).Port;

丑陋的转换是必要的,因为 TcpChannel.ChannelData 属性被键入为 object...

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; does that include the port number? (you might need to parse, via new Uri(s).Port).

Again, complete guess-work. If not, just say ;-p

edit by AakashM to add This is the correct approach. 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...

儭儭莪哋寶赑 2024-07-18 09:17:27

我的解决方案如下:

  • 使用以下代码来识别客户端应用程序的每个实例的未使用端口:

    IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 0); 
    
      使用 (Socket 套接字 = 新套接字( 
                   地址家庭.InterNetwork,  
                   SocketType.Stream,  
                   协议类型.Tcp)) 
      { 
          套接字.Bind(端点); 
          IPEndPoint local = (IPEndPoint)socket.LocalEndPoint; 
          返回本地端口; 
      } 
      
  • 将未使用的端口号存储在客户端应用程序中。

  • 通过命令行参数将存储的端口号传递给主机应用程序,以便在设置 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:

    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.

  • Use the stored port number in the client application in the URL passed to Activator.GetObject.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文