client_stateless.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * C ECHO client example using sockets
  3. */
  4. #include <stdio.h> //printf
  5. #include <string.h> //strlen
  6. #include <sys/socket.h> //socket
  7. #include <arpa/inet.h> //inet_addr
  8. #include <unistd.h> // for close
  9. #include <stdlib.h> // pulls in declaration of malloc, free
  10. //this stuff can not be build with amalgamation options, since it need internal APIs
  11. #include "ua_util.h"
  12. #include "ua_transport_generated_encoding_binary.h"
  13. #include "ua_types_generated_encoding_binary.h"
  14. int main(int argc , char *argv[])
  15. {
  16. int sock;
  17. struct sockaddr_in server;
  18. UA_ByteString message;
  19. message.data = (UA_Byte*)malloc(1000*sizeof(UA_Byte));
  20. message.length = 1000;
  21. //UA_UInt32 messageEncodedLength = 0;
  22. UA_Byte server_reply[2000];
  23. unsigned int messagepos = 0;
  24. //Create socket
  25. #ifdef EXTENSION_UDP
  26. sock = socket(AF_INET , SOCK_DGRAM , 0);
  27. #else
  28. sock = socket(AF_INET , SOCK_STREAM , 0);
  29. #endif
  30. if (sock == -1)
  31. {
  32. printf("Could not create socket");
  33. }
  34. server.sin_addr.s_addr = inet_addr("127.0.0.1");
  35. server.sin_family = AF_INET;
  36. server.sin_port = htons( 16664 );
  37. //Connect to remote server
  38. if (connect(sock , (struct sockaddr *)&server , sizeof(server)) < 0)
  39. {
  40. perror("connect failed. Error");
  41. return 1;
  42. }
  43. UA_TcpMessageHeader reqTcpHeader;
  44. UA_UInt32 reqSecureChannelId = 0;
  45. UA_UInt32 reqTokenId = 0;
  46. UA_SequenceHeader reqSequenceHeader;
  47. UA_NodeId reqRequestType;
  48. UA_ReadRequest req;
  49. UA_RequestHeader reqHeader;
  50. UA_NodeId reqHeaderAuthToken;
  51. UA_ExtensionObject reqHeaderAdditionalHeader;
  52. UA_NodeId_init(&reqRequestType);
  53. reqRequestType.identifierType = UA_NODEIDTYPE_NUMERIC;
  54. reqRequestType.identifier.numeric = 631; //read request
  55. UA_SequenceHeader_init(&reqSequenceHeader);
  56. reqSequenceHeader.sequenceNumber = 42;
  57. UA_ReadRequest_init(&req);
  58. req.requestHeader = reqHeader;
  59. UA_RequestHeader_init(&(req.requestHeader));
  60. req.requestHeader.authenticationToken = reqHeaderAuthToken;
  61. UA_NodeId_init(&(req.requestHeader.authenticationToken));
  62. req.requestHeader.additionalHeader = reqHeaderAdditionalHeader;
  63. UA_ExtensionObject_init(&(req.requestHeader.additionalHeader));
  64. req.nodesToRead= UA_Array_new(&UA_TYPES[UA_TYPES_READVALUEID], 1);
  65. req.nodesToReadSize = 1;
  66. UA_ReadValueId_init(&(req.nodesToRead[0]));
  67. req.nodesToRead[0].attributeId = 13; //UA_ATTRIBUTEID_VALUE
  68. UA_NodeId_init(&(req.nodesToRead[0].nodeId));
  69. req.nodesToRead[0].nodeId.identifierType = UA_NODEIDTYPE_NUMERIC;
  70. req.nodesToRead[0].nodeId.identifier.numeric = 2255;
  71. UA_QualifiedName_init(&(req.nodesToRead[0].dataEncoding));
  72. /**messageEncodedLength = UA_TcpMessageHeader_calcSizeBinary(&reqTcpHeader) +
  73. UA_UInt32_calcSizeBinary(&reqSecureChannelId)+
  74. UA_UInt32_calcSizeBinary(&reqTokenId)+
  75. UA_SequenceHeader_calcSizeBinary(&reqSequenceHeader)+
  76. UA_NodeId_calcSizeBinary(&reqRequestType) +
  77. UA_ReadRequest_calcSizeBinary(&req);**/
  78. UA_TcpMessageHeader_init(&reqTcpHeader);
  79. reqTcpHeader.messageTypeAndFinal = UA_MESSAGETYPEANDFINAL_MSGF;
  80. UA_TcpMessageHeader_encodeBinary(&reqTcpHeader, &message, &messagepos);
  81. UA_UInt32_encodeBinary(&reqSecureChannelId, &message, &messagepos);
  82. UA_UInt32_encodeBinary(&reqTokenId, &message, &messagepos);
  83. UA_SequenceHeader_encodeBinary(&reqSequenceHeader, &message, &messagepos);
  84. UA_NodeId_encodeBinary(&reqRequestType, &message, &messagepos);
  85. UA_ReadRequest_encodeBinary(&req, &message, &messagepos);
  86. reqTcpHeader.messageSize = messagepos;
  87. messagepos=0;
  88. UA_TcpMessageHeader_encodeBinary(&reqTcpHeader, &message, &messagepos);
  89. UA_UInt32_encodeBinary(&reqSecureChannelId, &message, &messagepos);
  90. UA_UInt32_encodeBinary(&reqTokenId, &message, &messagepos);
  91. UA_SequenceHeader_encodeBinary(&reqSequenceHeader, &message, &messagepos);
  92. UA_NodeId_encodeBinary(&reqRequestType, &message, &messagepos);
  93. UA_ReadRequest_encodeBinary(&req, &message, &messagepos);
  94. //Send some data
  95. if( send(sock , message.data, messagepos , 0) < 0)
  96. {
  97. puts("Send failed");
  98. return 1;
  99. }
  100. //Receive a reply from the server
  101. int received = recv(sock , server_reply , 2000 , 0);
  102. if(received < 0)
  103. {
  104. puts("recv failed");
  105. return 1;
  106. }
  107. for(int i=0;i<received;i++){
  108. //show only printable ascii
  109. if(server_reply[i] >= 32 && server_reply[i]<= 126)
  110. printf("%c",server_reply[i]);
  111. }
  112. printf("\n");
  113. close(sock);
  114. return 0;
  115. }