1、网络相关
linus-------netdb.h(网络数据库 database)
Unix和Linux特有的头文件,主要定义了与网络有关的结构、变量类型、宏、函数等。
以下参考:
搞了几个跨Linux与Windows的项目,碰到很多问题,记下来,以供查考。另外,因为水平的原因,肯定错误在所难免,请读者一定指正。
如无其它说明,本文所指Linux均表示2.6内核Linux,GCC编译器,Windows均表示Windows XP系统,Visual Studio 2005 sp1编译环境。
下面大概分几个方面进行罗列:
socket
Linux要包含
<div class="dp-highlighter bg_cpp" style="word-wrap: break-word; font-family: tahoma, 宋体; line-height: 1.6;"><div class="bar" style="word-wrap: break-word; line-height: 1.6;"><div class="tools" style="word-wrap: break-word; line-height: 1.6;"><strong>[cpp]</strong> </div></div><ol class="dp-cpp"><li class="alt"><span class="preprocessor"><span style="color:#808080;">#include <sys/socket.h> </span></span><span style="color:#000000;"> </span></li><li><span class="preprocessor"><span style="color:#808080;">#include <netinet/in.h> </span></span><span style="color:#000000;"> </span></li><li class="alt"><span class="preprocessor"><span style="color:#808080;">#include <netdb.h> </span></span><span style="color:#000000;"> </span></li><li><span class="preprocessor"><span style="color:#808080;">#include <arpa/inet.h></span></span><span style="color:#000000;"> </span></li></ol></div>等头文件,而windows下则是包含
<div class="dp-highlighter bg_cpp" style="word-wrap: break-word; font-family: tahoma, 宋体; line-height: 1.6;"><div class="bar" style="word-wrap: break-word; line-height: 1.6;"><div class="tools" style="word-wrap: break-word; line-height: 1.6;"><strong>[cpp]</strong> </div></div><ol class="dp-cpp"><li class="alt"><span class="preprocessor"><span style="color:#808080;">#include <winsock.h></span></span><span style="color:#000000;"> </span></li></ol></div>
。
Linux中socket为整形,Windows中为一个SOCKET。
Linux中关闭socket为close,Windows中为closesocket。
Linux中有变量socklen_t,Windows中直接为int。
因为linux中的socket与普通的fd一样,所以可以在TCP的socket中,发送与接收数据时,直接使用read和write。而windows只能使用recv和send。
设置socet选项,比如设置socket为非阻塞的。Linux下为
<div class="dp-highlighter bg_cpp" style="word-wrap: break-word; font-family: tahoma, 宋体; line-height: 1.6;"><div class="bar" style="word-wrap: break-word; line-height: 1.6;"><div class="tools" style="word-wrap: break-word; line-height: 1.6;"><strong>[cpp]</strong> </div></div><ol class="dp-cpp"><li class="alt"><span style="color:#000000;">flag = fcntl (fd, F_GETFL); </span></li><li><span style="color:#000000;">fcntl (fd, F_SETFL, flag | O_NONBLOCK); </span></li></ol></div>,Windows下为
<div class="dp-highlighter bg_cpp" style="word-wrap: break-word; font-family: tahoma, 宋体; line-height: 1.6;"><div class="bar" style="word-wrap: break-word; line-height: 1.6;"><div class="tools" style="word-wrap: break-word; line-height: 1.6;"><strong>[cpp]</strong> </div></div><ol class="dp-cpp"><li class="alt"><span style="color:#000000;">flag = 1; </span></li><li><span style="color:#000000;">ioctlsocket (fd, FIONBIO, (unsigned </span><span class="datatypes"><strong><span style="color:#2e8b57;">long</span></strong></span><span style="color:#000000;"> *) &flag); </span></li></ol></div>。
当非阻塞socket的TCP连接正在进行时,Linux的错误号为EINPROGRESS,Windows的错误号为WSAEWOULDBLOCK。
file
Linux下面,文件换行是"\n",而windows下面是"\r\n"。
Linux下面,目录分隔符是"/",而windows下面是"\"。
Linux与Windows下面,均可以使用stat调用来查询文件信息。但是,Linux只支持2G大小,而Windows只支持4G大小。为了支持更大的文件查询,可以在Linux环境下加_FILE_OFFSET_BITS=64定义,在Windows下面使用_stat64调用,入参为struct __stat64。
Linux中可根据stat的st_mode判断文件类型,有S_ISREG、S_ISDIR等宏。Windows中没有,需要自己定义相应的宏,如
<div class="dp-highlighter bg_cpp" style="word-wrap: break-word; font-family: tahoma, 宋体; line-height: 1.6;"><div class="bar" style="word-wrap: break-word; line-height: 1.6;"><div class="tools" style="word-wrap: break-word; line-height: 1.6;"><strong>[cpp]</strong> </div></div><ol class="dp-cpp"><li class="alt"><span class="preprocessor"><span style="color:#808080;">#define S_ISREG(m) (((m) & 0170000) == (0100000)) </span></span><span style="color:#000000;"> </span></li><li><span class="preprocessor"><span style="color:#808080;">#define S_ISDIR(m) (((m) & 0170000) == (0040000))</span></span><span style="color:#000000;"> </span></li></ol></div>
Linux中删除文件是unlink,Windows中为DeleteFile。
time
Linux中,time_t结构是长整形。而windows中,time_t结构是64位的整形。如果要在windows始time_t为32位无符号整形,可以加宏定义,_USE_32BIT_TIME_T。
Linux中,sleep的单位为秒。Windows中,Sleep的单位为毫秒。即,Linux下sleep (1),在Windows环境下则需要Sleep (1000)。
Windows中的timecmp宏,不支持大于等于或者小于等于。
Windows中没有struct timeval结构的加减宏可以使用,需要手动定义:
<div class="dp-highlighter bg_cpp" style="word-wrap: break-word; font-family: tahoma, 宋体; line-height: 1.6;"><div class="bar" style="word-wrap: break-word; line-height: 1.6;"><div class="tools" style="word-wrap: break-word; line-height: 1.6;"><strong>[cpp]</strong> </div></div><ol class="dp-cpp"><li class="alt"><span class="preprocessor"><span style="color:#808080;">#define MICROSECONDS (1000 * 1000) </span></span><span style="color:#000000;"> </span></li><li><span style="color:#000000;"> </span></li><li class="alt"><span class="preprocessor"><span style="color:#808080;">#define timeradd(t1, t2, t3) do { \ </span></span><span style="color:#000000;"> </span></li><li><span style="color:#000000;"> (t3)->tv_sec = (t1)->tv_sec + (t2)->tv_sec; \ </span></li><li class="alt"><span style="color:#000000;"> (t3)->tv_usec = (t1)->tv_usec + (t2)->tv_usec % MICROSECONDS; \ </span></li><li><span style="color:#000000;"> </span><span class="keyword"><strong><span style="color:#006699;">if</span></strong></span><span style="color:#000000;"> ((t1)->tv_usec + (t2)->tv_usec > MICROSECONDS) (t3)->tv_sec ++; \ </span></li><li class="alt"><span style="color:#000000;">} </span><span class="keyword"><strong><span style="color:#006699;">while</span></strong></span><span style="color:#000000;"> (0) </span></li><li><span style="color:#000000;"> </span></li><li class="alt"><span class="preprocessor"><span style="color:#808080;">#define timersub(t1, t2, t3) do { \ </span></span><span style="color:#000000;"> </span></li><li><span style="color:#000000;"> (t3)->tv_sec = (t1)->tv_sec - (t2)->tv_sec; \ </span></li><li class="alt"><span style="color:#000000;"> (t3)->tv_usec = (t1)->tv_usec - (t2)->tv_usec; \ </span></li><li><span style="color:#000000;"> </span><span class="keyword"><strong><span style="color:#006699;">if</span></strong></span><span style="color:#000000;"> ((t1)->tv_usec - (t2)->tv_usec < 0) (t3)->tv_usec --, (t3)->tv_usec += MICROSECONDS; \ </span></li><li class="alt"><span style="color:#000000;">} </span><span class="keyword"><strong><span style="color:#006699;">while</span></strong></span><span style="color:#000000;"> (0) </span></li></ol></div>
例子:
void CoverScene::connectGame()
{
//1715
CCNotificationCenter::sharedNotificationCenter()->addObserver(this,
callfuncO_selector(CoverScene::loginGame),
NET_CONNECT_OVER,
NULL);
CCLOG("connectGame1.........");
char **pptr;
const char *ptr;
struct hostent *hptr;
char str[32];
CCLOG("%s",GameUtil::userSelectServer.ip.c_str());
if((hptr = gethostbyname(GameUtil::userSelectServer.ip.c_str())) == NULL)
{
printf(" gethostbyname error for host:%s\n", GameUtil::userSelectServer.ip.c_str());
return ;
}
switch(hptr->h_addrtype)
{
case AF_INET:
pptr=hptr->h_addr_list;
ptr = inet_ntop(hptr->h_addrtype, *pptr, str, sizeof(str));
break;
case AF_INET6:
pptr=hptr->h_addr_list;
ptr = inet_ntop(hptr->h_addrtype, *pptr, str, sizeof(str));
break;
default:
printf("unknown address type\n");
break;
}
if(GameUtil::netDisconnect == M_LOGOUT)
{
MessageBoxLayer::getMessageBoxPoint()->createWithLoading();
if(atoi(GameUtil::userSelectServer.sid.c_str()) == 12 || atoi(GameUtil::userSelectServer.sid.c_str()) == 13 || atoi(GameUtil::userSelectServer.sid.c_str()) == 14 )
{
AppDelegate::outNet = false;
NetController::GetInstance()->changeAddress(GameUtil::userSelectServer.ip.c_str(), atoi(GameUtil::userSelectServer.port.c_str()));
}
else
{
AppDelegate::outNet = true;
//这里初始化 NetController!!!!!!!!!!!!!!!!!!!!!!!!!!!!
CCLOG("GameUtil::userSelectServer.port:%s",GameUtil::userSelectServer.port.c_str());
NetController::GetInstance()->changeAddress(ptr, atoi(GameUtil::userSelectServer.port.c_str()));
}
NetController::GetInstance()->reConnect();
}
else
{
CCLOG("connectGame else。。。。。");
AppDelegate::outNet = true;
if(atoi(GameUtil::userSelectServer.sid.c_str()) == 12 || atoi(GameUtil::userSelectServer.sid.c_str()) == 13 || atoi(GameUtil::userSelectServer.sid.c_str()) == 14 )
{
AppDelegate::outNet = false;
NetController::GetInstance()->Init(GameUtil::userSelectServer.ip.c_str(), atoi(GameUtil::userSelectServer.port.c_str()));
}
else
{
AppDelegate::outNet = true;
CCLOG("GameUtil::userSelectServer.port:%s",GameUtil::userSelectServer.port.c_str());
NetController::GetInstance()->Init(ptr, atoi(GameUtil::userSelectServer.port.c_str()));
}
CCUserDefault::sharedUserDefault()->setStringForKey(SERVER_ID, GameUtil::userSelectServer.sid);
CCUserDefault::sharedUserDefault()->flush();
CCLOG(" NetController::GetInstance。。。。。");
NetController::GetInstance()->Start();
MessageBoxLayer::getMessageBoxPoint()->createWithLoading();
}
}