opcuaServer.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. #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. 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. puts("starting demo Server");
  38. //call listen
  39. }
  40. void server_run()
  41. {
  42. UA_connection connection;
  43. UA_ByteString slMessage;
  44. TL_initConnectionObject(&connection);
  45. char optval;
  46. int sockfd, newsockfd, portno, clilen;
  47. char buffer[8192];
  48. struct sockaddr_in serv_addr, cli_addr;
  49. int n;
  50. /* First call to socket() function */
  51. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  52. if (sockfd < 0)
  53. {
  54. perror("ERROR opening socket");
  55. exit(1);
  56. }
  57. /* Initialize socket structure */
  58. bzero((char *) &serv_addr, sizeof(serv_addr));
  59. portno = 16664;
  60. serv_addr.sin_family = AF_INET;
  61. serv_addr.sin_addr.s_addr = INADDR_ANY;
  62. serv_addr.sin_port = htons(portno);
  63. optval = 1;
  64. if (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&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,8192);
  93. n = read( newsockfd,buffer,8192);
  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. opcua_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