opcuaServer.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. {
  28. #ifdef LINUX
  29. server_init();
  30. server_run();
  31. #endif
  32. return EXIT_SUCCESS;
  33. }
  34. #ifdef LINUX
  35. void server_init()
  36. {
  37. printf("Starting open62541 demo server on port %d\n", PORT);
  38. //call listen
  39. }
  40. void server_run()
  41. {
  42. UA_connection connection;
  43. UA_ByteString slMessage = {NULL,0};
  44. char optval = 1;
  45. int sockfd, newsockfd, portno, clilen;
  46. char buffer[BUFFER_SIZE];
  47. struct sockaddr_in serv_addr, cli_addr;
  48. int n;
  49. TL_initConnectionObject(&connection);
  50. SL_initConnectionObject(&connection);
  51. /* First call to socket() function */
  52. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  53. if (sockfd < 0)
  54. {
  55. perror("ERROR opening socket");
  56. exit(1);
  57. }
  58. /* Initialize socket structure */
  59. bzero((void *) &serv_addr, sizeof(serv_addr));
  60. portno = PORT;
  61. serv_addr.sin_family = AF_INET;
  62. serv_addr.sin_addr.s_addr = INADDR_ANY;
  63. serv_addr.sin_port = htons(portno);
  64. if (setsockopt(sockfd,SOL_SOCKET,(SO_REUSEADDR),(char*)&optval,sizeof(int)) == -1) {
  65. perror("setsockopt");
  66. exit(1);
  67. }
  68. /* Now bind the host address using bind() call.*/
  69. if (bind(sockfd, (struct sockaddr *) &serv_addr,
  70. sizeof(serv_addr)) < 0)
  71. {
  72. perror("ERROR on binding");
  73. exit(1);
  74. }
  75. /* Now start listening for the clients, here process will
  76. * go in sleep mode and will wait for the incoming connection
  77. */
  78. listen(sockfd,5);
  79. clilen = sizeof(cli_addr);
  80. /* Accept actual connection from the client */
  81. newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr,
  82. &clilen);
  83. if (newsockfd < 0)
  84. {
  85. perror("ERROR on accept");
  86. exit(1);
  87. }
  88. printf("One connection accepted");
  89. while(1)
  90. {
  91. /* If connection is established then start communicating */
  92. bzero(buffer,BUFFER_SIZE);
  93. n = read( newsockfd,buffer,BUFFER_SIZE);
  94. if (n > 0)
  95. {
  96. printf("received: %s\n",buffer);
  97. connection.readData.data = buffer;
  98. connection.readData.length = n;
  99. connection.newDataToRead = 1;
  100. //TL_receive(&connection, &slMessage);
  101. SL_receive(&connection, &slMessage);
  102. }
  103. else if (n < 0)
  104. {
  105. perror("ERROR reading from socket1");
  106. exit(1);
  107. }
  108. if(connection.newDataToWrite)
  109. {
  110. printf("data will be sent \n");
  111. n = write(newsockfd,connection.writeData.data,connection.writeData.length);
  112. printf("sent data \n");
  113. connection.newDataToWrite = 0;
  114. UA_free(connection.writeData.data);
  115. connection.writeData.data = NULL;
  116. connection.writeData.length = 0;
  117. }
  118. connection.readData.data = NULL;
  119. connection.readData.length = 0;
  120. connection.newDataToRead = 0;
  121. }
  122. }
  123. #endif