Parse IP addresses

Download Source Code and Example


Very often, if you write Internet applications, you need to verify IP address user entered and make sure that it's correct. So lets see how we can do that without a lot of troubles.
First of all we need to add declarations of the following function to header file of the dialog.

#include <winsock.h>

void WINAPI DDX_IPAddress(CDataExchange* pDX, int nIDC, UINT & value);
To implement data exchange you need to call DDX_IPAddress function from within your dialog DoDataExchange function
Now add following code to the implementation file of the dialog, and do not forget to include ws2_32.lib into the project or you'll get a lot of linking errors.
// DDX routine fo IP address translation
void WINAPI DDX_IPAddress(CDataExchange* pDX, int nIDC, UINT & value)
{
	// from dialog to class ?
	if( pDX->m_bSaveAndValidate)
	{
		CString Val;
		BOOL bValid = true;

		pDX->m_pDlgWnd->GetDlgItem(nIDC)->GetWindowText(Val);

		for( int i = 0; i < Val.GetLength(); i++)
		{
			// let's check if all entered char in entered
			// IP address are digits
			if(Val[i] == '.')
				continue;

			if(isdigit(Val[i]) == 0)
			{
				bValid = false;
				break;			
			}
		}

		if(bValid)
		{
			value = inet_addr(Val);
			if(value == INADDR_NONE)
			{
				pDX->m_pDlgWnd->MessageBox("The entered IP address is invalid.");
				pDX->PrepareEditCtrl(nIDC);
				pDX->Fail();
			}
		}
		else
		{
			pDX->m_pDlgWnd->MessageBox("IP address can only have digits and dots.");
			pDX->PrepareEditCtrl(nIDC);
			pDX->Fail();
		}
	}
	else
	{
		// if the value is a valid IP address store it in the child control
		in_addr IPaddress;
		memcpy(&IPaddress, &value, 4);
		CString Address = inet_ntoa(IPaddress);
		if(!Address.IsEmpty())
		{
			pDX->m_pDlgWnd->GetDlgItem(nIDC)->SetWindowText(Address);
		}
	}	
}

Last updated: 14 May 1998



Comments

  • Quicker way? Try this..

    Posted by Legacy on 04/06/1999 12:00am

    Originally posted by: Chris Morse Sebrell


    The sscanf() function works well too:

    // Assign Address at some point..
    CString Address = "20.15.7.4";

    // Parse Address
    int IP[4];
    if(sscanf((LPCTSTR) Address, "%d.%d.%d.%d", &IP[0], &IP[1], &IP[2], &IP[3]) == 4)
    {
    // Now IP[0] = 20, IP[1] = 15, IP[2] = 7, IP[3] = 4...
    SendUpdateMessageUDP(IP, "Programming Is Great!");
    }

    // CHRIS!
    mailto:win32mfc@home.com

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds