opcuaServer.c 3.1 KB

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