Untitled

By Gentle Cheetah, 11 Years ago, written in C++, viewed 717 times.
URL http://pb.stoleyour.com/view/d9f485e9 Embed
Download Paste or View RawExpand paste to full width of browser
  1. /*
  2. * TimeCastSrvr.c              (c) Bob Quinn              3/15/97
  3. *
  4. * Released to the public domain
  5. *
  6. * Description:
  7. *  Sample multicast server (sender) application that multicasts the
  8. *  system time for clients (receivers) to use to set their clocks.
  9. */
  10. #include <winsock2.h>
  11. #include <ws2tcpip.h>
  12. #include <stdio.h>
  13. #include <SFML/Audio.hpp>
  14.  
  15. #define BUFSIZE     1024
  16. #define MAXADDRSTR  16
  17.  
  18. #define TIMECAST_ADDR   "234.5.6.7"
  19. #define TIMECAST_PORT   8910
  20. #define TIMECAST_TTL    2
  21. #define TIMECAST_INTRVL 0
  22.  
  23. char achMCAddr[MAXADDRSTR] = TIMECAST_ADDR;
  24. u_long lMCAddr;
  25. u_short nPort              = TIMECAST_PORT;
  26. u_long  lTTL               = TIMECAST_TTL;
  27. u_short nInterval          = TIMECAST_INTRVL;
  28. SYSTEMTIME stSysTime;
  29.  
  30. void show_usage(void) {
  31.         printf(
  32.                 "usage: TimeCastSrvr [-g multicast address][:port number]\n");
  33.         printf("                    [-t IP time-to-live\n\n");
  34.         printf("  multicast address: 224.0.0.0 to 239.255.255.255\n");
  35.         printf("  port number:       server's port\n\n");
  36.         printf("If none provided, default values are %s:%d.\n",
  37.                 TIMECAST_ADDR, TIMECAST_PORT);
  38.         printf("  IP time-to-live: number of router hops (default: 2)\n");
  39. }
  40.  
  41. int main(int argc, char *argv[]) {
  42.         int nRet, i;
  43.         BOOL  fFlag;
  44.         SOCKADDR_IN stLclAddr, stDstAddr;
  45.         struct ip_mreq stMreq;        /* Multicast interface structure */
  46.         SOCKET hSocket;
  47.         WSADATA stWSAData;
  48.  
  49.         /* Display program banner */
  50.         printf("------------------------------------------------------\n");
  51.         printf(" AudioStreamSrvr - multicast audio server\n");
  52.         printf("------------------------------------------------------\n");
  53.  
  54.         /* Process input arguments */
  55.         if ((argc == 2) &&
  56.                 ((strcmp(argv[1],"-help")==0) || (strcmp(argv[1],"-?")==0))) {
  57.                         show_usage();
  58.                         exit(1);
  59.         }
  60.  
  61.         /* Init WinSock */
  62.         nRet = WSAStartup(0x0202, &stWSAData);
  63.         if (nRet) {
  64.                 printf ("WSAStartup failed: %d\r\n", nRet);
  65.                 exit (1);
  66.         }
  67.  
  68.         fFlag = FALSE;
  69.         for (i=1; i<argc; i++) {
  70.                 char *strTmp;
  71.                 if (fFlag) {
  72.                         fFlag = FALSE;
  73.                         continue;
  74.                 }
  75.                 if (!((argv[i][0] == '-') || (argv[i][0] == '/'))) {
  76.                         show_usage();
  77.                         WSACleanup();
  78.                         exit(0);
  79.                 }
  80.                 /* Multicast Group Address and Port setting */
  81.                 if (argv[i][1] == 'g') {
  82.                         fFlag = TRUE;
  83.                         strTmp = strchr(argv[i+1],':');
  84.                         /* Get user port number (in host order) */
  85.                         if (strTmp) {
  86.                                 *strTmp = '\0';
  87.                                 nPort = atoi(++strTmp);
  88.                                 if (nPort == 0) {
  89.                                         printf("Invalid port number provided by user.\n");
  90.                                         printf("using default port number: %d\n",
  91.                                                 TIMECAST_PORT);
  92.                                         nPort = TIMECAST_PORT;
  93.                                 }
  94.                         }
  95.                         /* Get user multicast address */
  96.                         if (argv[i+1] != '\0') {
  97.                                 /* Evaluate multicast address provided */
  98.                                 lMCAddr = inet_addr(argv[i+1]);
  99.                                 if (!((lMCAddr >= 0xe0000000) ||
  100.                                         (lMCAddr <= 0xefffffff))) {
  101.                                                 printf ("Invalid address %s provided by user.\n",
  102.                                                         argv[1]);
  103.                                                 printf ("using default address: %s\n", achMCAddr);
  104.                                 } else {
  105.                                         strcpy (achMCAddr, argv[i+1]); /* Address is ok */
  106.                                 }
  107.                         }
  108.                         continue;
  109.                 }
  110.  
  111.                 /* IP time-to-live setting (scope of multicast send) */
  112.                 if (argv[i][1] == 't') {
  113.                         fFlag = TRUE;
  114.                         lTTL = atoi(argv[i+1]);
  115.                         if ((lTTL <= 0) || (lTTL > 255)) {
  116.                                 printf (
  117.                                         "Invalid IP time-to-live, using default of %d.\n",
  118.                                         TIMECAST_TTL);
  119.                         }
  120.                 }
  121.         }
  122.  
  123.         /* Display current settings */
  124.         printf ("Multicast Address:%s, Port:%d, IP TTL:%d, Interval:%d\n",
  125.                 achMCAddr, nPort, lTTL, nInterval);
  126.  
  127.         /* Get a datagram socket */
  128.         hSocket = socket(AF_INET,
  129.                 SOCK_DGRAM,
  130.                 0);
  131.         if (hSocket == INVALID_SOCKET) {
  132.                 printf ("socket() failed, Err: %d\n", WSAGetLastError());
  133.                 exit(1);
  134.         }
  135.  
  136.         /* Bind the socket
  137.         *
  138.         * NOTE: Normally, we wouldn't need to call bind unless we were
  139.         *  assigning a local port number explicitly (naming the socket),
  140.         *  however Microsoft requires that a socket be bound before it
  141.         *  can join a multicast group with setsockopt() IP_ADD_MEMBERSHIP
  142.         *  (or fails w/ WSAEINVAL).
  143.         */
  144.         stLclAddr.sin_family      = AF_INET;
  145.         stLclAddr.sin_addr.s_addr = htonl(INADDR_ANY); /* any interface */
  146.         stLclAddr.sin_port        = 0;                 /* any port */
  147.         nRet = bind(hSocket,
  148.                 (struct sockaddr*) &stLclAddr,
  149.                 sizeof(stLclAddr));
  150.         if (nRet == SOCKET_ERROR) {
  151.                 printf ("bind() port: %d failed, Err: %d\n", nPort,
  152.                         WSAGetLastError());
  153.         }
  154.  
  155.         /* Join the multicast group
  156.         *
  157.         * NOTE: According to RFC 1112, a sender does not need to join the
  158.         *  group, however Microsoft requires a socket to join a group in
  159.         *  order to use setsockopt() IP_MULTICAST_TTL (or fails with error
  160.         *  WSAEINVAL).
  161.         */
  162.         stMreq.imr_multiaddr.s_addr = inet_addr(achMCAddr);
  163.         stMreq.imr_interface.s_addr = INADDR_ANY;
  164.         nRet = setsockopt(hSocket,
  165.                 IPPROTO_IP,
  166.                 IP_ADD_MEMBERSHIP,
  167.                 (char *)&stMreq,
  168.                 sizeof(stMreq));
  169.         if (nRet == SOCKET_ERROR) {
  170.                 printf (
  171.                         "setsockopt() IP_ADD_MEMBERSHIP address %s failed, Err: %d\n",
  172.                         achMCAddr, WSAGetLastError());
  173.         }
  174.  
  175.         /* Set IP TTL to traverse up to multiple routers */
  176.         nRet = setsockopt(hSocket,
  177.                 IPPROTO_IP,
  178.                 IP_MULTICAST_TTL,
  179.                 (char *)&lTTL,
  180.                 sizeof(lTTL));
  181.         if (nRet == SOCKET_ERROR) {
  182.                 printf ("setsockopt() IP_MULTICAST_TTL failed, Err: %d\n",
  183.                         WSAGetLastError());
  184.         }
  185.  
  186.         /* Disable loopback */
  187.         fFlag = FALSE;
  188.         nRet = setsockopt(hSocket,
  189.                 IPPROTO_IP,
  190.                 IP_MULTICAST_LOOP,
  191.                 (char *)&fFlag,
  192.                 sizeof(fFlag));
  193.         if (nRet == SOCKET_ERROR) {
  194.                 printf ("setsockopt() IP_MULTICAST_LOOP failed, Err: %d\n",
  195.                         WSAGetLastError());
  196.         }
  197.  
  198.         /* Assign our destination address */
  199.         stDstAddr.sin_family =      AF_INET;
  200.         stDstAddr.sin_addr.s_addr = inet_addr(achMCAddr);
  201.         stDstAddr.sin_port =        htons(nPort);
  202.  
  203.         int count = 0;
  204.     // load an audio buffer from a sound file
  205.     sf::SoundBuffer buffer;
  206.         const sf::Int16* pSample;
  207.         sf::Int16 * pSound;
  208.         sf::Int16 * temp;
  209.         size_t sampleCount, sampleRate, channelCount;
  210.     buffer.loadFromFile("test.wav");
  211.  
  212.         sampleRate   = buffer.getSampleRate();
  213.         channelCount = buffer.getChannelCount();
  214.         pSample      = buffer.getSamples();
  215.         sampleCount  = buffer.getSampleCount();
  216.  
  217.  
  218.         pSound = const_cast<sf::Int16*>(pSample);
  219.         temp   = const_cast<sf::Int16*>(pSample);
  220.  
  221.         char * test;
  222.        
  223.     // initialize and play our custom stream
  224.         for (;;) {
  225.                 test = (char *)temp;
  226.                 /*---------------------------------------
  227.  
  228.  
  229.                 Read File
  230.  
  231.  
  232.                 ----------------------------------------*/
  233.                 /* Send the time to our multicast group! */
  234.                 nRet = sendto(hSocket,
  235.                         test,
  236.                         sizeof(sampleRate),
  237.                         0,
  238.                         (struct sockaddr*)&stDstAddr,
  239.                         sizeof(stDstAddr));
  240.                 if (nRet < 0) {
  241.                         printf ("sendto() failed, Error: %d\n", WSAGetLastError());
  242.                         exit(1);
  243.                 }
  244.                 printf("Sent...\n");
  245.                 count++;
  246.                 &temp[count*sampleRate];       
  247.                 /* Wait for the specified interval */
  248.                 //Sleep(nInterval*1000);
  249.  
  250.         } /* end for(;;) */
  251.  
  252.         /* Close the socket */
  253.         closesocket(hSocket);
  254.  
  255.         /* Tell WinSock we're leaving */
  256.         WSACleanup();
  257.  
  258.         return (0);
  259. } /* end main() */

Reply to "Untitled"

Here you can reply to the paste above