opcuaServer.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. ============================================================================
  3. Name : opcuaServer.c
  4. Author :
  5. Version :
  6. Copyright : Your copyright notice
  7. Description :
  8. ============================================================================
  9. */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <memory.h> // bzero
  13. #include "opcua.h"
  14. #include "opcua_transportLayer.h"
  15. #ifdef LINUX
  16. #include <sys/types.h>
  17. #include <sys/socket.h>
  18. #include <netinet/in.h>
  19. #include <sys/socketvar.h>
  20. void server_init();
  21. void server_run();
  22. #endif
  23. #define PORT 16664
  24. #define MAXMSG 512
  25. #define BUFFER_SIZE 8192
  26. int main(void) {
  27. #ifdef LINUX
  28. server_init();
  29. server_run();
  30. #endif
  31. return EXIT_SUCCESS;
  32. }
  33. #ifdef LINUX
  34. void server_init() {
  35. printf("Starting open62541 demo server on port %d\n", PORT);
  36. //call listen
  37. }
  38. void server_run() {
  39. UA_connection connection;
  40. UA_ByteString slMessage = { -1, UA_NULL };
  41. int optval = 1;
  42. int sockfd, newsockfd, portno, clilen;
  43. char buffer[BUFFER_SIZE];
  44. struct sockaddr_in serv_addr, cli_addr;
  45. int n;
  46. TL_initConnectionObject(&connection);
  47. SL_initConnectionObject(&connection);
  48. /* First call to socket() function */
  49. // sockfd = socket(AF_INET, SOCK_STREAM, 0);
  50. sockfd = socket(PF_INET, SOCK_STREAM, 0);
  51. if (sockfd < 0) {
  52. perror("ERROR opening socket");
  53. exit(1);
  54. }
  55. /* Initialize socket structure */
  56. bzero((void *) &serv_addr, sizeof(serv_addr));
  57. portno = PORT;
  58. serv_addr.sin_family = AF_INET;
  59. serv_addr.sin_addr.s_addr = INADDR_ANY;
  60. serv_addr.sin_port = htons(portno);
  61. if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof optval)
  62. == -1) {
  63. perror("setsockopt");
  64. exit(1);
  65. }
  66. /* Now bind the host address using bind() call.*/
  67. if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
  68. perror("ERROR on binding");
  69. exit(1);
  70. }
  71. /* Now start listening for the clients, here process will
  72. * go in sleep mode and will wait for the incoming connection
  73. */
  74. listen(sockfd, 5);
  75. clilen = sizeof(cli_addr);
  76. /* Accept actual connection from the client */
  77. while (1) {
  78. newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
  79. if (newsockfd < 0) {
  80. perror("ERROR on accept");
  81. exit(1);
  82. }
  83. printf("One connection accepted\n");
  84. while (connection.transportLayer.connectionState != connectionState_CLOSE) {
  85. /* If connection is established then start communicating */
  86. bzero(buffer, BUFFER_SIZE);
  87. n = read(newsockfd, buffer, BUFFER_SIZE);
  88. if (n > 0) {
  89. connection.readData.data = buffer;
  90. connection.readData.length = n;
  91. connection.newDataToRead = 1;
  92. UA_ByteString_printx("server_run - received=",
  93. &connection.readData);
  94. //TL_receive(&connection, &slMessage);
  95. SL_receive(&connection, &slMessage);
  96. } else if (n < 0) {
  97. perror("ERROR reading from socket1");
  98. exit(1);
  99. }
  100. if (connection.newDataToWrite) {
  101. UA_ByteString_printx("Send data:", &connection.writeData);
  102. n = write(newsockfd, connection.writeData.data,
  103. connection.writeData.length);
  104. printf("written %d bytes \n", n);
  105. connection.newDataToWrite = 0;
  106. UA_ByteString_deleteMembers(&connection.writeData);
  107. connection.writeData.data = NULL;
  108. connection.writeData.length = 0;
  109. }
  110. connection.readData.data = NULL;
  111. connection.readData.length = 0;
  112. connection.newDataToRead = 0;
  113. }
  114. close(newsockfd);
  115. connection.transportLayer.connectionState = connectionState_CLOSED;
  116. }
  117. }
  118. #endif