Click here to Skip to main content
5,787,682 members and growing! (18,270 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » .NET Framework » General     Beginner License: A Public Domain dedication

Calculate the Broadcastaddress for Subnet

By Daniel Wernle

Calculate the Broadcastaddress for Subnet
C#, .NET, Dev

Posted: 22 Aug 2008
Updated: 22 Aug 2008
Views: 1,542
Bookmarked: 2 times
Note: This is an unedited reader contribution
Announcements
Loading...



Search    
Advanced Search
Sitemap
2 votes for this Article.
Popularity: 0.30 Rating: 1.00 out of 5
2 votes, 100.0%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
0 votes, 0.0%
4
0 votes, 0.0%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

This code returns the Subnet-Broadcast-Address for a given IP-Address and Subnetmask.

I needed it for a WOL Funktion on a website which was in an different subnet and where the standard broadcastaddress didn't worked out.

Using the code

This function is quite easy and could be used for example in an UdpClient Object like this.

 UdpClient client = new UdpClient();
 client.Connect(CalculateBroadCastAddress(ip,nm).ToString(), 40000); 

Code:

using System.Net.Sockets;
using System.Net;
using System.Globalization;
using System.Collections;

           
        static IPAddress CalculateBroadCastAddress(IPAddress currentIP, IPAddress ipNetMask)
        {
            string[] strCurrentIP = currentIP.ToString().Split('.');
            string[] strIPNetMask = ipNetMask.ToString().Split('.');

            ArrayList arBroadCast = new ArrayList();

            for (int i = 0; i < 4; i++)
            {
                int nrBCOct = int.Parse(strCurrentIP[i]) | (int.Parse(strIPNetMask[i]) ^ 255);
                arBroadCast.Add(nrBCOct.ToString());
            }
            return IPAddress.Parse(arBroadCast[0] + "." + arBroadCast[1] + "." + arBroadCast[2] + "." + arBroadCast[3]);
        }         

History

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication

About the Author

Daniel Wernle


.. was born
.. grew up
Occupation: Software Developer
Company: Infineon Technologies
Location: Austria Austria

Other popular .NET Framework articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 1 of 1 (Total in Forum: 1) (Refresh)FirstPrevNext
GeneralMuch better solutionmemberMichal Brylka13:25 22 Aug '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 22 Aug 2008
Editor:
Copyright 2008 by Daniel Wernle
Everything else Copyright © CodeProject, 1999-2009
Web12 | Advertise on the Code Project