Пример #1
0
    protected void OnServerNameEntryActivated(object sender, EventArgs e)
    {
        IPAddress hostaddr;

        // Resolve the server IP address
        try
        {
            hostaddr = Dns.GetHostAddresses(ServerName.Text)[0];
        }
        catch
        {
            MessageBox.Show("ERROR: Cannot resolve " + ServerName.Text);
            clnt = null;
            return;
        }

        // Open connection to the server
        try
        {
            clnt = new missileClient(hostaddr, OncRpcProtocols.ONCRPC_UDP);
        }
        catch
        {
            MessageBox.Show("ERROR: Connection to " + ServerName.Text + " failed");
            clnt = null;
            return;
        }

        // Send NOP command to the server, to test the connection
        try
        {
            int status = clnt.missile_command_1(missile.CMD_NOP);

            if (status != 0)
            {
                clnt = null;
                MessageBox.Show("ERROR: RPC call returned error " + status.ToString());
                return;
            }
        }
        catch
        {
            clnt = null;
            MessageBox.Show("ERROR: RPC call to " + ServerName.Text + " failed");
            return;
        }

        // Disable further changes to the server name field
        ServerName.IsEditable = false;

        // Enable buttons now that the connection to the server is working
        foreach (var b in CommandButtons.Keys)
        {
            b.Sensitive = true;
        }

        // Save the server name in the registry
        Registry.SetValue(RegistryKey, ServerName.Name, ServerName.Text);
    }