CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2005
    Posts
    49

    question about subnetmask

    is there any method in C# to which i pass ip address and subnetmask and it return true if given ip is in subnet mask?

    For example, i give ip 192.168.0.149 and subnetmask 192.168.0.0 to the funtion and it return true................... Is there some thing like this in C#
    Muhammad Waqas Badar

  2. #2
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: question about subnetmask

    If you have the IP (v4) address stored in a long....
    Code:
    bool match = (addr & mask) == mask;
    If you have it in a byte array..
    Code:
    bool match = (addr[0] & mask[0]) == mask[0]) &&
                         (addr[1] & mask[1]) == mask[1]) &&
                         (addr[2] & mask[2]) == mask[2]) &&
                         (addr[3] & mask[3]) == mask[3]);
    If you have it in a string.... DONT! Change your design.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured