简体   繁体   English

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

[英]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.

相关问题 如何在选定的计算机上找到.NET远程内存泄漏? - How do I find a .NET remoting memory leak on select machines? 如何测量用于.NET远程处理的IP端口的字节数? - How do I measure bytes in/out of an IP port used for .NET remoting? 如何处理从.net远程处理的HTTP 302重定向? - How do I handle http 302 redirect from .net remoting? 如何通过TCP增加.NET Remoting使用的线程数? - How do you increase the number of threads used by .NET Remoting over TCP? 我如何通过 Z2D50972FECD376129545507F1062089Z 远程调用模拟 windows 认证 web 用户? - how do i impersonate a windows authenticated web user over a .net remoting call? 如何获得ASP .NET中客户端的临时端口号? - How do you get the ephemeral port number of a client in ASP .NET? .NET Remoting-如何对所有连接的客户端进行回调? - .NET Remoting - How to do a callback to all connected clients? 在.NET Remoting中,您如何判断对象是否用作远程对象? - In .NET Remoting how do you tell if an object is used as a remotable object? 如何获取HttpWebRequest的本地端口号? - How do I get the local port number of an HttpWebRequest? 在.NET中哪个是更好的Remoting或WebServices? - Which one is better Remoting or WebServices in .NET?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM