📄 socketbase.cpp
字号:
/*
2003 by zhy
*/
#include "stdafx.h"
#include "../include/exception.h"
using namespace CommonInclude;
#include "SocketBase.h"
using namespace NET;
#include <sstream>
using namespace std;
void CSocketBase::SetStateBit(typeState nState)
{
long nFlags = static_cast<long>(m_nState);
nFlags |= static_cast<long>(nState);
m_nState = static_cast<typeState>(nFlags);
}
void CSocketBase::ClearStateBit(typeState nState)
{
long nFlags = static_cast<long>(m_nState);
nFlags &= ~(static_cast<long>(nState));
m_nState = static_cast<typeState>(nFlags);
}
////////////////////////////////////////////////////////////////////////////
// check if Read or Write is available,return 0 if available //
////////////////////////////////////////////////////////////////////////////
int CSocketBase::SocketTimeCheck(SOCKET fd,int nAction,int nSec,int nUSec)
{
fd_set action_fds;
fd_set except_fds;
struct timeval tv;
struct timeval *tp;
FD_ZERO( &action_fds );
FD_SET( fd, &action_fds );
FD_ZERO( &except_fds );
FD_SET( fd, &except_fds );
if ((nSec > 0) || (nUSec>0))
{
tv.tv_usec = nUSec;
tv.tv_sec = nSec;
tp = &tv;
}
else
tp = NULL;
int res;
if ( nAction == 1 ) // Wait for read
res = select( fd + 1, &action_fds , NULL, &except_fds, tp);
else
res = select( fd + 1, NULL, &action_fds , &except_fds, tp);
switch ( res )
{
case -1:
return -1;
case 0:
return -1;
default:
if (FD_ISSET(fd,&except_fds))
return -1;
return 0;
}//switch
return 0;
}
////////////////////////////////////////////////////////////////////////////
// Map IP to struct in_addr //
////////////////////////////////////////////////////////////////////////////
struct in_addr CSocketBase::GetIPAddr( const char * lpszHost )
{
struct in_addr IP;
if ( lpszHost == NULL || *lpszHost=='\0' )
{
IP.s_addr = INADDR_ANY;
return IP;
}
IP.s_addr =inet_addr( lpszHost );
if ( IP.s_addr == (unsigned long)INADDR_NONE ) //if it's not a dotted decimal
{
struct hostent * phe;
phe = gethostbyname( lpszHost );
if ( phe == NULL )
throw CExceptionBase( WSAGetLastError(), std::string("Can't get host entry of : ") + lpszHost );
memcpy(&IP,*phe->h_addr_list,sizeof(in_addr));
}
return IP;
};
////////////////////////////////////////////////////////////////////////////
// Get host's IP(may be more than one) //
////////////////////////////////////////////////////////////////////////////
bool CSocketBase::GetOwnIP(vector<string>& vOwnIP)
{
char szHostName[128];
vOwnIP.resize(0);
if( gethostname(szHostName, 128) == 0 )
{
// Get host adresses
struct hostent * pHost;
pHost = gethostbyname(szHostName);
for(int i = 0; pHost!= NULL && pHost->h_addr_list[i]!= NULL; i++ )
{
int j;
string strIP;
for( j = 0; j < pHost->h_length; j++ )
{
stringstream stream;
string addr;
if( j > 0 )
strIP += ".";
stream<<(unsigned int)(unsigned char)pHost->h_addr_list[i][j];
addr = stream.str();
strIP += addr;
}
vOwnIP.push_back(strIP);
}
if(i != 0)
return true;
else
return false;
}
else
return false;
}
////////////////////////////////////////////////////////////////////////////
// About Close Operations //
////////////////////////////////////////////////////////////////////////////
void CSocketBase::Close()
{
Shutdown();
Onlyclose();
}
void CSocketBase::Shutdown()
{
if (m_sock != INVALID_SOCKET)
::shutdown(m_sock,2);
}
void CSocketBase::Onlyclose()
{
if (m_sock!=INVALID_SOCKET)
{
closesocket(m_sock);
m_sock = INVALID_SOCKET;
SetStateBit(STATE_CLOSE);
}
}
////////////////////////////////////////////////////////////////////////////
// Inner Operations //
////////////////////////////////////////////////////////////////////////////
bool CSocketBase::Initsocket(int domain, int type, int protocol)
{
if (m_sock != INVALID_SOCKET)
return true;
m_sock = socket(domain,type,protocol);
return m_sock != INVALID_SOCKET;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -