Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. [mfc] ip address control

[mfc] ip address control

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
9 Posts 4 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rahuljin
    wrote on last edited by
    #1

    hello, i want to set the value of the ip address control from a string, say ip[100] which is read from a text file. how this can be done ? the text file contains ip like --- 127.0.0.1 or 111.111.11.1

    A M 2 Replies Last reply
    0
    • R rahuljin

      hello, i want to set the value of the ip address control from a string, say ip[100] which is read from a text file. how this can be done ? the text file contains ip like --- 127.0.0.1 or 111.111.11.1

      A Offline
      A Offline
      Adam Roderick J
      wrote on last edited by
      #2

      ULONG ulP = inet_addr(szIPAddress );

      R 1 Reply Last reply
      0
      • A Adam Roderick J

        ULONG ulP = inet_addr(szIPAddress );

        R Offline
        R Offline
        rahuljin
        wrote on last edited by
        #3

        i dont get, please explain. i am using this code but it makes the mfc application crash.

        len = strlen(fff);
        
        if(strlen(fff) > 0 && strcmp(fff, "\\n") != 0)
        {
        	while(fff\[i\] != '.')
        	{
        		c\[j\] = fff\[i\];
        		j++;
        		i++;
        	}
        	m1 = atoi(c);
        	j = 0;
        	i++;
        	ZeroMemory(c, 3);
        
        	while(fff\[i\] != '.')
        	{
        		c\[j\] = fff\[i\];
        		j++;
        		i++;
        	}
        	m2 = atoi(c);
        	j = 0;
        	i++;
        	ZeroMemory(c, 3);
        
        	while(fff\[i\] != '.')
        	{
        		c\[j\] = fff\[i\];
        		j++;
        		i++;
        	}
        	m3 = atoi(c);
        	j = 0;
        	i++;
        	ZeroMemory(c, 3);
        
        	while(len > i)
        	{
        		c\[j\] = fff\[i\];
        		j++;
        		i++;
        	}
        	m4 = atoi(c);
        	j = 0;
        	i++;
        	ZeroMemory(c, 3);
        	ipAdd.SetAddress(m1, m2, m3, m4);
        }
        
        B 1 Reply Last reply
        0
        • R rahuljin

          hello, i want to set the value of the ip address control from a string, say ip[100] which is read from a text file. how this can be done ? the text file contains ip like --- 127.0.0.1 or 111.111.11.1

          M Offline
          M Offline
          Maximilien
          wrote on last edited by
          #4

          I assume you are talking about CIPAddressCtrl. The method CIPAddressCtrl::SetAddress take 4 BYTE parameters (see : doc on MSDN[^]) So, you will need to extract each "part" from the string and convert each substring to a BYTE. (manually or int_addr[^])

          This signature was proudly tested on animals.

          1 Reply Last reply
          0
          • R rahuljin

            i dont get, please explain. i am using this code but it makes the mfc application crash.

            len = strlen(fff);
            
            if(strlen(fff) > 0 && strcmp(fff, "\\n") != 0)
            {
            	while(fff\[i\] != '.')
            	{
            		c\[j\] = fff\[i\];
            		j++;
            		i++;
            	}
            	m1 = atoi(c);
            	j = 0;
            	i++;
            	ZeroMemory(c, 3);
            
            	while(fff\[i\] != '.')
            	{
            		c\[j\] = fff\[i\];
            		j++;
            		i++;
            	}
            	m2 = atoi(c);
            	j = 0;
            	i++;
            	ZeroMemory(c, 3);
            
            	while(fff\[i\] != '.')
            	{
            		c\[j\] = fff\[i\];
            		j++;
            		i++;
            	}
            	m3 = atoi(c);
            	j = 0;
            	i++;
            	ZeroMemory(c, 3);
            
            	while(len > i)
            	{
            		c\[j\] = fff\[i\];
            		j++;
            		i++;
            	}
            	m4 = atoi(c);
            	j = 0;
            	i++;
            	ZeroMemory(c, 3);
            	ipAdd.SetAddress(m1, m2, m3, m4);
            }
            
            B Offline
            B Offline
            bolivar123
            wrote on last edited by
            #5

            You may be over complicating the conversion of the 4 number parts of the IP address. Try this instead:

            int m1,m2,m3,m4;
            
            sscanf(fff,"%d.%d.%d.%d",&m1,&m2,&m3,&m4);
            
            ipAdd.SetAddress(m1,m2,m3,m4);
            

            Of course, you'll need to validate the IP address is in the proper format.

            R 1 Reply Last reply
            0
            • B bolivar123

              You may be over complicating the conversion of the 4 number parts of the IP address. Try this instead:

              int m1,m2,m3,m4;
              
              sscanf(fff,"%d.%d.%d.%d",&m1,&m2,&m3,&m4);
              
              ipAdd.SetAddress(m1,m2,m3,m4);
              

              Of course, you'll need to validate the IP address is in the proper format.

              R Offline
              R Offline
              rahuljin
              wrote on last edited by
              #6

              this method shows wrong ip in the ip address control.

              B 1 Reply Last reply
              0
              • R rahuljin

                this method shows wrong ip in the ip address control.

                B Offline
                B Offline
                bolivar123
                wrote on last edited by
                #7

                The variable fff must be a string only containing a valid IP address. try this:

                LPSTR lpIPAddress = _T("127.0.0.1");
                int m1, m2, m3, m4;
                sscanf(lpIPAddress,"%d.%d.%d.%d",&m1,&m2,&m3,&m4);
                ipAdd.SetAddress(m1,m2,m3,m4);

                //You should now see 127.0.0.1 in the IPAddress control

                Make sure you're passing in just the IP address in the sscanf function.

                R 1 Reply Last reply
                0
                • B bolivar123

                  The variable fff must be a string only containing a valid IP address. try this:

                  LPSTR lpIPAddress = _T("127.0.0.1");
                  int m1, m2, m3, m4;
                  sscanf(lpIPAddress,"%d.%d.%d.%d",&m1,&m2,&m3,&m4);
                  ipAdd.SetAddress(m1,m2,m3,m4);

                  //You should now see 127.0.0.1 in the IPAddress control

                  Make sure you're passing in just the IP address in the sscanf function.

                  R Offline
                  R Offline
                  rahuljin
                  wrote on last edited by
                  #8

                  it is working now. since i am using

                  fileh.ReadString((LPTSTR)fff , 100);

                  therefore i have to use the following code for the correct result ---

                  swscanf((LPCTSTR)fff,_T("%d.%d.%d.%d"),&m1,&m2,&m3,&m4);

                  thanks for the help.

                  B 1 Reply Last reply
                  0
                  • R rahuljin

                    it is working now. since i am using

                    fileh.ReadString((LPTSTR)fff , 100);

                    therefore i have to use the following code for the correct result ---

                    swscanf((LPCTSTR)fff,_T("%d.%d.%d.%d"),&m1,&m2,&m3,&m4);

                    thanks for the help.

                    B Offline
                    B Offline
                    bolivar123
                    wrote on last edited by
                    #9

                    You're welcome! Glad you got it working. :-D

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups