/// <summary> /// Establishes the connection to the remote server; initializes this <see cref="_vncConnection"/>'s properties from /// <see cref="BaseConnectionForm{T}.Connection"/> and then calls <see cref="VncClient.Connect(string, int, VncClientConnectOptions)"/> on /// <see cref="_vncConnection"/>. /// </summary> public override void Connect() { OnResize(null); ParentForm.Closing += VncConnectionForm_FormClosing; VncClientConnectOptions connectionOptions = new VncClientConnectOptions() { PasswordRequiredCallback = GetPassword }; // Establish the actual connection _vncConnection.Connected += OnConnected; _vncConnection.ConnectionFailed += OnConnectionLost; _vncConnection.AllowInput = !Connection.ViewOnly; _vncConnection.AllowRemoteCursor = false; _vncConnection.AllowClipboardSharingFromServer = Connection.ShareClipboard; _vncConnection.AllowClipboardSharingToServer = Connection.ShareClipboard; // Spin the connection process up on a different thread to avoid blocking the UI. Thread connectionThread = new Thread( () => { try { _vncConnection.Client.Connect(Connection.Host, Connection.Port + Connection.Display, connectionOptions); } catch (Exception e) { Invoke(new Action(() => OnConnectionLost(this, new ErrorEventArgs(e)))); } }); connectionThread.Start(); }
private void btnConnect_Click(object sender, EventArgs e) { if (vncControl.Client.IsConnected) { vncControl.Client.Close(); } else { var hostname = txtHostname.Text.Trim(); if (hostname == "") { MessageBox.Show(this, "Hostname isn't set.", "Hostname", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } int port; if (!int.TryParse(txtPort.Text, out port) || port < 1 || port > 65535) { MessageBox.Show(this, "Port must be between 1 and 65535.", "Port", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } var options = new VncClientConnectOptions(); if (txtPassword.Text != "") { options.Password = txtPassword.Text.ToCharArray(); } try { try { Cursor = Cursors.WaitCursor; try { vncControl.Client.Connect(hostname, port, options); } finally { Cursor = Cursors.Default; } } catch (VncException ex) { MessageBox.Show(this, "Connection failed (" + ex.Reason.ToString() + "," + ex.Message + ").", "Connect", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } catch (SocketException ex) { MessageBox.Show(this, "Connection failed (" + ex.SocketErrorCode.ToString() + ").", "Connect", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } vncControl.Focus(); } finally { if (options.Password != null) { Array.Clear(options.Password, 0, options.Password.Length); } } } }