Click here to Skip to main content
6,304,948 members and growing! (18,906 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
Views:3,294
Bookmarked:2 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
3 votes for this article.
Popularity: 0.48 Rating: 1.00 out of 5
3 votes, 100.0%
1

2

3

4

5

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


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

Other popular .NET Framework articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 2 of 2 (Total in Forum: 2) (Refresh)FirstPrevNext
GeneralMy vote of 1 PinmemberAcoustic11:15 5 Feb '09  
GeneralMuch better solution PinmemberMichal 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
Web10 | Advertise on the Code Project