server_firststeps.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /* This work is licensed under a Creative Commons CCZero 1.0 Universal License.
  2. * See http://creativecommons.org/publicdomain/zero/1.0/ for more information. */
  3. /**
  4. * Building a simple server
  5. * ------------------------
  6. *
  7. * This series of tutorial guide you through your first steps with open62541.
  8. * For compiling the examples, you need a compiler (MS Visual Studio 2015 or
  9. * newer, GCC, Clang and MinGW32 are all known to be working). The compilation
  10. * instructions are given for GCC but should be straightforward to adapt.
  11. *
  12. * It will also be very helpful to install an OPC UA Client with a graphical
  13. * frontend, such as UAExpert by Unified Automation. That will enable you to
  14. * examine the information model of any OPC UA server.
  15. *
  16. * To get started, downdload the open62541 single-file release from
  17. * http://open62541.org or generate it according to the :ref:`build instructions
  18. * <building>` with the "amalgamation" option enabled. From now on, we assume
  19. * you have the ``open62541.c/.h`` files in the current folder.
  20. *
  21. * Now create a new C source-file called ``myServer.c`` with the following content: */
  22. #include <signal.h>
  23. #include "open62541.h"
  24. UA_Boolean running = true;
  25. static void stopHandler(int sig) {
  26. running = false;
  27. }
  28. int main(void) {
  29. signal(SIGINT, stopHandler);
  30. signal(SIGTERM, stopHandler);
  31. UA_ServerConfig config = UA_ServerConfig_standard;
  32. UA_ServerNetworkLayer nl;
  33. nl = UA_ServerNetworkLayerTCP(UA_ConnectionConfig_standard, 4840);
  34. config.networkLayers = &nl;
  35. config.networkLayersSize = 1;
  36. UA_Server *server = UA_Server_new(config);
  37. UA_Server_run(server, &running);
  38. UA_Server_delete(server);
  39. nl.deleteMembers(&nl);
  40. return 0;
  41. }
  42. /**
  43. * This is all that is needed for a simple OPC UA server. Compile the the server
  44. * with GCC using the following command:
  45. *
  46. * .. code-block:: bash
  47. *
  48. * $ gcc -std=c99 open62541.c myServer.c -o myServer
  49. *
  50. * Now start the server (and stop with ctrl-c):
  51. *
  52. * .. code-block:: bash
  53. *
  54. * $ ./myServer
  55. *
  56. * You have now compiled and run your first OPC UA server. You can go ahead and
  57. * browse the information model with a generic client, such as UAExpert. The
  58. * server will be listening on ``opc.tcp://localhost:4840`` - go ahead and give
  59. * it a try.
  60. *
  61. * In the following tutorials, you will be shown how to populate the server's
  62. * information model and how to create a client application. */