By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,778 Members | 1,890 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,778 IT Pros & Developers. It's quick & easy.

MultiCasting Code

P: 17
Hi all,
I am a relatively new programmer in C++...I need to write a code that performs multicasting as well as returns the data-packets in a decompressed form.Data-receipt seems to work fine....but decompression throws up an error...


The following is my code:

Expand|Select|Wrap|Line Numbers
  1. # include "StdAfx.h"
  2.  
  3. #include <winsock2.h>
  4. #include <ws2tcpip.h>
  5. #include <sys/types.h>  /* for type definitions */
  6.  
  7. #include <stdio.h>      /* for printf() and fprintf() */
  8.  
  9. #include <stdlib.h>     /* for atoi() */
  10.  
  11. #include <string.h>     /* for strlen() */
  12.  
  13. //#include "lzoconf.h"
  14. //#include <lzo1x.h>
  15.  
  16.  
  17.  
  18. /* portability layer */
  19.  
  20. //#define WANT_LZO_MALLOC 1
  21. //#include "portab.h"
  22.  
  23.  
  24.  
  25. #define MAX_LEN  1024   /* maximum receive string size */ 
  26.  
  27. #define MIN_PORT 1024   /* minimum port allowed */
  28.  
  29. #define MAX_PORT 65535  /* maximum port allowed */
  30.  
  31. #ifndef IN_LEN
  32. #define IN_LEN      (128*1024L)
  33. #endif
  34. #define OUT_LEN     (IN_LEN + IN_LEN / 16 + 64 + 3)
  35.  
  36. //#include "lutil.h"
  37.  
  38. int main(int argc, char *argv[]) {
  39.  
  40.  
  41.   //string str;
  42.  
  43.   //string port;
  44.  
  45.   int sock;                     /* socket descriptor */
  46.  
  47.   int flag_on = 1;              /* socket option flag */
  48.  
  49.   struct sockaddr_in mc_addr;   /* socket address structure */
  50.  
  51.   char recv_str[MAX_LEN+1];     /* buffer to receive string */
  52.  
  53.   int recv_len;                 /* length of string received */
  54.  
  55.   struct ip_mreq mc_req;        /* multicast request structure */
  56.  
  57.   char mc_addr_str[11]="225.0.0.36";            /* multicast IP address */
  58.  
  59.   int mc_port=15000;       /* multicast port */
  60.  
  61. //char mc_addr_str[11]="233.1.2.4";            /* multicast IP address */
  62.  
  63.   //int mc_port=6755;       /* multicast port */
  64.  
  65.  
  66.   struct sockaddr_in from_addr; /* packet source */
  67.  
  68.   int from_len;        /* source addr length */
  69.  
  70.   WSADATA wsaData; 
  71.  
  72.   /* Windows socket DLL structure */
  73.  
  74.     /*int r;
  75.     lzo_byte *in;
  76.     //lzo_bytep recv_str;
  77.     lzo_byte *wrkmem;
  78.     lzo_uint in_len;
  79.     lzo_uint recv_str_len;
  80.     lzo_uint new_len;
  81.  
  82.  
  83.   //str="225.0.0.36";
  84.  
  85.   //port="15000";
  86.  
  87.   /* validate number of arguments */
  88.  
  89.   if (argc != 3) {
  90.  
  91.     fprintf(stderr, 
  92.  
  93.             "Usage: %s <Multicast IP> <Multicast Port>\n", 
  94.  
  95.             argv[0]);
  96.  
  97.    // exit(1);
  98.  
  99.   }
  100.  
  101.  
  102.  
  103.  // mc_addr_str = "225.0.0.36";      /* arg 1: multicast ip address */
  104.  
  105.   //mc_port = "15000";    /* arg 2: multicast port number */
  106.  
  107.  
  108.  
  109.   /* validate the port range */
  110.  
  111.   if ((mc_port < MIN_PORT) || (mc_port > MAX_PORT)) {
  112.  
  113.     fprintf(stderr, "Invalid port number argument %d.\n",
  114.  
  115.             mc_port);
  116.  
  117.     fprintf(stderr, "Valid range is between %d and %d.\n",
  118.  
  119.             MIN_PORT, MAX_PORT);
  120.  
  121.    // exit(1);
  122.  
  123.   }
  124.  
  125.  
  126.  
  127.   /* Load Winsock 2.0 DLL */
  128.  
  129.   if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0) {
  130.  
  131.       fprintf(stderr, "WSAStartup() failed");
  132.  
  133.       //exit(1);
  134.  
  135.   }
  136.  
  137.  
  138.  
  139.   /* create socket to join multicast group on */
  140.  
  141.   if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
  142.  
  143.     perror("socket() failed");
  144.  
  145.     //exit(1);
  146.  
  147.   }
  148.  
  149.  
  150.  
  151.   /* set reuse port to on to allow multiple binds per host */
  152.  
  153.   if ((setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*)&flag_on,
  154.  
  155.        sizeof(flag_on))) < 0) {
  156.  
  157.     perror("setsockopt() failed");
  158.  
  159.      // exit(1);
  160.  
  161.   }
  162.  
  163.  
  164.  
  165.   /* construct a multicast address structure */
  166.  
  167.   memset(&mc_addr, 0, sizeof(mc_addr));
  168.  
  169.   mc_addr.sin_family      = AF_INET;
  170.  
  171.   mc_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  172.  
  173.   mc_addr.sin_port        = htons(mc_port);
  174.  
  175.  
  176.  
  177.   /* bind to multicast address to socket */
  178.  
  179.   if ((bind(sock, (struct sockaddr *) &mc_addr, 
  180.  
  181.        sizeof(mc_addr))) < 0) {
  182.  
  183.     perror("bind() failed");
  184.  
  185.     //exit(1);
  186.  
  187.   }
  188.  
  189.  
  190.  
  191.   /* construct an IGMP join request structure */
  192.  
  193.   mc_req.imr_multiaddr.s_addr = inet_addr(mc_addr_str);
  194.  
  195.   mc_req.imr_interface.s_addr = htonl(INADDR_ANY);
  196.  
  197.  
  198.  
  199.   /* send an ADD MEMBERSHIP message via setsockopt */
  200.  
  201.   if ((setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, 
  202.  
  203.        (char*) &mc_req, sizeof(mc_req))) < 0) {
  204.  
  205.     perror("setsockopt() failed");
  206.  
  207.    // exit(1);
  208.  
  209.   }
  210.  
  211.  
  212.  
  213.   for (;;) {          /* loop forever */
  214.  
  215.  
  216.  
  217.     /* clear the receive buffers & structs */
  218.  
  219.     memset(recv_str, 0, sizeof(recv_str));
  220.  
  221.     from_len = sizeof(from_addr);
  222.  
  223.     memset(&from_addr, 0, from_len);
  224.  
  225.  
  226.  
  227.     /* block waiting to receive a packet */
  228.  
  229.     if ((recv_len = recvfrom(sock, recv_str, MAX_LEN, 0, 
  230.  
  231.          (struct sockaddr*)&from_addr, &from_len)) < 0) {
  232.  
  233.       perror("recvfrom() failed");
  234.  
  235.       exit(1);
  236.  
  237.     }
  238.  
  239.  
  240.  
  241.     /* output received string */
  242.  
  243.     printf("Received %d bytes from %s: ", recv_len, 
  244.  
  245.            inet_ntoa(from_addr.sin_addr));
  246.  
  247.     printf("%s", recv_str);
  248.  
  249.   }
  250.  
  251.   /*byte packet;
  252.   packet=(byte)recv_str;
  253.  
  254. #if defined(_EMX_)
  255.   _response(&argc,&argv);
  256.   _wildcard(&argc,&argv);
  257. #endif
  258.  
  259.   if(argc<0 &&argv==NULL)
  260.       return 0;
  261.  
  262.   printf("\nLZO real-time data compression library(v%s,%s).\n",lzo_version_string(),lzo_version_date());
  263.   printf("Copyright (C)1996-2000 Markus Franz Xaver Johannes Oberhumer\n\n");
  264.  
  265. /*
  266.  * Step 1: initialize the LZO library
  267.  */
  268. /*if (lzo_init() != LZO_E_OK)
  269.     {
  270.         printf("internal error - lzo_init() failed !!!\n");
  271.         printf("(this usually indicates a compiler bug - try recompiling\nwithout optimizations, and enable `-DLZO_DEBUG' for diagnostics)\n");
  272.         //return 4;
  273.     }
  274.  
  275. /*
  276.  * Step 2: allocate blocks and the work-memory
  277.  */
  278.     /*in = (lzo_bytep) lzo_malloc(IN_LEN);
  279.     //out = (lzo_bytep) lzo_malloc(OUT_LEN);
  280.     wrkmem = (lzo_bytep) lzo_malloc(LZO1X_1_MEM_COMPRESS);
  281.     if (in == NULL || recv_str == NULL || wrkmem == NULL) 
  282.     {
  283.         printf("out of memory\n");
  284.         //return 3;
  285.     }
  286.  
  287. /*
  288.  * Step 3: prepare the input block that will get compressed.
  289.  *         We just fill it with zeros in this example program,
  290.  *         but you would use your real-world data here.
  291.  */
  292.     /*in_len = IN_LEN;
  293.     lzo_memset(in,packet,in_len);
  294.  
  295.  
  296. /*
  297.  * Step 4: decompress again, now going from `out' to `in'
  298.  */
  299.     /*new_len = in_len;
  300.     r = lzo1x_decompress(packet,recv_str_len,in,&new_len,NULL);
  301.     if (r == LZO_E_OK && new_len == in_len)
  302.         printf("decompressed %lu bytes back into %lu bytes\n",
  303.             (unsigned long) recv_str_len, (unsigned long) in_len);
  304.     else
  305.     {
  306.         /* this should NEVER happen */
  307.         /*printf("internal error - decompression failed: %d\n", r);
  308.         return 1;
  309.     }
  310.  
  311.     lzo_free(wrkmem);
  312.     lzo_free(recv_str);
  313.     lzo_free(in);
  314.     printf("Simple compression test passed.\n");
  315.     return 0; 
  316. }
  317.  
  318.  
  319.  
  320.  
  321.   /* send a DROP MEMBERSHIP message via setsockopt */
  322.  
  323.   if ((setsockopt(sock, IPPROTO_IP, IP_DROP_MEMBERSHIP, 
  324.  
  325.        (const char*) &mc_req, sizeof(mc_req))) < 0) 
  326.   {
  327.     perror("setsockopt() failed");
  328.  
  329.     exit(1);
  330.  
  331. }
  332.  
  333.  
  334.   closesocket(sock);  
  335.  
  336.   WSACleanup();  /* Cleanup Winsock */
  337.  
  338.  
  339. }
