Thread: if != inet_aton() then ?

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    9

    if != inet_aton() then ?

    hi everyone..

    i`m new to socket programming using C. i`ve made a client and been given the server code. what i would like to do is to accept and validate IP address..

    now i`ve been following Beej's Guide and several other online resources and did search this forum.. almost all these guides suggest to use inet_ntoa() and inet_aton() .. the thing is i`m forced to use VC++ for my C assignment and the header files are not available in the VC IDE.

    i would greatly appreciate it if anyone of you could tell me what header file i could use(with VC++ ofcourse) . or suggest any other alternative that would help me to validate an IP address.

    thankyou very much for your time.
    Last edited by madcat23; 04-25-2006 at 06:53 PM.

  2. #2
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Are you sure? For Windows you should include winsock.h.

    Otherwise you could make a simple inet_aton using sscanf.
    Code:
    int inet_aton (char* addr, struct in_addr* dest)
    {
    	int a[4];
    	if (sscanf(addr, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]) != 4)
    		return 0;
    	dest->s_addr = a[3] | a[2]<<8 | a[1]<<16 | a[0]<<24;
    	return 1;
    }
    This one will only allow addresses on the form x.x.x.x and there are lots of other flaws too but it should work as long as the input is correct.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    9
    wow that sure works like a charm !! thankyou

    i did more research about it .. and it seems winsock.h has inet_addr in place of inet_aton .. but inet_addr is a bit old and has a few issues .. like not accepting 255.255.255.255 ..

    but winsock does have inet_ntoa() .. soo i`m sorted thanks again OnionKnight ..

    (jumping up and down)
    Last edited by madcat23; 04-27-2006 at 04:17 PM.

Popular pages Recent additions subscribe to a feed