如何在c ++中实现组播套接字

时间:2011-04-06 10:18:27

标签: c++ multithreading wcf sockets multicastsocket

我是一名c#程序员,我需要在c ++中实现一个多播套接字 我试着谷歌搜索它并没有找到太多的帮助 因此,如果有人能给我一些好的c ++多播套接字教程链接,我们将非常感激  我的c#套接字实现如下所示:

public class UdpMulticast
    {
        private Socket s;
        private Thread listenThread;
        private string mcastGroup;
        private int port;

        public UdpMulticast(string mcastGroup, int port)
        {
            this.mcastGroup = mcastGroup;
            this.port = port; 
        }

        private void Listen()
        {
            while (true)
            {
                try
                {
                    byte[] b = new byte[1024];
                    Thread.Sleep(1);                    
                    int recv = s.Receive(b);
                    if (OnNewDataRecv != null)
                    {
                        byte[] tmp = new byte[recv];
                        for (int i = 0; i < recv; i++)
                        {
                            tmp[i] = b[i];
                        }
                        byte[] decByte = Encryption.Decrypt(tmp);
                        if(this.OnNewDataRecv !=null)
                            this.OnNewDataRecv(decByte, decByte.Length);                        
                    }

                    if (s == null)
                    {
                        break;
                    }
                }
                catch (ThreadAbortException)
                {
                    break;
                }
                catch (Exception ex)
                {

                    Console.WriteLine(ex.ToString());
                }
            }
        }

        public delegate void newData4Send(byte[] data, int dataLen);
        public event newData4Send OnNewDataRecv;

        public bool StartListen()
        {
            bool ret = false;
            try
            {
                if (s == null)
                {
                    s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                }
                if (s != null)
                {
                    Console.WriteLine("PORT multi cast :" + port);
                    IPEndPoint ipep = new IPEndPoint(IPAddress.Any, port);
                    s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
                    s.Bind(ipep);
                    IPAddress ip = IPAddress.Parse(mcastGroup);
                    s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(ip, IPAddress.Any));
                    s.SetSocketOption(SocketOptionLevel.IP,
                            SocketOptionName.MulticastTimeToLive, int.Parse("1"));
                    listenThread = new Thread(new ThreadStart(Listen));
                    listenThread.IsBackground = true;
                }
                if (listenThread != null)
                    listenThread.Start();
                ret = true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            return ret;
        }

        public void StopListen()
        {
            if (listenThread != null)
            {
                if (listenThread.IsAlive)
                {
                    listenThread.Abort();
                    listenThread = null;
                }
            }
            if (s != null)
            {
                s.Close();
                s = null;
            }
        }

        public void Send(byte[] data, int len)
        {
            if (s == null)
            {
                s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            }

            if (s != null)
            {
                IPEndPoint ipep = new IPEndPoint(IPAddress.Parse(mcastGroup), port);
                byte[] encByte = Encryption.Encrypt(data);
                s.SendTo(encByte, encByte.Length, SocketFlags.None, ipep);
            }
            else Console.WriteLine("s is NULL");
        }
    }

我想我在wcf中找到了一些关于它的东西,但我找不到一个好的教程。

1 个答案:

答案 0 :(得分:0)

没有Socket类或者某事。类似于普通的C ++。我建议使用像Boost.Asio这样的框架。

http://www.boost.org/doc/libs/1_46_1/doc/html/boost_asio/examples.html

文档中有一个多播示例。