예제 #1
0
 public void Disconnect()
 {
     if (!IsConnected)
     {
         return;
     }
     m_client.Disconnect();
     m_client     = null;
     m_isLoggedIn = false;
 }
예제 #2
0
        /// <summary>
        /// Disconnect from current server
        /// </summary>
        public void Disconnect()
        {
            if (!IsConnected)
            {
                return;
            }

            IsConnected = false;
            m_client.Disconnect();
            m_client = null;
        }
예제 #3
0
 public void TryConnect()
 {
     clientHandler.hostName = stringToEdit;
     clientHandler.port     = Int32.Parse(port);
     if (clientHandler.isConnected())
     {
         clientHandler.Disconnect();
     }
     else
     {
         clientHandler.ConnectToServer();
         //gameObject.SetActive(false);
     }
 }
예제 #4
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);
        }