0

I'm a c# programmer and i need to implement a multicast socket in c++.
I've try to google search it and didnt 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.

Gabriel
  • 1,435
  • 3
  • 17
  • 24

1 Answers1

0

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.

duselbaer
  • 935
  • 2
  • 6
  • 10
  • Isn't this a Socket class for c++ #include #include – Gabriel Apr 06 '11 at 10:32
  • These are plain-C functions for dealing with sockets (TCP/IP). Usually you don't want to use these low level socket functions directly because it's much more complex than just using a higher level framework. – duselbaer Apr 06 '11 at 10:37
  • Another advantage of Boost.Asio is that it is platform independent. – duselbaer Apr 06 '11 at 10:38
  • @duselbaer This app will run on Windows desktop machine , I dont need platform independent. With Boost.Asio framework i dont know if the license permits me to use it. – Gabriel Apr 06 '11 at 10:42
  • The license should be ok for closed source software. Many people use it. Sorry, I'm not familiar with WCF or any other MS specific stuff. We use Asio for networking on Windows :) On http://ntrg.cs.tcd.ie/undergrad/4ba2/multicast/antony/example.html I found a multicast example which uses plain socket functions. It should be somehow transferrable to the WinSock API – duselbaer Apr 06 '11 at 11:10
  • @duselbaer thanks i will try to take the boost route and see if i can manage – Gabriel Apr 06 '11 at 11:48
  • 3
    @duselbaer this is an ancient post but it's frustrating to find a post that reads "use boost" when looking for an answer. Personally I think boost is fantastic but if someone is trying to implement multicasting themselves this is a poor answer. – Connor Hollis Apr 01 '14 at 19:53
  • To me his question sounded like he doesn't know about boost and it is not a must to implement it on his own.So in my opinion it is a valid thing to tell him that there is something which might do the trick for him without spending too much time on implementing his own socket class - aka inventing the wheel again doing the same errors all the others did before. – duselbaer Apr 02 '14 at 06:15
  • 1
    @Connor Hollis What would you suggest to the OP if not boost? I have the same question. I want to make a multicast example for myself using c++. Platform doesn't matter. – Gaurav Jul 08 '14 at 19:15
  • 1
    @Guarav Boost is great. I think that it is the answer to "don't reinvent the wheel" for many casual and professional programmers. The problem I had was that any library can obfuscate the actual functionality of the question at hand. If someone wants to know HOW to implement multicasting then Boost is not the answer for stackoverflow. If someone NEEDS multicasting then Boost can be the answer. Hopefully that isn't too tangential and helps put you in the right direction. – Connor Hollis Jul 09 '14 at 00:27