client_stateless.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. int sock;
  16. struct sockaddr_in server;
  17. UA_ByteString message;
  18. message.data = (UA_Byte*)malloc(1000*sizeof(UA_Byte));
  19. message.length = 1000;
  20. //UA_UInt32 messageEncodedLength = 0;
  21. UA_Byte server_reply[2000];
  22. unsigned int messagepos = 0;
  23. //Create socket
  24. #ifdef UA_ENABLE_NONSTANDARD_UDP
  25. sock = socket(AF_INET , SOCK_DGRAM , 0);
  26. #else
  27. sock = socket(AF_INET , SOCK_STREAM , 0);
  28. #endif
  29. if(sock == -1) {
  30. printf("Could not create socket");
  31. }
  32. server.sin_addr.s_addr = inet_addr("127.0.0.1");
  33. server.sin_family = AF_INET;
  34. server.sin_port = htons( 16664 );
  35. //Connect to remote server
  36. if(connect(sock, (struct sockaddr *)&server, sizeof(server)) < 0) {
  37. perror("connect failed. Error");
  38. return 1;
  39. }
  40. UA_TcpMessageHeader reqTcpHeader;
  41. UA_UInt32 reqSecureChannelId = 0;
  42. UA_UInt32 reqTokenId = 0;
  43. UA_SequenceHeader reqSequenceHeader;
  44. UA_NodeId reqRequestType;
  45. UA_ReadRequest req;
  46. UA_RequestHeader reqHeader;
  47. UA_NodeId reqHeaderAuthToken;
  48. UA_ExtensionObject reqHeaderAdditionalHeader;
  49. UA_NodeId_init(&reqRequestType);
  50. reqRequestType.identifierType = UA_NODEIDTYPE_NUMERIC;
  51. reqRequestType.identifier.numeric = 631; //read request
  52. UA_SequenceHeader_init(&reqSequenceHeader);
  53. reqSequenceHeader.sequenceNumber = 42;
  54. UA_ReadRequest_init(&req);
  55. req.requestHeader = reqHeader;
  56. UA_RequestHeader_init(&(req.requestHeader));
  57. req.requestHeader.authenticationToken = reqHeaderAuthToken;
  58. UA_NodeId_init(&(req.requestHeader.authenticationToken));
  59. req.requestHeader.additionalHeader = reqHeaderAdditionalHeader;
  60. UA_ExtensionObject_init(&(req.requestHeader.additionalHeader));
  61. req.nodesToRead= UA_Array_new(1, &UA_TYPES[UA_TYPES_READVALUEID]);
  62. req.nodesToReadSize = 1;
  63. UA_ReadValueId_init(&(req.nodesToRead[0]));
  64. req.nodesToRead[0].attributeId = 13; //UA_ATTRIBUTEID_VALUE
  65. UA_NodeId_init(&(req.nodesToRead[0].nodeId));
  66. req.nodesToRead[0].nodeId.identifierType = UA_NODEIDTYPE_NUMERIC;
  67. req.nodesToRead[0].nodeId.identifier.numeric = 2255;
  68. UA_QualifiedName_init(&(req.nodesToRead[0].dataEncoding));
  69. /**messageEncodedLength = UA_TcpMessageHeader_calcSizeBinary(&reqTcpHeader) +
  70. UA_UInt32_calcSizeBinary(&reqSecureChannelId)+
  71. UA_UInt32_calcSizeBinary(&reqTokenId)+
  72. UA_SequenceHeader_calcSizeBinary(&reqSequenceHeader)+
  73. UA_NodeId_calcSizeBinary(&reqRequestType) +
  74. UA_ReadRequest_calcSizeBinary(&req);**/
  75. UA_TcpMessageHeader_init(&reqTcpHeader);
  76. reqTcpHeader.messageTypeAndChunkType = UA_MESSAGETYPE_MSG + UA_CHUNKTYPE_FINAL;
  77. UA_TcpMessageHeader_encodeBinary(&reqTcpHeader, &message, &messagepos);
  78. UA_UInt32_encodeBinary(&reqSecureChannelId, &message, &messagepos);
  79. UA_UInt32_encodeBinary(&reqTokenId, &message, &messagepos);
  80. UA_SequenceHeader_encodeBinary(&reqSequenceHeader, &message, &messagepos);
  81. UA_NodeId_encodeBinary(&reqRequestType, &message, &messagepos);
  82. UA_ReadRequest_encodeBinary(&req, &message, &messagepos);
  83. reqTcpHeader.messageSize = messagepos;
  84. messagepos=0;
  85. UA_TcpMessageHeader_encodeBinary(&reqTcpHeader, &message, &messagepos);
  86. UA_UInt32_encodeBinary(&reqSecureChannelId, &message, &messagepos);
  87. UA_UInt32_encodeBinary(&reqTokenId, &message, &messagepos);
  88. UA_SequenceHeader_encodeBinary(&reqSequenceHeader, &message, &messagepos);
  89. UA_NodeId_encodeBinary(&reqRequestType, &message, &messagepos);
  90. UA_ReadRequest_encodeBinary(&req, &message, &messagepos);
  91. //Send some data
  92. if(send(sock , message.data, messagepos , 0) < 0) {
  93. puts("Send failed");
  94. return 1;
  95. }
  96. //Receive a reply from the server
  97. int received = recv(sock , server_reply , 2000 , 0);
  98. if(received < 0) {
  99. puts("recv failed");
  100. return 1;
  101. }
  102. for(int i=0;i<received;i++) {
  103. //show only printable ascii
  104. if(server_reply[i] >= 32 && server_reply[i]<= 126)
  105. printf("%c",server_reply[i]);
  106. }
  107. printf("\n");
  108. close(sock);
  109. return 0;
  110. }