当您在 windows 上的 zeromq/netmq 上创建一个套接字时,一个专用套接字用于在 io 线程和用户线程之间发出信号,该套接字占用两个端口。如果您调用 bind,则将另一个端口与您选择的端口绑定。
专用套接字正在使用动态端口范围(netmq),因此如果您远离该范围,则不会有任何问题。
windows vista 及更高版本的动态端口范围是 49152 到 65535
港口计数代码:
static void Main(string[] args)
{
var id = Process.GetCurrentProcess().Id;
using (var context = NetMQContext.Create())
{
List<NetMQSocket> sockets = new List<NetMQSocket>();
NetMQSocket server = context.CreateDealerSocket();
server.Bind("tcp://localhost:6666");
int i= 0;
while (true)
{
var client = context.CreateDealerSocket();
client.Connect("tcp://localhost:6666");
sockets.Add(client);
Thread.Sleep(1000);
ProcessStartInfo startInfo = new ProcessStartInfo("NETSTAT.EXE", "-a -o");
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
Console.WriteLine("Calculating taken ports...");
Process process = Process.Start(startInfo);
int portCounter = -7; // start with minus 4 for framework and server socket
while (!process.StandardOutput.EndOfStream)
{
if (process.StandardOutput.ReadLine().Contains(id.ToString()))
{
portCounter ++;
}
}
Console.Clear();
Console.WriteLine("{0} sockets takes {1} ports, avg of {2} ports per socket", sockets.Count, portCounter, portCounter / sockets.Count);
Console.WriteLine("Press enter to create another socket");
Console.ReadLine();
}
}
}