static void Main(string[] args)
        {
            SenderReceiver sr = new SenderReceiver();
            ReceiverSender rs = new ReceiverSender();

            byte[] sendbuf, bytebufreceive;

            while (true)
            {
                Console.WriteLine("Digite uma mensagem: ");
                sendbuf = Encoding.ASCII.GetBytes(Console.ReadLine());

                sr.s.SendTo(sendbuf, sr.ep);

                Console.WriteLine("Mensagem enviada para o endereço...");


                try
                {
                    Console.WriteLine("Esperando por mensagens...");
                    bytebufreceive = rs.listener.Receive(ref rs.groupEP);

                    Console.WriteLine("Mensagem recebida:");
                    Console.WriteLine(Encoding.ASCII.GetString(bytebufreceive, 0, bytebufreceive.Length));
                }
                catch (SocketException e)
                {
                    Console.WriteLine(e.Message);
                }
                Thread.Sleep(500);
            }
        }
Exemple #2
0
        static void ReceiveMessage(string ip, ReceiverSender rs)
        {
            SenderReceiver sr = new SenderReceiver(ip);

            byte[] bytebufreceive, bytesendreply;
            string message = "";

            while (true)
            {
                try
                {
                    bytebufreceive = rs.listener.Receive(ref rs.groupEP);
                    message        = Encoding.ASCII.GetString(bytebufreceive, 0, bytebufreceive.Length);

                    Console.WriteLine("Mensagem recebida do IP({0}): {1}", ip, message);

                    if (message.ToLower().Trim() == "heartbeat request")
                    {
                        bytesendreply = Encoding.ASCII.GetBytes("Heartbeat Reply");
                        Console.WriteLine("Mensagem enviada para o endereço {0}", ip);
                        sr.s.SendTo(bytesendreply, sr.ep);
                    }
                }
                catch (SocketException e)
                {
                    Console.WriteLine(e.Message);
                }
                Thread.Sleep(500);
            }
        }
