Building a Simple Client

You should already have a basic server from the previous tutorials. open62541 provides both a server- and clientside API, so creating a client is as easy as creating a server. Copy the following into a file myClient.c:

#include <stdio.h>
#include "open62541.h"

int main(void) {
    UA_Client *client = UA_Client_new(UA_ClientConfig_standard);
    UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
    if(retval != UA_STATUSCODE_GOOD) {
        return (int)retval;

    /* Read the value attribute of the node. UA_Client_readValueAttribute is a
     * wrapper for the raw read service available as UA_Client_Service_read. */
    UA_Variant value; /* Variants can hold scalar values and arrays of any type */

    /* NodeId of the variable holding the current time */
    const UA_NodeId nodeId =

    retval = UA_Client_readValueAttribute(client, nodeId, &value);
    if(retval == UA_STATUSCODE_GOOD &&
       UA_Variant_hasScalarType(&value, &UA_TYPES[UA_TYPES_DATETIME])) {
        UA_DateTime raw_date = *(UA_DateTime*);
        UA_String string_date = UA_DateTime_toString(raw_date);
        printf("string date is: %.*s\n", (int)string_date.length,;

    /* Clean up */
    UA_Client_delete(client); /* Disconnects the client internally */

Compilation is similar to the server example.

$ gcc -std=c99 open6251.c myClient.c -o myClient

Further tasks

  • Try to connect to some other OPC UA server by changing opc.tcp://localhost:16664 to an appropriate address (remember that the queried node is contained in any OPC UA server).
  • Try to set the value of the variable node (ns=1,i=”the.answer”) containing an Int32 from the example server (which is built in Building a Simple Server) using “UA_Client_write” function. The example server needs some more modifications, i.e., changing request types. The answer can be found in “examples/exampleClient.c”.