查找在.net下一个TCP端口 [英] Find the next TCP port in .Net

查看:244
本文介绍了查找在.net下一个TCP端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个新的net.tcp://本地主机:X /服务端点的WCF服务调用,具有动态分配新的开放的TCP端口

I want to create a new net.tcp://localhost:x/Service endpoint for a WCF service call, with a dynamically assigned new open tcp port.

我知道,当我开到给定服务器的连接的TcpClient将分配一个新的客户端端口。

I know that TcpClient will assign a new client side port when I open a connection to a given server.

有没有一种简单的方法来找到在.NET中的下一个开放TCP端口?

Is there a simple way to find the next open TCP port in .Net?

我需要的实际数量,这样我就可以建立上面的字符串,0不工作,因为我需要这个字符串传递给另一个进程,这样我可以再打上新的渠道。

I need the actual number, so that I can build the string above, 0 does not work, since I need to pass that string to another process, so that I can call back on that new channel.

推荐答案

这是我一直在寻找的:

static int FreeTcpPort()
{
  TcpListener l = new TcpListener(IPAddress.Loopback, 0);
  l.Start();
  int port = ((IPEndPoint)l.LocalEndpoint).Port;
  l.Stop();
  return port;
}

这篇关于查找在.net下一个TCP端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