Beispiel #1
0
        public async Task <bool> ConnectAsync()
        {
            if (m_ip == null)
            {
                throw new InvalidOperationException("Can not connect to a server if no Ip address is specified");
            }
            if (IsConnected)
            {
                throw new InvalidOperationException("Le client est déjà connecté");
            }
            m_client = new TcpClientHandler(m_ip, m_port);

            bool isSuccessful = await m_client.ConnectAsync();

            if (isSuccessful)
            {
                m_isConnected          = true;
                m_client.Disconnected += m_client_Disconnected;
                m_client.ReceivedFull += m_client_ReceivedFull;
                return(true);
            }

            m_isConnected = false;
            m_client      = null;
            return(false);
        }
Beispiel #2
0
        public bool FindServer(int port)
        {
            foreach (NetworkInterface netwIntrf in NetworkInterface.GetAllNetworkInterfaces())
            {
                //if the current interface doesn't have an IP, skip it
                if (!(netwIntrf.GetIPProperties().GatewayAddresses.Count > 0))
                {
                    continue;
                }

                //get current IP Address(es)
                foreach (UnicastIPAddressInformation uniIpInfo in netwIntrf.GetIPProperties().UnicastAddresses)
                {
                    if (uniIpInfo.Address.IsIPv6LinkLocal || uniIpInfo.IPv4Mask == IPAddress.Any)
                    {
                        continue;
                    }

                    //get the subnet mask and the IP address as bytes
                    byte[] subnetMask = uniIpInfo.IPv4Mask.GetAddressBytes();
                    byte[] ipAddr     = uniIpInfo.Address.GetAddressBytes();

                    // we reverse the byte-array if we are dealing with littl endian.
                    if (BitConverter.IsLittleEndian)
                    {
                        Array.Reverse(subnetMask);
                        Array.Reverse(ipAddr);
                    }

                    uint maskAsInt = BitConverter.ToUInt32(subnetMask, 0);
                    uint ipAsInt   = BitConverter.ToUInt32(ipAddr, 0);

                    //we negate the subnet to determine the maximum number of host possible in this subnet
                    uint validHostsEndingMax = ~BitConverter.ToUInt32(subnetMask, 0);

                    uint validHostsStart = BitConverter.ToUInt32(ipAddr, 0) & BitConverter.ToUInt32(subnetMask, 0);

                    //we increment the startIp to the number of maximum valid hosts in this subnet and for each we check the intended port (refactoring needed)
                    for (uint i = 1; i <= validHostsEndingMax; i++)
                    {
                        uint   host      = validHostsStart + i;
                        byte[] hostBytes = BitConverter.GetBytes(host);
                        if (BitConverter.IsLittleEndian)
                        {
                            Array.Reverse(hostBytes);
                        }

                        //this is the candidate IP address in "readable format"
                        String ipCandidate = Convert.ToString(hostBytes[0]) + "." + Convert.ToString(hostBytes[1]) + "." + Convert.ToString(hostBytes[2]) + "." + Convert.ToString(hostBytes[3]);

                        this.Ip = new IPAddress(hostBytes);

                        m_client = new TcpClientHandler(m_ip, m_port);

                        IAsyncResult result  = m_client.ConnectAsync();
                        bool         success = result.AsyncWaitHandle.WaitOne(20, true);

                        if (success && m_client.InfoHandler.Client.Connected)
                        {
                            m_client.Disconnected += m_client_Disconnected;
                            m_client.ReceivedFull += m_client_ReceivedFull;
                            m_isConnected          = true;
                            return(true);
                        }
                        else
                        {
                            m_client.Disconnect();
                            m_client      = null;
                            m_isConnected = false;
                        }
                    }
                }
            }
            Console.WriteLine("No server found");
            return(false);
        }