opcuaServerACPLT.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <memory.h>
  4. #include "ua_types.h"
  5. #include "ua_transport.h"
  6. #include "ua_transport_binary.h"
  7. #include "networklayer.h"
  8. #ifdef LINUX
  9. #include <sys/types.h>
  10. #include <sys/socket.h>
  11. #include <netinet/in.h>
  12. #include <sys/socketvar.h>
  13. #include <unistd.h>
  14. void server_run();
  15. #endif
  16. #define PORT 16664
  17. #define MAXMSG 512
  18. #define BUFFER_SIZE 8192
  19. int main(void) {
  20. #ifdef LINUX
  21. printf("Starting open62541 demo server on port %d\n", PORT);
  22. server_run();
  23. #endif
  24. return EXIT_SUCCESS;
  25. }
  26. #ifdef LINUX
  27. void tmpTestFunction()
  28. {
  29. }
  30. void server_run() {
  31. //just for debugging
  32. #ifdef DEBUG
  33. tmpTestFunction();
  34. #endif
  35. TL_Connection connection;
  36. connection.connectionState = CONNECTIONSTATE_CLOSED;
  37. connection.writerCallback = (TL_Writer) NL_TCP_writer;
  38. connection.localConf.maxChunkCount = 1;
  39. connection.localConf.maxMessageSize = BUFFER_SIZE;
  40. connection.localConf.protocolVersion = 0;
  41. connection.localConf.recvBufferSize = BUFFER_SIZE;
  42. connection.localConf.recvBufferSize = BUFFER_SIZE;
  43. UA_ByteString slMessage = { -1, UA_NULL };
  44. int 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. /* First call to socket() function */
  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. memset((void *) &serv_addr, 0, 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) == -1) {
  62. perror("setsockopt");
  63. exit(1);
  64. }
  65. /* Now bind the host address using bind() call.*/
  66. if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
  67. perror("ERROR on binding");
  68. exit(1);
  69. }
  70. /* Now start listening for the clients, here process will
  71. * go in sleep mode and will wait for the incoming connection
  72. */
  73. while (listen(sockfd, 5) != -1) {
  74. clilen = sizeof(cli_addr);
  75. /* Accept actual connection from the client */
  76. newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, (socklen_t*) &clilen);
  77. if (newsockfd < 0) {
  78. perror("ERROR on accept");
  79. exit(1);
  80. }
  81. printf("server_run - connection accepted: %i, state: %i\n", newsockfd, connection.connectionState);
  82. connection.connectionHandle = newsockfd;
  83. do {
  84. memset(buffer, 0, BUFFER_SIZE);
  85. n = read(newsockfd, buffer, BUFFER_SIZE);
  86. if (n > 0) {
  87. slMessage.data = (UA_Byte*) buffer;
  88. slMessage.length = n;
  89. #ifdef DEBUG
  90. UA_ByteString_printx("server_run - received=",&slMessage);
  91. #endif
  92. TL_Process(&connection, &slMessage);
  93. } else if (n <= 0) {
  94. perror("ERROR reading from socket1");
  95. exit(1);
  96. }
  97. } while(connection.connectionState != CONNECTIONSTATE_CLOSE);
  98. shutdown(newsockfd,2);
  99. close(newsockfd);
  100. connection.connectionState = CONNECTIONSTATE_CLOSED;
  101. }
  102. shutdown(sockfd,2);
  103. close(sockfd);
  104. }
  105. #endif