qos0pub.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*******************************************************************************
  2. * Copyright (c) 2014 IBM Corp.
  3. *
  4. * All rights reserved. This program and the accompanying materials
  5. * are made available under the terms of the Eclipse Public License v1.0
  6. * and Eclipse Distribution License v1.0 which accompany this distribution.
  7. *
  8. * The Eclipse Public License is available at
  9. * http://www.eclipse.org/legal/epl-v10.html
  10. * and the Eclipse Distribution License is available at
  11. * http://www.eclipse.org/org/documents/edl-v10.php.
  12. *
  13. * Contributors:
  14. * Ian Craggs - initial API and implementation and/or initial documentation
  15. * Sergio R. Caprile - clarifications and/or documentation extension
  16. *******************************************************************************/
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <stdlib.h>
  20. #include "MQTTPacket.h"
  21. #include "transport.h"
  22. int main(int argc, char *argv[])
  23. {
  24. MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
  25. int rc = 0;
  26. char buf[200];
  27. int buflen = sizeof(buf);
  28. int mysock = 0;
  29. MQTTString topicString = MQTTString_initializer;
  30. char* payload = "mypayload";
  31. int payloadlen = strlen(payload);
  32. int len = 0;
  33. char *host = "m2m.eclipse.org";
  34. int port = 1883;
  35. if (argc > 1)
  36. host = argv[1];
  37. if (argc > 2)
  38. port = atoi(argv[2]);
  39. mysock = transport_open(host,port);
  40. if(mysock < 0)
  41. return mysock;
  42. printf("Sending to hostname %s port %d\n", host, port);
  43. data.clientID.cstring = "me";
  44. data.keepAliveInterval = 20;
  45. data.cleansession = 1;
  46. data.username.cstring = "testuser";
  47. data.password.cstring = "testpassword";
  48. data.MQTTVersion = 4;
  49. len = MQTTSerialize_connect((unsigned char *)buf, buflen, &data);
  50. topicString.cstring = "mytopic";
  51. len += MQTTSerialize_publish((unsigned char *)(buf + len), buflen - len, 0, 0, 0, 0, topicString, (unsigned char *)payload, payloadlen);
  52. len += MQTTSerialize_disconnect((unsigned char *)(buf + len), buflen - len);
  53. rc = transport_sendPacketBuffer(mysock, buf, len);
  54. if (rc == len)
  55. printf("Successfully published\n");
  56. else
  57. printf("Publish failed\n");
  58. exit:
  59. transport_close(mysock);
  60. return 0;
  61. }