statelessClient.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. #include "ua_transport_generated.h"
  11. #include "ua_namespace_0.h"
  12. #include "ua_util.h"
  13. int main(int argc , char *argv[])
  14. {
  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 EXTENSION_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. {
  31. printf("Could not create socket");
  32. }
  33. server.sin_addr.s_addr = inet_addr("127.0.0.1");
  34. server.sin_family = AF_INET;
  35. server.sin_port = htons( 16664 );
  36. //Connect to remote server
  37. if (connect(sock , (struct sockaddr *)&server , sizeof(server)) < 0)
  38. {
  39. perror("connect failed. Error");
  40. return 1;
  41. }
  42. UA_TcpMessageHeader reqTcpHeader;
  43. UA_UInt32 reqSecureChannelId = 0;
  44. UA_UInt32 reqTokenId = 0;
  45. UA_SequenceHeader reqSequenceHeader;
  46. UA_NodeId reqRequestType;
  47. UA_ReadRequest req;
  48. UA_RequestHeader reqHeader;
  49. UA_NodeId reqHeaderAuthToken;
  50. UA_ExtensionObject reqHeaderAdditionalHeader;
  51. UA_NodeId_init(&reqRequestType);
  52. reqRequestType.identifierType = UA_NODEIDTYPE_NUMERIC;
  53. reqRequestType.identifier.numeric = 631; //read request
  54. UA_SequenceHeader_init(&reqSequenceHeader);
  55. reqSequenceHeader.sequenceNumber = 42;
  56. UA_ReadRequest_init(&req);
  57. req.requestHeader = reqHeader;
  58. UA_RequestHeader_init(&(req.requestHeader));
  59. req.requestHeader.authenticationToken = reqHeaderAuthToken;
  60. UA_NodeId_init(&(req.requestHeader.authenticationToken));
  61. req.requestHeader.additionalHeader = reqHeaderAdditionalHeader;
  62. UA_ExtensionObject_init(&(req.requestHeader.additionalHeader));
  63. UA_Array_new((void **)&req.nodesToRead, 1, &UA_TYPES[UA_READVALUEID]);
  64. req.nodesToReadSize = 1;
  65. UA_ReadValueId_init(&(req.nodesToRead[0]));
  66. req.nodesToRead[0].attributeId = 13; //UA_ATTRIBUTEID_VALUE
  67. UA_NodeId_init(&(req.nodesToRead[0].nodeId));
  68. req.nodesToRead[0].nodeId.identifierType = UA_NODEIDTYPE_NUMERIC;
  69. req.nodesToRead[0].nodeId.identifier.numeric = 2255;
  70. UA_QualifiedName_init(&(req.nodesToRead[0].dataEncoding));
  71. messageEncodedLength = UA_TcpMessageHeader_calcSizeBinary(&reqTcpHeader) +
  72. UA_UInt32_calcSizeBinary(&reqSecureChannelId)+
  73. UA_UInt32_calcSizeBinary(&reqTokenId)+
  74. UA_SequenceHeader_calcSizeBinary(&reqSequenceHeader)+
  75. UA_NodeId_calcSizeBinary(&reqRequestType) +
  76. UA_ReadRequest_calcSizeBinary(&req);
  77. UA_TcpMessageHeader_init(&reqTcpHeader);
  78. reqTcpHeader.messageType = UA_MESSAGETYPE_MSG;
  79. reqTcpHeader.messageSize = messageEncodedLength;
  80. reqTcpHeader.isFinal = 'F';
  81. UA_TcpMessageHeader_encodeBinary(&reqTcpHeader, &message, &messagepos);
  82. UA_UInt32_encodeBinary(&reqSecureChannelId, &message, &messagepos);
  83. UA_UInt32_encodeBinary(&reqTokenId, &message, &messagepos);
  84. UA_SequenceHeader_encodeBinary(&reqSequenceHeader, &message, &messagepos);
  85. UA_NodeId_encodeBinary(&reqRequestType, &message, &messagepos);
  86. UA_ReadRequest_encodeBinary(&req, &message, &messagepos);
  87. //Send some data
  88. if( send(sock , message.data, messagepos , 0) < 0)
  89. {
  90. puts("Send failed");
  91. return 1;
  92. }
  93. //Receive a reply from the server
  94. int received = recv(sock , server_reply , 2000 , 0);
  95. if(received < 0)
  96. {
  97. puts("recv failed");
  98. return 1;
  99. }
  100. for(int i=0;i<received;i++){
  101. //show only printable ascii
  102. if(server_reply[i] >= 32 && server_reply[i]<= 126)
  103. printf("%c",server_reply[i]);
  104. }
  105. printf("\n");
  106. close(sock);
  107. return 0;
  108. }