client.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* This is a simple TCP client that connects to port 1234 and prints a list
  2. * of files in a given directory.
  3. *
  4. * It directly deserializes and serializes messages from network, minimizing
  5. * memory use.
  6. *
  7. * For flexibility, this example is implemented using posix api.
  8. * In a real embedded system you would typically use some other kind of
  9. * a communication and filesystem layer.
  10. */
  11. #include <sys/socket.h>
  12. #include <sys/types.h>
  13. #include <netinet/in.h>
  14. #include <unistd.h>
  15. #include <dirent.h>
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <pb_encode.h>
  19. #include <pb_decode.h>
  20. #include "fileproto.pb.h"
  21. #include "common.h"
  22. /* This callback function will be called once for each filename received
  23. * from the server. The filenames will be printed out immediately, so that
  24. * no memory has to be allocated for them.
  25. */
  26. bool ListFilesResponse_callback(pb_istream_t *istream, pb_ostream_t *ostream, const pb_field_iter_t *field)
  27. {
  28. PB_UNUSED(ostream);
  29. if (istream != NULL && field->tag == ListFilesResponse_file_tag)
  30. {
  31. FileInfo fileinfo = {};
  32. if (!pb_decode(istream, FileInfo_fields, &fileinfo))
  33. return false;
  34. printf("%-10lld %s\n", (long long)fileinfo.inode, fileinfo.name);
  35. }
  36. return true;
  37. }
  38. /* This function sends a request to socket 'fd' to list the files in
  39. * directory given in 'path'. The results received from server will
  40. * be printed to stdout.
  41. */
  42. bool listdir(int fd, char *path)
  43. {
  44. /* Construct and send the request to server */
  45. {
  46. ListFilesRequest request = {};
  47. pb_ostream_t output = pb_ostream_from_socket(fd);
  48. /* In our protocol, path is optional. If it is not given,
  49. * the server will list the root directory. */
  50. if (path == NULL)
  51. {
  52. request.has_path = false;
  53. }
  54. else
  55. {
  56. request.has_path = true;
  57. if (strlen(path) + 1 > sizeof(request.path))
  58. {
  59. fprintf(stderr, "Too long path.\n");
  60. return false;
  61. }
  62. strcpy(request.path, path);
  63. }
  64. /* Encode the request. It is written to the socket immediately
  65. * through our custom stream. */
  66. if (!pb_encode_delimited(&output, ListFilesRequest_fields, &request))
  67. {
  68. fprintf(stderr, "Encoding failed: %s\n", PB_GET_ERROR(&output));
  69. return false;
  70. }
  71. }
  72. /* Read back the response from server */
  73. {
  74. ListFilesResponse response = {};
  75. pb_istream_t input = pb_istream_from_socket(fd);
  76. if (!pb_decode_delimited(&input, ListFilesResponse_fields, &response))
  77. {
  78. fprintf(stderr, "Decode failed: %s\n", PB_GET_ERROR(&input));
  79. return false;
  80. }
  81. /* If the message from server decodes properly, but directory was
  82. * not found on server side, we get path_error == true. */
  83. if (response.path_error)
  84. {
  85. fprintf(stderr, "Server reported error.\n");
  86. return false;
  87. }
  88. }
  89. return true;
  90. }
  91. int main(int argc, char **argv)
  92. {
  93. int sockfd;
  94. struct sockaddr_in servaddr;
  95. char *path = NULL;
  96. if (argc > 1)
  97. path = argv[1];
  98. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  99. /* Connect to server running on localhost:1234 */
  100. memset(&servaddr, 0, sizeof(servaddr));
  101. servaddr.sin_family = AF_INET;
  102. servaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  103. servaddr.sin_port = htons(1234);
  104. if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) != 0)
  105. {
  106. perror("connect");
  107. return 1;
  108. }
  109. /* Send the directory listing request */
  110. if (!listdir(sockfd, path))
  111. return 2;
  112. /* Close connection */
  113. close(sockfd);
  114. return 0;
  115. }