The Lzo(Oberhumer.com)algorithm has been used for decompression.
The header files used are WSOCK32.LIB,WS2_32.LIB,lzo1x.h,lutil.h...

Where am I going wrong?

Any help will be really appreciated...

Cheers
Feb 23 '07 #1
Share this Question
Share on Google+
9 Replies


DeMan
100+
P: 1,806
Please include the error you said was appearing.....
Feb 23 '07 #2

P: 17
Actually,I was changing the decompression part often....the latest change being trying to typecast the received packets' type...the latest error I received was abt problem regarding conversion of 'unsigned char' to 'const unsigned char*' in the lzo_decompress function.Am I doing the rest of the coding ok?

Thanks for quick response..Cheers
Feb 23 '07 #3

DeMan
100+
P: 1,806
So long as the parameters you pass are right (ie if the function wants a const char* you can't give is a char* and vice versa)
Feb 23 '07 #4

P: 17
So long as the parameters you pass are right (ie if the function wants a const char* you can't give is a char* and vice versa)
Ok,so now how do I resolve this problem?the variable 'packet' needs to be typecasted?the data packets are being received in ' recv_str' byte stream...what should the correct code snippet be?

Thanks for your effort...
Cheers...
Feb 23 '07 #5

DeMan
100+
P: 1,806
I can't see where packet is declared (although sometimes I scan too quickly so someone may be able to enlighten me.....)
Feb 23 '07 #6

P: 17
The variable 'packet' has been declared just before 'recv_str' has typecasted to 'int'....I tried declaring 'packet' as 'byte' or 'int' but none really helped.....so currently the line 'byte packet' is commented....

Thanks & Cheers
Feb 23 '07 #7

DeMan
100+
P: 1,806
A byte and a char are the smae thing (I think) so the problem is that the method expects a const char. I THINK that you can cast it to a const char (that is put the type the method expects in brackets before the variable.
Alternatively, if you never change packet (other than wher you initialise it) some languages are happy for you to declare something const (and not initialise it) but when you initialise it, it becomes a const of the initialised value. Assuming you never modify it, declaring it const may be a better solution.....
Feb 23 '07 #8

P: 17
Hi,
I tried typecasting 'packet' to const char(and also to 'char')but the problem persists...Has this something to do with the pointers...the error is 'lzo1x_decompress-cannot convert 'char' to 'unsigned const char*'....I am really confused...:(

Thanks for the effort...I appreciate it..

Cheers
Feb 23 '07 #9

P: 17
Hi...
I resolved that problem by casting 'recv_str' to 'unsigned const char*' directly(and not using 'packet').But now I have a new problem....since I am using an infinite 'for' loop(I have to,since live data needs to be received)...the control never comes to the decompression part of the code..!How do I solve this?

Thanks in advance...
Cheers
Feb 23 '07 #10

Post your reply

Sign in to post your reply or Sign up for a free account.