Esempio n. 1
0
        public static void disconnect()
        {
            var data = Encoding.UTF8.GetBytes("disconnected" + ";" + Environment.MachineName);

            Console.WriteLine("[send] => " + "disconnected" + ";" + Environment.MachineName);
            foreach (var ipAndMask in InterfaceFinder.Find())
            {
                udpClient.Send(data, data.Length, IPCalculator.CalculateBCAddress(ipAndMask[0], ipAndMask[1]).ToString(), PORT);
            }
        }
        /// <summary>
        /// Find the ip which let the 2 pcs communicate to each other
        /// </summary>
        /// <param name="ipCsv">A CSV file that contain the state of the machine, his name and his ips and masks</param>
        /// <returns></returns>
        private static string FindMatchingNetwork(string ipCsv)
        {
            List <List <IPAddress> > ipsAndMasks              = InterfaceFinder.Find();
            List <IPAddress>         NetAddresses             = new List <IPAddress>();
            List <IPAddress>         otherMachineNetAddresses = new List <IPAddress>();

            for (int i = 0; i < ipsAndMasks.Count; i++)
            {
                NetAddresses.Add(IPCalculator.NetworkCalculator(ipsAndMasks[i][0], ipsAndMasks[i][1]));
            }
            string[]  data         = ipCsv.Split(';');
            string    otherPcName  = data[1];
            bool      findNetIp    = false;
            IPAddress foundedNetIp = IPAddress.Any;

            for (int i = 2; i < data.Length; i += 2)
            {
                IPAddress NetIp = IPCalculator.NetworkCalculator(IPAddress.Parse(data[i]), IPAddress.Parse(data[i + 1]));
                for (int j = 0; j < NetAddresses.Count; j++)
                {
                    if (NetIp.Equals(NetAddresses[j]))
                    {
                        findNetIp    = true;
                        foundedNetIp = IPAddress.Parse(data[i]);
                        break;
                    }
                    if (findNetIp)
                    {
                        break;
                    }
                }
            }

            bool write = true;

            for (int j = 0; j < allPcName.Count; j++)
            {
                if (allPcName[j] == otherPcName)
                {
                    //Console.WriteLine(allPcName[j] + " == " + otherPcName + " | " + allIPOfTransfer[j] + " == " + foundedNetIp);
                    write = false;
                    break;
                }
            }
            if (write)
            {
                Console.WriteLine("pc name: " + otherPcName);
                allPcName.Add(otherPcName);
                Console.WriteLine("ip: " + foundedNetIp);
                allIPOfTransfer.Add(foundedNetIp);
                mainForm.ShowPcInvoke(new Machine(otherPcName, foundedNetIp, mainForm));
            }
            return(foundedNetIp.ToString());
        }
