FILE EXISTS error on setting IPV4 address using ioctl

I am getting a File exists error on using the SIOCSIFADDR ioctl command to set IP address ,but the IP address is changed
to the requested value. Why the error occurs?

Code snippet:

bool setIpv4Address(std::string interfaceName,std::string ipAddr)
{
struct sockaddr_in sin;
int sockFd;
std::string Ip;
struct ifreq ifr;
bool retStatus;

memset(&ifr, 0x00, sizeof(ifr));
memset(&sin, 0x00, sizeof(sin));
retStatus = true;

sin.sin_family = AF_INET;
sin.sin_len = sizeof(sin);
/// check valid ip
if (inet_aton(&ipAddr[0], &sin.sin_addr) == 0)
{
retStatus = false;
}
else
{
/// Create socket for INET and update the address information.
memset(&ifr, 0x00, sizeof(ifr));
strcpy(ifr.ifr_name, &interfaceName[0]);
memcpy(&ifr.ifr_addr, &sin, sizeof(ifr.ifr_addr));
if ((sockFd = socket(AF_INET, SOCK_DGRAM, 0)) != -1)
{

/// Set IP address using SIOCSIFADDR ioctl flag.
if (ioctl(sockFd, SIOCSIFADDR, &ifr) != -1)
{
/// Set Interface Up
/// Get ifreq.flags
ioctl(sockFd, SIOCGIFFLAGS, &ifr);

ifr.ifr_flags |= (IFF_UP| IFF_RUNNING);

if(ioctl(sockFd, SIOCSIFFLAGS, &ifr) == -1)
{
perror(“Interface link status change failed”);
retStatus = false;
}
}
else
{
perror(“setIpv4Address Setting IPV4 address Error :”);
retStatus = false;
}
}
else
{
perror("Socket creation failed ");
retStatus = false;
}
close (sockFd);
}

return retStatus;
}

  1. Use code tags to make your code readable.

  2. Never use this kind of writing

if ((sockFd = socket(AF_INET, SOCK_DGRAM, 0)) != -1)

Instead, use

sockFd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockFd != -1)
  1. Add printf() to precisely trace the execution of you program.

  2. Report the trace output here.