2015-08-14 40 views
0

我正在编写某人的其他代码,这是为某些套接字编程编写的。这个项目有以下两个文件。错误C2373:'inet_addr':重新定义;不同类型的修饰符

SOCKUTIL.H

#if !defined(SOCKUTIL_H) 
#define SOCKUTIL_H 

unsigned long inet_addr(const char *sIp); 

unsigned short htons(unsigned short port); 
#endif 

sockUtil.cpp

#include "stdafx.h" 
#include "sockutil.h" 
#include <stdlib.h> 
#include <string.h> 

unsigned long inet_addr(const char *sIp) 
{ 
    int    octets[4]; 
    int    i; 
    const char  *auxCad = sIp; 
    unsigned long lIp = 0; 

    //we extract each octet of the ip address 
    //atoi will get characters until it found a non numeric character(in our case '.') 
    for(i = 0; i < 4; i++) 
    { 
     octets[i] = atoi(auxCad); 

     if(octets[i] < 0 || octets[i] > 255) 
     { 
      return(0); 
     } 

     lIp |= (octets[i] << (i * 8)); 

     //update auxCad to point to the next octet 
     auxCad = strchr(auxCad, '.'); 

     if(auxCad == NULL && i != 3) 
     { 
      return(0); 
     } 

     auxCad++; 
    } 

    return(lIp); 
} 

unsigned short htons(unsigned short port) 
{ 
    unsigned short portRet; 

    portRet = ((port << 8) | (port >> 8)); 

    return(portRet); 
} 

该项目最初是在VC6开发,当我在VS2013打开它时,Visual Studio将其转换。但是当我建立它,那么它给出以下错误。

错误C2373:'inet_addr':重新定义;不同类型的修饰符

错误C2373:'htons':重新定义;不同类型的修饰符

我试图找到解决方案,但没有得到该怎么做。我对此没有太多的了解。

编辑:此代码不使用#include Winsock2.h。我查了几个可用的在线解决方案,声称这个库是重新定义的原因,但在这种情况下这不是真的。

回答

2

这些功能在最近版本的Visual Studio已经为你定义(见:MSDN) - 你可以从你的项目中删除这些文件,并删除所有出现:

#include "sockutil.h" 
+0

谢谢,它帮助。 –

相关问题