Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

IP Address, Port Number, Subnet Mask Submission Form Library

0.00/5 (No votes)
18 Mar 2003 1  
A class library for an easy submission of IP Address, Port Number and Subnet Mask

Sample Image

Introduction

This class library is providing an easy and flexible way to submit ip address, port number and subnet mask. I believe such a submission dialog is needed in many applications - and I hope it can also help you because it provides different dialogs for different needs.

Different dialogs for different needs

  1. IP Submission Dialog
  2. IP Submission + Port Number Dialog
  3. IP Submission + Port Number + Subnet Mask Dialog
  4. IP Submission + Port Number + Subnet Mask + Local network adapters Dialog


Constructor Call 1: IP Address
Constructor Call 2: IP Address + Port
Constructor Call 3: IP Address, Port, Subnet Mask
Constructor Call 4: IP Address, Port, Subnet Mask, Local network adapters

Setting up the form

As you can see in the constructor calls below all important text components in the dialog can be named individually.

These Form Components are:

  • Title
  • Information message for user
  • OK Button
  • Cancel Button
  • (and when calling dialog 4. another textfield free for use).

Sample constructor calls:

// Example for 1.)


FrmCommon.FrmIPSubnet i = new FrmCommon.FrmIPSubnet(" IP Address", 
    "Please enter ip address.", "OK", "Cancel", "192.168.0.1");

// Example for 2.)

FrmCommon.FrmIPSubnet i = new FrmCommon.FrmIPSubnet(" IP Address",
    "Please enter ip address and port number.", "OK", "Cancel",
  "192.168.0.1", "55521");

// Example for 3.)

FrmCommon.FrmIPSubnet i = new FrmCommon.FrmIPSubnet(" IP Address",
    "Please enter ip address, port number and subnet mask.", "OK",
  "Cancel", "192.168.0.1", "55521", "255.255.255.0");

// Example for 4.)

FrmCommon.FrmIPSubnet i = new FrmCommon.FrmIPSubnet(" IP Address",
"Please enter your IP Address and Port number. The subnet mask and " +
"informations about local adapters are not transferred.",
"OK", "Cancel", "192.168.0.1", "55521", "255.255.255.0",
"In order to connect to client the entered subnet mask must match " +
"your local one.");

The arguments used when calling the constructor affect the type and look of the form. For example: if you're calling the form with constructor number one you just expect the ip address to be entered and so - only the GUI components regarding this are shown.

Getting the values

Getting the entered values back is as simple as setting them. Every IP submission dialog has the properties:

  • IPAddress
  • Port
  • SubnetMask

Every property can be used for setting and getting the properties. Because these values are initialized when calling the constructor, setting them shouldn't be important for you (only if you want to change the value afterwards).

The values are set and returned in the form (without quotes):

IPAddress : "192.168.0.1"
Port : "55521"
SubnetMask : "255.255.255.0"

If you are using unallowed values (e.g. letters, numbers of of range) the standard value will be used. Those standard values are:

IPAddress: "192.168.0.1"
Port: "1"
SubnetMask: "255.255.255.0"

Sample form call:

// Example for 3.)



// Initializing variables


string ipAddress;

string port;

string subnetMask;


// Calling form and constructor


FrmCommon.FrmIPSubnet i = new FrmCommon.FrmIPSubnet(" IP Address",
    "Please enter ip address, port number and subnet mask.", "OK",
  "Cancel", "192.168.0.1", "55521", "255.255.255.0");

// Shows the dialog

i.ShowDialog();

// Store the entered values in the local variables

ipAddress = i.IPAddress;
port = i.Port;
subnetMask = i.SubnetMask;

Internal logic

Of course the form should decide whether or not the entered values are valid. Because its a bit tricky (and you might want to customize the library I provided) - here is the internal logic:

General

The ip address and the subnet mask contain of 4 blocks:
Examples:

Examples:    
  IP: 192.168.0.1
  Subnet Mask: 255.255.255.0

Conditions for IP Address

1. Value range for block one must be between 1 and 223
2. Value range for blocks two-four must be between 0 and 255

Network class Binary Decimal Subnet mask
A 0xxx xxxx 0 ... 127 255.0.0.0
B 10xx xxxx 128 ... 191 255.255.0.0
C 110x xxxx 192 ... 223 255.255.255.0
D 1110 xxxx 224 ... 239 - (Multicast addresses)
E 1111 xxxx 240 ... 255 - (Experimental addresses)

Private IP Address range:
10.x.x.x
172.x.x.x
192.168.x.x

Conditions for Subnet Mask

1. Only values 0, 128, 192, 224, 240, 248, 252, 254, 255 are allowed in all blocks
  Example: 255.128.0.0 allowed, 255.17.0.0 not allowed
   
2. Is a block containing a value unequal to 0, all blocks on the left side thereof must have the value 255.
  Example: 255.255.248.0 allowed, 192.255.248.0 not allowed
   
3. Is a block containing a value unequal to 255, all blocks on the right side thereof must have the value 0.
  Example: 224.0.0.0 allowed, 224.0.0.128 not allowed
   
4. In the first block the value 0 is not allowed
  In the fourth block the values 254 and 255 are not allowed
  Example: 0.0.0.0 / 255.255.255.254 / 255.255.255.255 not allowed

The subnet mask will be compared to the ip address. Depending on the first block 'AA' of the ip
address (AA.xx.xx.xx) the subnet mask is subject to restrictions.

0 < AA < 127 Subnet mask must contain at least 8 leading one's
  Example: 255.0.0.0 / 255.128.0.0 allowed, 254.0.0.0 / 192.0.0.0 not allowed
   
128 < AA < 191 Subnet mask must contain at least 16 leading one's
  Example: 255.255.0.0 / 255.255.224.0 allowed, 255.240.0.0 / 255.0.0.0 not allowed
   
129 < AA < 255 Subnet mask must contain at least 24 leading one's
  Example: 255.255.255.0 / 255.255.255.248 allowed, 255.252.0.0 / 255.255.254.0 not allowed

 

History

19. March 2003: Initial Release

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here