Esempio n. 3
0
        public static void send(UdpClient udpClientLocal, int PORTLocal, IPEndPoint fromLocal, Form1 mainFormRef)
        {
            udpClient = udpClientLocal;
            PORT      = PORTLocal;
            from      = fromLocal;
            mainForm  = mainFormRef;
            IPAddress[] allNetworkIp = GetAllNetworkIPOfPc();
            string      pcName       = System.Environment.MachineName;

            IPAddress[] myIps            = InterfaceFinder.FindIPAddress().ToArray();
            IPAddress[] subnetMasks      = GetAllSubnetMask();
            string      strMessageToSend = "";

            strMessageToSend += pcName + ";";
            List <List <IPAddress> > ipsAndMasks = InterfaceFinder.Find();

            for (var i = 0; i < myIps.Length; i++)
            {
                strMessageToSend += ipsAndMasks[i][0].ToString() + ";" + ipsAndMasks[i][1].ToString();
                if (i != myIps.Length - 1)
                {
                    strMessageToSend += ";";
                }
            }
            var toSend = Encoding.UTF8.GetBytes(strMessageToSend);

            bool meAsSend = false;

            /*string pcName = Environment.MachineName;
             * string strHostName = Dns.GetHostName();
             * IPHostEntry ipEntry = Dns.GetHostEntry(strHostName);
             * string strMessageToSend = "";
             * strMessageToSend += pcName + ";";
             * for (var i = 0; i < allLocalIp.Length; i++)
             * {
             *  strMessageToSend += allLocalIp[i].ToString() + ";" + allLocalSubnet[i];
             *  if (i != myIps.Length - 1)
             *  {
             *      strMessageToSend += ";";
             *  }
             *
             * }
             * byte[] toSend = Encoding.UTF8.GetBytes(strMessageToSend);*/
            var data = Encoding.UTF8.GetBytes("connected" + ";" + Environment.MachineName);

            foreach (var ipAndMask in InterfaceFinder.Find())
            {
                udpClient.Send(data, data.Length, IPCalculator.CalculateBCAddress(ipAndMask[0], ipAndMask[1]).ToString(), PORT);
                Thread.Sleep(100);
                udpClient.Send(toSend, toSend.Length, IPCalculator.CalculateBCAddress(ipAndMask[0], ipAndMask[1]).ToString(), PORT);
            }
            Console.WriteLine("[send] => " + Encoding.UTF8.GetString(data));
            while (true)
            {
                var    recvBuffer  = udpClient.Receive(ref from);
                string recvMessage = Encoding.UTF8.GetString(recvBuffer);
                //if the programm receive info about antoher pc
                if (recvMessage.Split(';')[0] != Environment.MachineName && !(recvMessage.Split(';')[0] == "connected" || recvMessage.Split(';')[0] == "disconnected"))
                {
                    Console.WriteLine("[receive] => " + Encoding.UTF8.GetString(recvBuffer));
                    string   strResult = Encoding.UTF8.GetString(recvBuffer);
                    string[] eachIp    = strResult.Split(';');
                    if (eachIp.Length != 0)
                    {
                        IPAddress[] reiceivedNetworks = new IPAddress[(eachIp.Length - 1) / 2];
                        IPAddress[] ipAddreses        = new IPAddress[(eachIp.Length - 1) / 2];
                        string      otherPcName       = eachIp[0];
                        for (int r = 1; r < eachIp.Length; r += 2)
                        {
                            IPAddress IP         = IPAddress.Parse(eachIp[r]);
                            IPAddress subnetMask = IPAddress.Parse(eachIp[r + 1]);
                            ipAddreses[(r - 1) / 2] = IP;
                            Console.WriteLine(GetNetworkIP(IP, subnetMask));
                            reiceivedNetworks[(r - 1) / 2] = GetNetworkIP(IP, subnetMask);
                        }
                        List <int> whichIp = new List <int>();
                        int        i       = 0;
                        while (i < reiceivedNetworks.Length)
                        {
                            for (var g = 0; g < allNetworkIp.Length; g++)
                            {
                                if (reiceivedNetworks[i].Equals(allNetworkIp[g]))
                                {
                                    whichIp.Add(i);
                                    break;
                                }
                            }
                            i++;
                        }
                        Console.WriteLine(whichIp);
                        if (whichIp.Count > 0)
                        {
                            int g = 0;
                            while (g < whichIp.Count)
                            {
                                if (PingHost(ipAddreses[g].ToString()))
                                {
                                    bool write = true;
                                    for (int j = 0; j < allPcName.Count; j++)
                                    {
                                        if (allPcName[j] == otherPcName)
                                        {
                                            //Console.WriteLine(allPcName[j] + " == " + otherPcName + " | " + allIPOfTransfer[j] + " == " + ipAddreses[g]);
                                            write = false;
                                            break;
                                        }
                                    }
                                    if (write)
                                    {
                                        Console.WriteLine("pc name: " + otherPcName);
                                        allPcName.Add(otherPcName);
                                        Console.WriteLine("ip: " + ipAddreses[g]);
                                        allIPOfTransfer.Add(ipAddreses[g]);
                                        mainForm.ShowPcInvoke(new Machine(otherPcName, ipAddreses[g], mainForm));
                                    }
                                    break;
                                }
                                g++;
                            }
                        }
                    }
                }
                else
                {
                    string strPc     = recvMessage.Split(';')[1];
                    string strStatus = recvMessage.Split(';')[0];
                    if (strPc != pcName)
                    {
                        if (strStatus == "connected")
                        {
                            Console.WriteLine("[receive] => " + recvMessage);
                            Thread.Sleep(100);
                            Console.WriteLine("[send] => " + Encoding.UTF8.GetString(toSend));
                            foreach (var ipAndMask in InterfaceFinder.Find())
                            {
                                udpClient.Send(toSend, toSend.Length, IPCalculator.CalculateBCAddress(ipAndMask[0], ipAndMask[1]).ToString(), PORT);
                            }
                        }
                        else if (strStatus == "disconnected")
                        {
                            allIPOfTransfer.RemoveAt(allPcName.IndexOf(strPc));
                            allPcName.Remove(strPc);
                            mainForm.invokeDeleteMachine(strPc);
                        }
                    }
                }
            }
        }
        public static void send(UdpClient udpClientLocal, int PORTLocal, IPEndPoint fromLocal, Form1 mainFormRef)
        {
            udpClient = udpClientLocal;
            PORT      = PORTLocal;
            from      = fromLocal;
            mainForm  = mainFormRef;

            string pcName = System.Environment.MachineName;


            byte[] toSend = GetMachineAndIp("connected");

            bool meAsSend = false;

            /*string pcName = Environment.MachineName;
             * string strHostName = Dns.GetHostName();
             * IPHostEntry ipEntry = Dns.GetHostEntry(strHostName);
             * string strMessageToSend = "";
             * strMessageToSend += pcName + ";";
             * for (var i = 0; i < allLocalIp.Length; i++)
             * {
             *  strMessageToSend += allLocalIp[i].ToString() + ";" + allLocalSubnet[i];
             *  if (i != myIps.Length - 1)
             *  {
             *      strMessageToSend += ";";
             *  }
             *
             * }
             * byte[] toSend = Encoding.UTF8.GetBytes(strMessageToSend);*/
            Thread threadGetOtherPcIp = new Thread(() => ReceiveIpFromOtherMachine());

            threadGetOtherPcIp.IsBackground = true;
            threadGetOtherPcIp.Start();
            foreach (var ipAndMask in InterfaceFinder.Find())
            {
                try
                {
                    udpClient.Send(toSend, toSend.Length, IPCalculator.CalculateBCAddress(ipAndMask[0], ipAndMask[1]).ToString(), PORT);
                    Console.WriteLine("[send] => " + Encoding.UTF8.GetString(toSend) + " [throught] => broadcast (" + IPCalculator.CalculateBCAddress(ipAndMask[0], ipAndMask[1]).ToString() + ")");
                }
                catch (SocketException s)
                {
                }
            }
            while (true)
            {
                var    recvBuffer  = udpClient.Receive(ref from);
                string recvMessage = Encoding.UTF8.GetString(recvBuffer);
                //if the programm receive info about antoher pc

                string strStatus = recvMessage.Split(';')[0];
                string strPc     = recvMessage.Split(';')[1];

                if (strPc != pcName)
                {
                    Console.WriteLine("[receive] => " + recvMessage + " [throught] => broadcast");
                    if (strStatus == "connected")
                    {
                        Thread.Sleep(100);
                        string broadcastersIP = FindMatchingNetwork(recvMessage);
                        Console.WriteLine("[send] => " + Encoding.UTF8.GetString(toSend) + " [to] => " + broadcastersIP.ToString());
                        IPSender(broadcastersIP.ToString());
                    }
                    else if (strStatus == "disconnected")
                    {
                        try
                        {
                            allIPOfTransfer.RemoveAt(allPcName.IndexOf(strPc));
                            allPcName.Remove(strPc);
                            mainForm.invokeDeleteMachine(strPc);
                        }catch
                        {
                        }
                    }
                }
            }
        }