473,480 Members | 1,874 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

MultiCasting Code

17 New Member
Hi all,
I am a comparative fresher in C# coding.I need to write a code for multicasting as well as decompression of the received data packets.The following is my code:

Expand|Select|Wrap|Line Numbers
  1. using System;             // For Console and Exception classes
  2. using System.Net;         // For IPAddress class
  3. using System.Net.Sockets; // For Socket class
  4. using System.Runtime.InteropServices;
  5. using System.IO.Compression;
  6. using ManagedQLZ;
  7. using System.IO;
  8.  
  9. //using Simplicit.Net.Lzo;
  10. //enum IN_LEN; //:long {Max = 1024L, Min = 128};
  11. //enum OUT_LEN; //:long{IN_LEN + IN_LEN / 64 + 16 + 3};
  12.  
  13. class MCReceive
  14. {
  15.  
  16.     const int MIN_PORT = 1024;  // min port value
  17.     const int MAX_PORT = 65535; // max port value
  18.  
  19.     unsafe static void Main()
  20.     {
  21.  
  22.         //string str = "233.1.2.4";
  23.        //string port = "6755";
  24.         string str = "225.0.0.36";
  25.        string port = "15000";
  26.         Socket sock;           // Multicast socket
  27.         IPAddress mcIP;           // Multicast group to join
  28.         int mcPort;         // Port to receive on
  29.         IPEndPoint receivePoint;   // IP end point
  30.         int MAX_LEN = 1024;   // Max receive buffer size
  31.         Boolean done = false;     // loop variable
  32.         //int r;
  33.         /*lzo_byte* inData;
  34.         lzo_byte* wrkmem; 
  35.         lzo_uint inData_len;
  36.         lzo_uint recData_len;
  37.         lzo_uint new_len;*/
  38.  
  39.         Console.Error.WriteLine("Usage: MCReceive " +
  40.                                 "<Multicast IP> <Multicast Port>");
  41.         mcIP = IPAddress.Parse(str);
  42.         mcPort = Int32.Parse(port);
  43.         if ((mcPort < MIN_PORT) || (mcPort > MAX_PORT))
  44.         {
  45.             Console.Error.WriteLine("Invalid Port specified.");
  46.             Console.Error.WriteLine("Port must be between " + MIN_PORT
  47.                                     + " and " + MAX_PORT);
  48.             return;
  49.         }
  50.  
  51.         //try
  52.         {
  53.  
  54.             // Create the Socket
  55.             sock = new Socket(AddressFamily.InterNetwork,
  56.                               SocketType.Dgram, ProtocolType.Udp);
  57.                               //ProtocolType.Udp);
  58.  
  59.             // Set the reuse address option
  60.             sock.SetSocketOption(SocketOptionLevel.Socket,
  61.                                  SocketOptionName.ReuseAddress, 1);
  62.  
  63.             // Create an IPEndPoint and bind to it
  64.             IPEndPoint ipep = new IPEndPoint(IPAddress.Any, mcPort);
  65.             sock.Bind(ipep);
  66.  
  67.             // Add membership in the multicast group
  68.             sock.SetSocketOption(SocketOptionLevel.IP,
  69.                                  SocketOptionName.AddMembership,
  70.                                  new MulticastOption(mcIP, IPAddress.Any));
  71.  
  72.             // Create the EndPoint class
  73.             receivePoint = new IPEndPoint(IPAddress.Any, 0);
  74.             EndPoint tempReceivePoint = (EndPoint)receivePoint;
  75.  
  76.  
  77.  
  78.             string path = "C:\\decompress.txt";
  79.             StreamWriter writer = File.AppendText(@path);
  80.             //string OutputDecompress;
  81.  
  82.             //int i = 0;
  83.             byte[] recData = new byte[MAX_LEN];
  84.             byte[] Outdata = new byte[MAX_LEN];
  85.  
  86.             //while (!done && i<=4)
  87.  
  88.             while (!done )
  89.             {                
  90.                 // Receive the multicast packets
  91.                 int length = sock.ReceiveFrom(recData, 0, MAX_LEN,
  92.                                               SocketFlags.None,
  93.                                               ref tempReceivePoint);
  94.                 //foreach(int i in recData )
  95.                 //Console.WriteLine(recData[i].ToString());
  96.                 // Format and output the received data packet
  97.                 System.Text.ASCIIEncoding encode =
  98.                                         new System.Text.ASCIIEncoding();
  99.                 Console.WriteLine("Received " + length + " bytes from " +
  100.                                   tempReceivePoint.ToString() + ": "); //+encode.GetString(recData, 0, length));
  101.  
  102.  
  103.                 byte[] compressed = QuickLZ.Compress(recData, 0, (UInt32)recData.Length);
  104.                 byte[] decompressed = QuickLZ.Decompress(compressed, 0);
  105.  
  106.                // LZOCompressor lzo1 = new LZOCompressor(); 
  107.                 //byte[] decompressed =  lzo1.Decompress(recData);
  108.  
  109.  
  110.                // writer.WriteLine(recData[100]); 
  111.                 //Outdata = Compress.Decompress1(recData);
  112.                writer.WriteLine("Received " + length + " bytes from " +
  113.                                   tempReceivePoint.ToString() + ": ------------------");
  114.  
  115.                System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
  116.                //string OutputDecompress = enc.GetString(decompressed);
  117.                string OutputDecompress = enc.GetString(decompressed);
  118.                writer.WriteLine(OutputDecompress);
  119.                //System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
  120.                writer.WriteLine("----------------");
  121.                 foreach (int j in decompressed)
  122.               {
  123.  
  124.                  //OutputDecompress = enc.GetString(decompressed[j]);
  125.  
  126.                    writer.Write(decompressed[j]+"   ");
  127.                    //writer.Write(decompressed + "   ");
  128.                }
  129.                     //Console.WriteLine(decompressed[j]);
  130.  
  131.                 //Console.WriteLine(Outdata[0]);
  132.                 //Console.WriteLine(recData[101]);
  133.             }
  134.             //Compress decom = new Compress();
  135.  
  136.             //writer.Close();
  137.             //Console.WriteLine(Outdata);
  138.  
  139.             //writer.WriteLine(Outdata);                
  140.  
  141.             writer.Close();
  142.  
  143.             //StreamWriter writer = File.AppendText(@path);
  144.             ////Test In txt File
  145.  
  146.             //Set the enable property to false.
  147.  
  148.             //Append the text Tick to the C:\sample.txt file.
  149.  
  150.             //writer.WriteLine(Outdata);
  151.  
  152.  
  153.             //end///Test In txt File
  154.             //Decompress
  155.  
  156.  
  157.             ///End Decompress
  158.  
  159.  
  160.             /*//Step 1:initialise LZO library
  161.             if (lzo_init() != LZO_E_OK)
  162.             {
  163.                 printf("lzo_init() failed !!!\n");
  164.             }
  165.  
  166.             //Step 2: allocate blocks and the work-memory
  167.  
  168.             inData = lzo_malloc(1024);
  169.             recData = lzo_malloc(2048);
  170.             wrkmem = lzo_malloc(LZO1X_1_MEM_COMPRESS);
  171.             if (inData == NULL || recData == NULL || wrkmem == NULL)
  172.             {
  173.                 printf("out of memory\n");
  174.                 //return 1;
  175.             }
  176.  
  177.             inData_len = IN_LEN;
  178.             // Step 3: decompress now going from `recData' to `inData'
  179.  
  180.             r = lzo1x_decompress(recData, recData_len, inData, &new_len, NULL);
  181.             if (r == LZO_E_OK && new_len == inData_len)
  182.                 printf("decompressed %lu bytes back into %lu bytes\n",
  183.                     (long)recData_len, (long)inData_len);
  184.             else
  185.             {
  186.                 /* this should NEVER happen 
  187.                 printf("internal error - decompression failed: %d\n", r);
  188.                 return 1;
  189.             }
  190.  
  191.             lzo_free(wrkmem);
  192.             lzo_free(inData);
  193.             lzo_free(recData);
  194.             printf("Simple compression test passed.\n");
  195.             return 0;*/
  196.  
  197.             // Drop membership
  198.             sock.SetSocketOption(SocketOptionLevel.IP,
  199.                                 SocketOptionName.DropMembership,
  200.                                  new MulticastOption(mcIP,
  201.                                                      IPAddress.Any));
  202.  
  203.             // Close the socket
  204.             sock.Close();
  205.  
  206.         } 
  207.         //catch (SocketException se)
  208.         //{
  209.         //    Console.WriteLine("Socket Exception: " + se.ToString());
  210.         //    return;
  211.         //}
  212.         //catch (Exception e)
  213.         //{
  214.         //    Console.Error.WriteLine("Exception: " + e.ToString());
  215.         //    return;
  216.         //}
  217.  
  218.     }
  219.  
  220.     // return 1;
  221.  
  222. }