Exemple #3
0
        static void Main(string[] args)
        {
            ReceiverSender rs = new ReceiverSender();

            List <string> ips = new List <string>();

            ips.Add("172.18.0.81");
            ips.Add("172.18.0.99");
            ips.Add("172.18.3.68");
            ips.Add("172.18.3.72");
            ips.Add("172.18.3.69");
            ips.Add("172.18.3.129");
            ips.Add("172.18.3.124");
            ips.Add("172.18.3.97");
            ips.Add("172.18.0.108");
            ips.Add("172.18.3.137");
            ips.Add("172.18.3.115");
            ips.Add("172.18.0.83");
            ips.Add("172.18.0.84");

            foreach (string ip in ips)
            {
                Thread s = new Thread(() => SendMessage(ip));
                Thread r = new Thread(() => ReceiveMessage(ip, rs));

                s.Start();
                r.Start();
            }
        }
        static void ReceiveMessage(ReceiverSender receiverSender)
        {
            byte[] bytebufreceive;
            string message = "";

            while (true)
            {
                try
                {
                    bytebufreceive = receiverSender.listener.Receive(ref receiverSender.groupEP);
                    message        = Encoding.ASCII.GetString(bytebufreceive, 0, bytebufreceive.Length);
                    if (message.ToLower().Trim() == "heartbeat request")
                    {
                        ResponseRequest(new SenderReceiver(receiverSender.groupEP.Address.ToString()), Encoding.ASCII.GetBytes("Heartbeat Reply"));
                    }
                    else if (message.ToLower().Trim() == "heartbeat reply")
                    {
                        if (!alive.Contains(receiverSender.groupEP.Address.ToString()))
                        {
                            AddAlives(receiverSender.groupEP.Address.ToString());
                        }
                    }
                    verifyLeader();
                    Console.WriteLine("Lider: {0} | IP: {1}", lider, liderIP);
                }
                catch (SocketException error)
                {
                    Console.WriteLine(error.Message);
                }
            }
        }
        static void NovaThread(string ip)
        {
            SenderReceiver sr = new SenderReceiver("172.18.3.83");
            ReceiverSender rs = new ReceiverSender();

            byte[] sendbuf, bytebufreceive;

            while (true)
            {
                Thread.Sleep(500);
                sendbuf = Encoding.ASCII.GetBytes("HeartBeat Request");

                sr.s.SendTo(sendbuf, sr.ep);

                Console.WriteLine("Mensagem enviada para o endereço...");


                try
                {
                    Console.WriteLine("Esperando por mensagens...");
                    bytebufreceive = rs.listener.Receive(ref rs.groupEP);

                    Console.WriteLine("Mensagem recebida:");
                    Console.WriteLine(Encoding.ASCII.GetString(bytebufreceive, 0, bytebufreceive.Length));
                }
                catch (SocketException e)
                {
                    Console.WriteLine(e.Message);
                }
                Thread.Sleep(500);
            }
        }
        static void ReceiveMessage(List <string> ips, ReceiverSender rs)
        {
            SenderReceiver sr;

            byte[] bytebufreceive, bytesendreply;
            string message = "";

            while (true)
            {
                try
                {
                    bytebufreceive = rs.listener.Receive(ref rs.groupEP);
                    message        = Encoding.ASCII.GetString(bytebufreceive, 0, bytebufreceive.Length);

                    //Console.WriteLine("Mensagem recebida do IP({0}): {1}", rs.groupEP.Address, message);

                    if (message.ToLower().Trim() == "heartbeat request")
                    {
                        bytesendreply = Encoding.ASCII.GetBytes("HeartBeat Reply");
                        // Console.WriteLine("Mensagem enviada para o endereço {0}", rs.groupEP.Address);
                        sr = new SenderReceiver(rs.groupEP.Address.ToString());
                        sr.s.SendTo(bytesendreply, sr.ep);

                        if (!ips.Contains(rs.groupEP.Address.ToString()))
                        {
                            ips.Add(rs.groupEP.Address.ToString());
                            ipsDict.Add(rs.groupEP.Address.ToString(), ipsDict.Count);
                            Thread s = new Thread(() => SendMessage(rs.groupEP.Address.ToString()));
                            s.Start();
                        }
                    }

                    if (!alive.Contains(rs.groupEP.Address.ToString()))
                    {
                        alive.Add(rs.groupEP.Address.ToString());

                        foreach (string ip in alive)
                        {
                            int position = ipsDict[ip];

                            if (position < lider)
                            {
                                lider   = position;
                                liderIP = ip;
                            }
                        }

                        Console.WriteLine("Lider: {0} | IP: {1}", lider, liderIP);
                    }

                    Thread.Sleep(1000);
                }
                catch (SocketException e)
                {
                    Console.WriteLine(e.Message);
                }
            }
        }
        static void Main(string[] args)
        {
            ReceiverSender rs = new ReceiverSender();

            List <string> ips = new List <string>();

            ips.Add("172.18.0.1");

            foreach (string ip in ips)
            {
                Thread s = new Thread(() => SendMessage(ip));
                Thread r = new Thread(() => ReceiveMessage(ips, rs));

                s.Start();
                r.Start();
            }
        }
        static void Main(string[] args)
        {
            ips.Add("172.18.3.40");
            ips.Add("172.18.0.99");
            ips.Add("172.18.0.106");
            ips.Add("172.18.0.176");
            ips.Add("172.18.0.107");
            ips.Add("172.18.3.137");
            ips.Add("172.18.0.108");
            ips.Add("172.18.0.86");
            ips.Add("172.18.0.81");
            ips.Add("172.18.3.25");
            ips.Add("172.18.3.66");
            ips.Add("172.18.3.60");
            ips.Add("172.18.0.83");
            ips.Add("172.18.3.62");
            ips.Add("172.18.3.33");
            ips.Add("172.18.3.69");
            ips.Add("172.18.3.71");
            ips.Add("172.18.3.67");

            ReceiverSender rs = new ReceiverSender();

            for (int i = 0; i < ips.Count; i++)
            {
                ipsDict.Add(ips[i], i);
            }

            foreach (string ip in ips)
            {
                Thread s = new Thread(() => SendMessage(ip));
                Thread r = new Thread(() => ReceiveMessage(ips, rs));

                s.Start();
                r.Start();
            }
        }