namespace Microsoft.Rfid.Reader.Driver.Acme
{
    internal class UdpTransport
    {
        private UdpClient client;
        private UdpClient asynClient;        
        private ILogger logger;
        private AcmeReaderLayer readerLayer;       
        private ManualResetEvent asynEvent;
        private const int clientPort = 4567;
        private const int asynClientPort = 4568;
        private static string localHost = Dns.GetHostName();
        private Thread asynReceiveThread;
        private string host;
        private int port;
        public UdpTransport()
        { 
        
        }
        public void Init(TcpTransportSettings settings,ILogger logger, AcmeReaderLayer readerLayer)
        {
            lock (this)
            {
                try
                {
                    this.logger = logger;
                    this.readerLayer = readerLayer;
                    this.host = settings.Host;
                    this.port = settings.Port;  
                    asynEvent = new ManualResetEvent(true);
                    IPAddress ip = Dns.GetHostAddresses(localHost)[0];
                    this.client = new UdpClient(new IPEndPoint(ip, clientPort));
                    this.asynClient = new UdpClient(new IPEndPoint(ip, asynClientPort));
                }
                catch (Exception ex)
                {
                    logger.Error("UdpTransport Contrusture Error:" + ex.ToString());
                }
            } private const int clientPort = 4567;
        private const int asynClientPort = 4568; 这两个断口,现在是固定的
现在想这的常数值,变位动态可变的在下面的两句里被引用
this.client = new UdpClient(new IPEndPoint(ip, clientPort));
                    this.asynClient = new UdpClient(new IPEndPoint(ip, asynClientPort));
要问的问题是:
在MS的BIZTALK RFID 中,每加一设备,就要走到INIT()里
而每个设备要两个端口,如何实现端口不重复?
描述:
  如有一个设备
A INTI 占 5000 和5001而再加入个设备B 则要分配其他的两个端口号,且这两个端口要未被使用。加C 
端口不允许重复 如何搞啊?

解决方案 »

  1.   

    你添加一个设备时,产生两个1024到65535的随机数,即两个端口,并将两个端口号添加到一个比如ArrayList中存储起来,等下次再添加一个设备时,再产生两个随机数,但要与ArrayList中存储的已分配的端口号作一下比较,保证没有相同的..
      

  2.   

    随机也可以啊,在5000 到8000 之间,一次产生一个,
    如得到一个数是不6000,而一次我要两个端口好,我可以用6001,
    但如何让第二次再产生的随机数和第一次产生的不相同(即和6000 、6001不同)?最好给点参考代码 谢谢代码在INTI() 实现要,INIT()的执行过程,加一个设备,执行一次INIT(),
    就说要在INIT()里做判断
      

  3.   

    写了个小例子:
    class Program
        {
            //定义一个ArrayList,用于存储已分配的端口号
            public static ArrayList list = new ArrayList();
            public static void Main()
            {
                string str = Console.ReadLine();
                while (str != "exit")
                {
                    DevicePort devicePort = GetPort();
                    Console.WriteLine("FirstPort: " + devicePort.firstPort.ToString() + "\t" + "SecondPort: " + devicePort.secondPort.ToString());
                    str = Console.ReadLine();
                }
            }
            public static DevicePort GetPort()
            {
                Random rd = new Random();
                int[] ports = new int[2];
                for (int i = 0; i < 2; i++)
                {
                    int port = rd.Next(5000, 8000);
                    while (list.Contains(port))
                    {
                        port = rd.Next(5000, 8000);
                    }
                    list.Add(port);
                    ports[i] = port;
                }
                DevicePort devicePort = new DevicePort();
                devicePort.firstPort = ports[0];
                devicePort.secondPort = ports[1];
                return devicePort;
            }    }
        struct DevicePort
        {
            public int firstPort;
            public int secondPort;
        }输出如下:
    FirstPort: 7228 SecondPort: 5639
    FirstPort: 5661 SecondPort: 5586
    FirstPort: 6299 SecondPort: 7714
    FirstPort: 5537 SecondPort: 6014
    FirstPort: 6989 SecondPort: 5682
    FirstPort: 5798 SecondPort: 5386
    FirstPort: 6175 SecondPort: 5970
    FirstPort: 6322 SecondPort: 6302
    FirstPort: 6374 SecondPort: 5142
    FirstPort: 5089 SecondPort: 6354
    FirstPort: 6312 SecondPort: 5770
    FirstPort: 6459 SecondPort: 6101
    FirstPort: 5174 SecondPort: 7313
    exit
      

  4.   

    不过有个问题,就是一旦你的应用程序关闭,ArrayList中存储的值就会丢失...是不是用数据库会更好?
      

  5.   

    namespace random
    {
        public partial class Form1 : Form
        {
           public static ArrayList list = new ArrayList();        public Form1()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
                DevicePort devicePort = GetPort();
                textBox1.Text = devicePort.firstPort.ToString();
                      }
    public static DevicePort GetPort()
            {
                Random rd = new Random();
                int[] ports = new int[2];
                for (int i = 0; i < 2; i++)
                {
                    int port = rd.Next(5000, 8000);
                    while (list.Contains(port))
                    {
                        port = rd.Next(5000, 8000);
                    }
                    list.Add(port);
                    ports[i] = port;
                }
                DevicePort devicePort = new DevicePort();
                devicePort.firstPort = ports[0];
                devicePort.secondPort = ports[1];
                return devicePort;
            }        struct DevicePort
            {
                public int firstPort;
                public int secondPort;
            }    }
    }Error 1 Inconsistent accessibility: return type 'random.Form1.DevicePort' is less accessible than method 'random.Form1.GetPort()' C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\random\random\Form1.cs 57 34 random这是什么意思?
      

  6.   

    try..namespace random
    {
    public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private  ArrayList list = new ArrayList();
            private  DevicePort GetPort()
            {
                Random rd = new Random();
                int[] ports = new int[2];
                for (int i = 0; i < 2; i++)
                {
                    int port = rd.Next(5000, 8000);
                    while (list.Contains(port))
                    {
                        port = rd.Next(5000, 8000);
                    }
                    list.Add(port);
                    ports[i] = port;
                }
                DevicePort devicePort = new DevicePort();
                devicePort.firstPort = ports[0];
                devicePort.secondPort = ports[1];
                return devicePort;
            }        private void button2_Click(object sender, EventArgs e)
            {
                DevicePort devicePort = GetPort();
                this.textBox2.Text = devicePort.firstPort.ToString();
                this.textBox3.Text = devicePort.secondPort.ToString();
            }    }
        struct DevicePort
        {
            public int firstPort;
            public int secondPort;
        }
    }
      

  7.   

    可以的我借鉴你的写成如下:
    public int[] GetPort()
            {
                Random rd = new Random();
                int[] ports = new int[2];
               
                    int port = rd.Next(1, 5);
                    while (list.Contains(port)||list.Contains(port+1)||list.Contains(5) )
                    {
                        port = rd.Next(1, 5);
                    }
                    list.Add(port);
                    list.Add(port+1);
                    ports[0] = port;
                    ports[1] = ports[0] + 1;       
              
                return ports ;
            }
    也可以的