I have tried lzo.Net algorithm but it does not seem to work.The QuickLZ algorithm does not seem to provide meaningful results.
Where am I going wrong?

Any help will be really appreciated...
Cheers..
Feb 23 '07 #1
0 1370

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

Similar topics

51
5196
by: Mudge | last post by:
Please, someone, tell me why OO in PHP is better than procedural.
9
3841
by: bigoxygen | last post by:
Hi. I'm using a 3 tier FrontController Design for my web application right now. The problem is that I'm finding to have to duplicate a lot of code for similar functions; for example, listing...
4
2413
by: jason | last post by:
Hello. Newbie on SQL and suffering through this. I have two tables created as such: drop table table1; go drop table table2; go
16
3082
by: Dario de Judicibus | last post by:
I'm getting crazy. Look at this code: #include <string.h> #include <stdio.h> #include <iostream.h> using namespace std ; char ini_code = {0xFF, 0xFE} ; char line_sep = {0x20, 0x28} ;
0
1125
by: Nicholas Beenham | last post by:
Hi all, I'm having problems with a udpclient with multicasting and then waiting for a response and if none arrives closes after a few seconds. I have the client multicasting fine but am having...
0
1571
by: gregory_may | last post by:
I posted this in the dotnet.framework group with no responce yet, maybe someone in these groups can point me in the right direction? Or maybe I need to use something already in .Net to get an IGMP...
8
1350
by: Pete | last post by:
Assume I have an n by n matrix of identical objects, each of which can both invoke and consume event A. If, say, obect invokes the event, the only other objects that need to consume it are those in...
0
1084
by: POgletree | last post by:
I need to set up a small client/server or, best solution, a peer-to-peer system to swap data strings between wide spread users. I've looked at IP multicasting and it seems like a reasonable...
9
5641
by: Sayudh27 | last post by:
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...
1
4659
by: Nick | last post by:
Does any one have a simple wcf of udp multicating other than the one posted here http://wcf.netfx3.com/files/folders/transport_channels/entry5235.aspx I would like a program with just the basics...
0
7041
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
6737
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6921
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5336
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4776
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4481
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2984
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
563
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
179
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.