streamplayer.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, Digium, Inc.
  5. *
  6. * Russell Bryant <russell@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*
  19. *
  20. * streamplayer.c
  21. *
  22. * A utility for reading from a stream
  23. *
  24. */
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <netdb.h>
  29. #include <unistd.h>
  30. #include <sys/types.h>
  31. #include <sys/socket.h>
  32. #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__Darwin__) || defined(__CYGWIN__)
  33. #include <netinet/in.h>
  34. #endif
  35. #include <sys/time.h>
  36. int main(int argc, char *argv[])
  37. {
  38. struct sockaddr_in sin;
  39. struct hostent *hp;
  40. int s;
  41. int res;
  42. char buf[2048];
  43. fd_set wfds;
  44. struct timeval tv;
  45. if (argc != 3) {
  46. fprintf(stderr, "streamplayer -- A utility for reading from a stream.\n");
  47. fprintf(stderr, "Written for use with Asterisk (http://www.asterisk.org)\n");
  48. fprintf(stderr, "Copyright (C) 2005 -- Russell Bryant -- Digium, Inc.\n\n");
  49. fprintf(stderr, "Usage: ./streamplayer <ip> <port>\n");
  50. exit(1);
  51. }
  52. hp = gethostbyname(argv[1]);
  53. if (!hp) {
  54. fprintf(stderr, "Unable to lookup IP for host '%s'\n", argv[1]);
  55. exit(1);
  56. }
  57. memset(&sin, 0, sizeof(sin));
  58. sin.sin_family = AF_INET;
  59. sin.sin_port = htons(atoi(argv[2]));
  60. memcpy(&sin.sin_addr, hp->h_addr, sizeof(sin.sin_addr));
  61. s = socket(AF_INET, SOCK_STREAM, 0);
  62. if (s < 0) {
  63. fprintf(stderr, "Unable to allocate socket!\n");
  64. exit(1);
  65. }
  66. res = connect(s, (struct sockaddr *)&sin, sizeof(sin));
  67. if (res) {
  68. fprintf(stderr, "Unable to connect to host!\n");
  69. close(s);
  70. exit(1);
  71. }
  72. while (1) {
  73. res = read(s, buf, sizeof(buf));
  74. if (res < 1)
  75. break;
  76. memset(&tv, 0, sizeof(tv));
  77. FD_ZERO(&wfds);
  78. FD_SET(1, &wfds);
  79. select(2, NULL, &wfds, NULL, &tv);
  80. if (FD_ISSET(1, &wfds))
  81. write(1, buf, res);
  82. }
  83. close(s);
  84. exit(res);
  85. }