ACR_ServerCommunicator.GameServer.SetHostnameAndPort C# (CSharp) Method

SetHostnameAndPort() public method

Set the hostname and port based on parsing an address string, which may be either 'hostname' or 'hostname:port'. The default port of 5121 is used if no port was set.
public SetHostnameAndPort ( string AddressString ) : void
AddressString string Supplies the address string to set the /// server network address information from.
return void
        public void SetHostnameAndPort(string AddressString)
        {
            string OldHostname = ServerHostname;
            int i = AddressString.IndexOf(':');

            if (i == -1)
            {
                ServerHostname = AddressString;
                ServerPort = 5121;
                return;
            }

            ServerHostname = AddressString.Substring(0, i);
            ServerPort = Convert.ToInt32(AddressString.Substring(i + 1));

            if (OldHostname == null || OldHostname != ServerHostname)
            {
                IPAddress Address;

                if (IPAddress.TryParse(ServerHostname, out Address))
                {
                    ServerIPAddress = Address;
                }
                else
                {
                    try
                    {
                        IPHostEntry Entry = Dns.GetHostEntry(ServerHostname);

                        if (Entry.AddressList != null && Entry.AddressList.Length > 0)
                            ServerIPAddress = Entry.AddressList[0];
                    }
                    catch
                    {
                        ServerIPAddress = IPAddress.None;
                    }
                }
            }
        }

Usage Example

        /// <summary>
        /// Reference the data for a server by the server name.  If the data
        /// was not yet available, it is retrieved from the database.
        /// </summary>
        /// <param name="ServerName">Supplies the object name.</param>
        /// <param name="Database">Supplies the database connection to use for
        /// queries, if required.  The active rowset may be consumed.</param>
        /// <returns>The object data is returned, else null if the object did
        /// not exist.</returns>
        public GameServer ReferenceServerByName(string ServerName, IALFADatabase Database)
        {
            //
            // Check if the object is already known first.
            //

            GameServer Server = (from S in Servers
                                 where S.ServerName.Equals(ServerName, StringComparison.InvariantCultureIgnoreCase)
                                 select S).FirstOrDefault();

            if (Server != null)
                return Server;

            //
            // Need to pull the data from the database.
            //

            if (Database == null)
                return null;

            Database.ACR_SQLQuery(String.Format(
                "SELECT `ID`, `IPAddress`, `Name` FROM `servers` WHERE `Name` = '{0}'",
                Database.ACR_SQLEncodeSpecialChars(ServerName)));

            if (!Database.ACR_SQLFetch())
                return null;

            Server = new GameServer(this);

            Server.ServerId = Convert.ToInt32(Database.ACR_SQLGetData(0));
            Server.SetHostnameAndPort(Database.ACR_SQLGetData(1));
            Server.ServerName = Database.ACR_SQLGetData(2);

            InsertNewServer(Server, Database);

            return Server;
        }
All Usage Examples Of ACR_ServerCommunicator.GameServer::SetHostnameAndPort