Posts tagged How to validate an IP address in string format
How to extract IP address from a string in C
Nov 5th
The library function inet_aton can be used to extract IP address from a string format (eg: “123.23.42.76″). This function returns non-zero value if the IP address is valid and zero if it is an invalid IP address. The following example shows how to use this library function.
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
char *ips[] = {
"12.34.56.7",
"123.123.123.123",
"255.255.255.255",
"345.123.23.54",
NULL
};
int main(int argc, char *argv[])
{
int i;
struct in_addr ia;
i = 0;
while(ips[i]) {
if(inet_aton(ips[i],&ia) == 0) {
printf("%s is an INVALID ip address\n", ips[i]);
} else {
printf("%s is a VALID ip address. 0x%lx\n", More > 

Recent Comments