Find net Mask : Net Mask « Network « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# / C Sharp » Network » Net MaskScreenshots 
Find net Mask
Find net Mask

/*
C# Network Programming 
by Richard Blum

Publisher: Sybex 
ISBN: 0782141765
*/
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

public class FindMask
{
   public static void Main()
   {
      byte[] data = new byte[1024];
      int recv;
      Socket host = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Icmp);
      IPEndPoint iep = new IPEndPoint(IPAddress.Broadcast, 0);
      EndPoint ep = (EndPoint)iep;
      ICMP packet = new ICMP();

      packet.Type = 0x11;
      packet.Code = 0x00;
      packet.Checksum = 0;
      Buffer.BlockCopy(BitConverter.GetBytes(1)0, packet.Message, 02);
      Buffer.BlockCopy(BitConverter.GetBytes(1)0, packet.Message, 22);
      Buffer.BlockCopy(BitConverter.GetBytes(0)0, packet.Message, 44);
      packet.MessageSize = 8;
      int packetsize = packet.MessageSize + 4;

      UInt16 chksm = packet.getChecksum();
      packet.Checksum = chksm;

      host.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 3000);
      host.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
      host.SendTo(packet.getBytes(), packetsize, SocketFlags.None, iep);
      try
      {
         data = new byte[1024];
         recv = host.ReceiveFrom(data, ref ep);
      catch (SocketException)
      {
         Console.WriteLine("Unable to determine subnet mask for this subnet");
         return;
      }
      ICMP response = new ICMP(data, recv);
      Console.WriteLine("Received ICMP Type {0} packet", response.Type);
      long answer = BitConverter.ToUInt32(response.Message, 4);
      IPAddress netmask = new IPAddress(answer);
      Console.WriteLine("The subnet mask for this subnet is: {0}", netmask.ToString());
   }
}
class ICMP
{
   public byte Type;
   public byte Code;
   public UInt16 Checksum;
   public int MessageSize;
   public byte[] Message = new byte[1024];

   public ICMP()
   {
   }

   public ICMP(byte[] data, int size)
   {
      Type = data[20];
      Code = data[21];
      Checksum = BitConverter.ToUInt16(data, 22);
      MessageSize = size - 24;
      Buffer.BlockCopy(data, 24, Message, 0, MessageSize);
   }

   public byte[] getBytes()
   {
      byte[] data = new byte[MessageSize + 9];
      Buffer.BlockCopy(BitConverter.GetBytes(Type)0, data, 01);
      Buffer.BlockCopy(BitConverter.GetBytes(Code)0, data, 11);
      Buffer.BlockCopy(BitConverter.GetBytes(Checksum)0, data, 22);
      Buffer.BlockCopy(Message, 0, data, 4, MessageSize);
      return data;
   }

   public UInt16 getChecksum()
   {
      UInt32 chcksm = 0;
      byte[] data = getBytes();
      int packetsize = MessageSize + 8;
      int index = 0;

      while index < packetsize)
      {
         chcksm += Convert.ToUInt32(BitConverter.ToUInt16(data, index));
         index += 2;
      }
      chcksm = (chcksm >> 16(chcksm & 0xffff);
      chcksm += (chcksm >> 16);
      return (UInt16)(~chcksm);
   }
}
           
       
Related examples in the same category
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.