开发者

How to implement a multicast socket in c++

开发者 https://www.devze.com 2023-02-22 04:55 出处:网络
I\'m a c# programmer and i need to implement a multicast socket in c++. I\'ve try to google search it and didnt开发者_StackOverflow中文版 find much help.

I'm a c# programmer and i need to implement a multicast socket in c++.

I've try to google search it and didnt开发者_StackOverflow中文版 find much help.

So if someone could give me some links to a good c++ multicast socket tutorial it will be highly appreciated.

My c# socket implementation looks like this:

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");
        }
    }

I think i found something about it in wcf but i cant find a good tutorial.


There's no Socket class or sth. similar in plain C++. I suggest using a framework like Boost.Asio.

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

There is a multicast example in the documentation.

0

精彩评论

暂无评论...
验证码 换一张
取 消