【发布时间】:2015-08-14 10:11:08
【问题描述】:
我正在编写其他人的代码,该代码是为某些套接字编程而编写的。该项目有以下两个文件。
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对其进行了转换。但是当我 build 它时,它会给出以下错误。
错误 C2373:“inet_addr”:重新定义;不同的类型修饰符
错误 C2373:'htons':重新定义;不同的类型修饰符
我试图找到解决方案,但不知道该怎么做。我对此了解不多。
编辑:此代码不使用 #include Winsock2.h。我检查了几个在线解决方案,声称这个库是重新定义的原因,但在这种情况下并非如此。
【问题讨论】:
标签: c++ sockets visual-c++ visual-studio-2013