Skip to main content
This page covers the functions that drive the connection lifecycle of a FreeRDP client: establishing the session, pumping the event loop, disconnecting, and interpreting errors.

Overview

Connecting

Performs the full RDP connection sequence:
  1. Calls instance->PreConnect (if set).
  2. Establishes the TCP connection, negotiates security (TLS/NLA), and completes the RDP handshake.
  3. Calls instance->PostConnect on success.
Returns TRUE on success, FALSE on failure. On failure, call freerdp_get_last_error() for a detailed error code.
freerdp_connect() blocks until the connection sequence completes or fails. Run it in a dedicated thread (as shown in the sample client) to keep your UI responsive.

Disconnecting

freerdp_abort_connect_context() is safe to call from any thread. It signals the connection to shut down; the event loop should detect this and exit, after which freerdp_disconnect() cleans up.

Reconnecting

To reconnect after a dropped session:
  1. Call freerdp_disconnect_before_reconnect_context() to cleanly prepare the instance for reconnection without fully tearing down all state.
  2. Call freerdp_reconnect() which re-runs the connection sequence.
The client-common helper client_auto_reconnect_ex() wraps this pattern with automatic retry logic:

Event Loop

FreeRDP is event-driven. After a successful freerdp_connect(), your thread must call these two functions in a loop:
freerdp_get_event_handles() returns the number of handles written, or 0 on error. freerdp_check_event_handles() returns FALSE if an unrecoverable error occurred; check freerdp_get_last_error() to distinguish a protocol-level disconnect from a real error.

Canonical Event Loop

Error Handling

Context-level error

Error codes are composed from a class and a type:

Connection Error Codes (FREERDP_ERROR_CONNECT_*)

These are the most common codes returned by freerdp_get_last_error() after a failed freerdp_connect():

Server-sent Error Info (freerdp_error_info())

The server can push an Error Info PDU to explain why it is disconnecting:
Common ERRINFO_* codes:

Connection State

Disconnect Reason

After a disconnect you can retrieve the MCS-level ultimatum reason:
Possible values are defined by enum Disconnect_Ultimatum: domain_disconnected, provider_initiated, token_purged, user_requested, channel_purged.

Complete Example

This example mirrors the structure of the FreeRDP sample client (client/Sample/tf_freerdp.c):