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
- using System; // For Console and Exception classes
- using System.Net; // For IPAddress class
- using System.Net.Sockets; // For Socket class
- using System.Runtime.InteropServices;
- using System.IO.Compression;
- using ManagedQLZ;
- using System.IO;
- //using Simplicit.Net.Lzo;
- //enum IN_LEN; //:long {Max = 1024L, Min = 128};
- //enum OUT_LEN; //:long{IN_LEN + IN_LEN / 64 + 16 + 3};
- class MCReceive
- {
- const int MIN_PORT = 1024; // min port value
- const int MAX_PORT = 65535; // max port value
- unsafe static void Main()
- {
- //string str = "233.1.2.4";
- //string port = "6755";
- string str = "225.0.0.36";
- string port = "15000";
- Socket sock; // Multicast socket
- IPAddress mcIP; // Multicast group to join
- int mcPort; // Port to receive on
- IPEndPoint receivePoint; // IP end point
- int MAX_LEN = 1024; // Max receive buffer size
- Boolean done = false; // loop variable
- //int r;
- /*lzo_byte* inData;
- lzo_byte* wrkmem;
- lzo_uint inData_len;
- lzo_uint recData_len;
- lzo_uint new_len;*/
- Console.Error.WriteLine("Usage: MCReceive " +
- "<Multicast IP> <Multicast Port>");
- mcIP = IPAddress.Parse(str);
- mcPort = Int32.Parse(port);
- if ((mcPort < MIN_PORT) || (mcPort > MAX_PORT))
- {
- Console.Error.WriteLine("Invalid Port specified.");
- Console.Error.WriteLine("Port must be between " + MIN_PORT
- + " and " + MAX_PORT);
- return;
- }
- //try
- {
- // Create the Socket
- sock = new Socket(AddressFamily.InterNetwork,
- SocketType.Dgram, ProtocolType.Udp);
- //ProtocolType.Udp);
- // Set the reuse address option
- sock.SetSocketOption(SocketOptionLevel.Socket,
- SocketOptionName.ReuseAddress, 1);
- // Create an IPEndPoint and bind to it
- IPEndPoint ipep = new IPEndPoint(IPAddress.Any, mcPort);
- sock.Bind(ipep);
- // Add membership in the multicast group
- sock.SetSocketOption(SocketOptionLevel.IP,
- SocketOptionName.AddMembership,
- new MulticastOption(mcIP, IPAddress.Any));
- // Create the EndPoint class
- receivePoint = new IPEndPoint(IPAddress.Any, 0);
- EndPoint tempReceivePoint = (EndPoint)receivePoint;
- string path = "C:\\decompress.txt";
- StreamWriter writer = File.AppendText(@path);
- //string OutputDecompress;
- //int i = 0;
- byte[] recData = new byte[MAX_LEN];
- byte[] Outdata = new byte[MAX_LEN];
- //while (!done && i<=4)
- while (!done )
- {
- // Receive the multicast packets
- int length = sock.ReceiveFrom(recData, 0, MAX_LEN,
- SocketFlags.None,
- ref tempReceivePoint);
- //foreach(int i in recData )
- //Console.WriteLine(recData[i].ToString());
- // Format and output the received data packet
- System.Text.ASCIIEncoding encode =
- new System.Text.ASCIIEncoding();
- Console.WriteLine("Received " + length + " bytes from " +
- tempReceivePoint.ToString() + ": "); //+encode.GetString(recData, 0, length));
- byte[] compressed = QuickLZ.Compress(recData, 0, (UInt32)recData.Length);
- byte[] decompressed = QuickLZ.Decompress(compressed, 0);
- // LZOCompressor lzo1 = new LZOCompressor();
- //byte[] decompressed = lzo1.Decompress(recData);
- // writer.WriteLine(recData[100]);
- //Outdata = Compress.Decompress1(recData);
- writer.WriteLine("Received " + length + " bytes from " +
- tempReceivePoint.ToString() + ": ------------------");
- System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
- //string OutputDecompress = enc.GetString(decompressed);
- string OutputDecompress = enc.GetString(decompressed);
- writer.WriteLine(OutputDecompress);
- //System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
- writer.WriteLine("----------------");
- foreach (int j in decompressed)
- {
- //OutputDecompress = enc.GetString(decompressed[j]);
- writer.Write(decompressed[j]+" ");
- //writer.Write(decompressed + " ");
- }
- //Console.WriteLine(decompressed[j]);
- //Console.WriteLine(Outdata[0]);
- //Console.WriteLine(recData[101]);
- }
- //Compress decom = new Compress();
- //writer.Close();
- //Console.WriteLine(Outdata);
- //writer.WriteLine(Outdata);
- writer.Close();
- //StreamWriter writer = File.AppendText(@path);
- ////Test In txt File
- //Set the enable property to false.
- //Append the text Tick to the C:\sample.txt file.
- //writer.WriteLine(Outdata);
- //end///Test In txt File
- //Decompress
- ///End Decompress
- /*//Step 1:initialise LZO library
- if (lzo_init() != LZO_E_OK)
- {
- printf("lzo_init() failed !!!\n");
- }
- //Step 2: allocate blocks and the work-memory
- inData = lzo_malloc(1024);
- recData = lzo_malloc(2048);
- wrkmem = lzo_malloc(LZO1X_1_MEM_COMPRESS);
- if (inData == NULL || recData == NULL || wrkmem == NULL)
- {
- printf("out of memory\n");
- //return 1;
- }
- inData_len = IN_LEN;
- // Step 3: decompress now going from `recData' to `inData'
- r = lzo1x_decompress(recData, recData_len, inData, &new_len, NULL);
- if (r == LZO_E_OK && new_len == inData_len)
- printf("decompressed %lu bytes back into %lu bytes\n",
- (long)recData_len, (long)inData_len);
- else
- {
- /* this should NEVER happen
- printf("internal error - decompression failed: %d\n", r);
- return 1;
- }
- lzo_free(wrkmem);
- lzo_free(inData);
- lzo_free(recData);
- printf("Simple compression test passed.\n");
- return 0;*/
- // Drop membership
- sock.SetSocketOption(SocketOptionLevel.IP,
- SocketOptionName.DropMembership,
- new MulticastOption(mcIP,
- IPAddress.Any));
- // Close the socket
- sock.Close();
- }
- //catch (SocketException se)
- //{
- // Console.WriteLine("Socket Exception: " + se.ToString());
- // return;
- //}
- //catch (Exception e)
- //{
- // Console.Error.WriteLine("Exception: " + e.ToString());
- // return;
- //}
- }
- // return 1;
- }
Where am I going wrong?
Any help will be really appreciated...
Cheers..