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