cls_sock.cpp

来自「ABis无线接口全套资料」· C++ 代码 · 共 915 行 · 第 1/2 页

CPP
915
字号
	SetSocketProp();
	SetNonBlock();
}

void CLS_TCPConnect::SetConnectProp(int iSocket, const char *strIP, int iPort)
{
	if(m_iSocket>0)
		CloseSocket();
	m_iSocket=iSocket;
	m_iStatus=SS_CONNECT;
	strcpy(m_strIP, strIP);
	m_iPort=iPort;
	SetSocketProp();
	SetNonBlock();
}

char *CLS_TCPConnect::GetIP()
{
	return m_strIP;
}

int CLS_TCPConnect::GetPort()
{
	return m_iPort;
}


CLS_TCPSvr::CLS_TCPSvr()
	:CLS_Socket()
{
	m_iCount=0;
	m_strLocalIP[0]=0;
	m_iLocalPort=0;
	memset(m_pClient, 0, sizeof(CLS_TCPConnect *)*MAX_CONNECTION);
}

CLS_TCPSvr::~CLS_TCPSvr()
{
	int i;

	for(i=0; i<m_iCount; i++)
		delete m_pClient[i];
}

void CLS_TCPSvr::SetLocalPort(int iPort)
{
	m_iLocalPort=iPort;
}

int CLS_TCPSvr::GetLocalPort()
{
	return m_iLocalPort;
}

void CLS_TCPSvr::SetLocalIP(const char *strLocalIP)
{
	strcpy(m_strLocalIP, strLocalIP);
}

char *CLS_TCPSvr::GetLocalIP()
{
	return m_strLocalIP;
}

int CLS_TCPSvr::GetClientCount()
{
	return m_iCount;
}

CLS_TCPConnect *CLS_TCPSvr::GetClient(int index)
{
	if(index<MAX_CONNECTION)
		return m_pClient[index];
	return NULL;
}

int CLS_TCPSvr::Bind()
{
	if(m_strLocalIP && m_strLocalIP[0])
		return CLS_Socket::Bind(m_strLocalIP, m_iLocalPort);
	return 0;
}

int CLS_TCPSvr::Listen()
{
	char *strIP;

	if(m_iStatus==SS_UNUSED)
		if(CreateSocket()<0)
			return -1;

	if(m_iStatus!=SS_LISTEN)
	{
		if(m_iStatus!=SS_BOUND)
			if(Bind()<0)
				return -2;

		if (listen(m_iSocket, 5)<0) 
		{
			CloseSocket();
			return -3;
		}

		if(!m_iLocalPort)
		{
			strIP=GetLocalHost(&m_iLocalPort);
			if(strIP)
				strcpy(m_strLocalIP, strIP);
		}

		m_iStatus=SS_LISTEN;
	}

	return 0;
}

int CLS_TCPSvr::AddConnect(int iSocket, const char *strIP, int iPort)
{
	int i;

	if(m_iCount==MAX_CONNECTION)
		return -1;

	for(i=0; i<m_iCount; i++)
		if(!strcmp(m_pClient[i]->GetIP(), strIP))
		{
			m_pClient[i]->SetConnectProp(iSocket, strIP, iPort);
			break;
		}

	if(i==m_iCount)
	{
		m_pClient[i]=new CLS_TCPConnect(iSocket, strIP, iPort);
		m_iCount++;
	}

	return i;
}

int CLS_TCPSvr::Accept()
{
	int iLen;
	int iAcceptSock;
	// unsigned long iFlag=1;
	struct sockaddr_in  sockAddr;
	
	if(m_iCount==MAX_CONNECTION)
		return -1;

	iLen=sizeof(struct sockaddr);
	iAcceptSock= accept(m_iSocket, (struct sockaddr *)&sockAddr, (socklen_t *)&iLen);
	if(iAcceptSock >0) 
	{
		m_iSocketError=0;
		return AddConnect(iAcceptSock, inet_ntoa(sockAddr.sin_addr), sockAddr.sin_port);
	}

#ifdef _WIN32
	m_iSocketError=GetLastError();
	if(m_iSocketError!=WSAEWOULDBLOCK)
	{
		CloseSocket();
		return -3;
	}
#else
	m_iSocketError=errno;
	// if(!(m_iSocketError==EWOULDBLOCK||m_iSocketError==EINTR))
	if ((EWOULDBLOCK == m_iSocketError) // Modify 2006-01-24, by Wu jianjin.
		|| (EINTR == m_iSocketError) // 
		|| (EAGAIN == m_iSocketError)) // Ignore "Broken pipe" error.
	{
		// Nothing to do.
	}
	else
	{
		CloseSocket();
		return -3;
	}
#endif

	return -2;
}

CLS_TCPClient::CLS_TCPClient()
	:CLS_Socket()
{
	m_strSvrIP[0]=0;
	m_iSvrPort=-1;
	m_strLocalIP[0]=0;
	m_iLocalPort=0;
}

CLS_TCPClient::~CLS_TCPClient()
{
}

void CLS_TCPClient::SetSvrIP(const char *strIP)
{
	strcpy(m_strSvrIP, strIP);
}

char *CLS_TCPClient::GetSvrIP()
{
	return m_strSvrIP;
}

void CLS_TCPClient::SetSvrPort(int iPort)
{
	m_iSvrPort=iPort;
}

int CLS_TCPClient::GetSvrPort()
{
	return m_iSvrPort;
}

void CLS_TCPClient::SetLocalIP(const char *strLocalIP)
{
	strcpy(m_strLocalIP, strLocalIP);
}

char *CLS_TCPClient::GetLocalIP()
{
	return m_strLocalIP;
}

void CLS_TCPClient::SetLocalPort(int iPort)
{
	m_iLocalPort=iPort;
}

int CLS_TCPClient::GetLocalPort()
{
	return m_iLocalPort; 
}

int CLS_TCPClient::Bind()
{
	if(m_strLocalIP && m_strLocalIP[0])
		return CLS_Socket::Bind(m_strLocalIP, m_iLocalPort);
	return 0;
}

int CLS_TCPClient::Connect()
{
	char *strIP;
	static struct sockaddr_in sockAddr;

	if(m_iStatus==SS_UNUSED)
	{
		if(CreateSocket()<0)
		{
//			AfxMessageBox("CreateSocket Error!");
			return -1;
		}
	}

	if(m_iStatus!=SS_CONNECT)
	{
		if(m_iStatus!=SS_BOUND)
		{
			if(Bind()<0)
			{
#ifdef _WIN32
				m_iSocketError=GetLastError();
#else
				m_iSocketError=errno;
#endif
				CloseSocket();
				return -1;
			}
		}

		memset(&sockAddr, 0, sizeof(sockAddr));
		sockAddr.sin_family = AF_INET;
		sockAddr.sin_addr.s_addr = inet_addr(m_strSvrIP);
		sockAddr.sin_port = htons(m_iSvrPort);

		if(connect(m_iSocket, (struct sockaddr *)&sockAddr, sizeof(sockAddr))<0)
		{
#ifdef _WIN32
			m_iSocketError=GetLastError();
			if(m_iSocketError!=WSAEISCONN)
			{
				if(m_iSocketError==WSAEINVAL)
					CloseSocket();
#else
			m_iSocketError=errno;
			if(m_iSocketError!=EISCONN)
			{
				if(m_iSocketError==EINVAL)
					CloseSocket();
#endif
				return -3;
			}
		}

		if(!(m_strLocalIP[0]||m_iLocalPort))
		{
			strIP=GetLocalHost(&m_iLocalPort);
			if(strIP)
				strcpy(m_strLocalIP, strIP);
		}
		m_iSocketError=0;
		m_iStatus=SS_CONNECT;
	}

	return 0;
}

CLS_UDP::CLS_UDP()
	:CLS_Socket()
{
	m_strLocalIP[0]=0;
	m_iLocalPort=0;
	m_strPeerIP[0]=0;
	m_iPeerPort=-1;
}

CLS_UDP::~CLS_UDP()
{
}

void CLS_UDP::SetPeerIP(const char *strIP)
{
	strcpy(m_strPeerIP, strIP);
}

char *CLS_UDP::GetPeerIP()
{
	return m_strPeerIP;
}

void CLS_UDP::SetPeerPort(int iPort)
{
	m_iPeerPort=iPort;
}

int CLS_UDP::GetPeerPort()
{
	return m_iPeerPort;
}

void CLS_UDP::SetLocalIP(const char *strIP)
{
	strcpy(m_strLocalIP, strIP);
}

char *CLS_UDP::GetLocalIP()
{
	return m_strLocalIP;
}

void CLS_UDP::SetLocalPort(int iPort)
{
	m_iLocalPort=iPort;
}

int CLS_UDP::GetLocalPort()
{
	return m_iLocalPort;
}

int CLS_UDP::CreateSocket()
{
	m_iSocket = socket(AF_INET, SOCK_DGRAM, 0); 
	if(m_iSocket>-1)
		m_iStatus=SS_CREATED;

	return m_iSocket;
}

int CLS_UDP::Bind()
{
	int iRet;
	char *strIP;

	iRet=CLS_Socket::Bind(m_strLocalIP, m_iLocalPort);
	if(!iRet && !m_iLocalPort)
	{
		strIP=GetLocalHost(&m_iLocalPort);
		if(strIP)
			strcpy(m_strLocalIP, strIP);
	}

	return iRet;
}

int CLS_UDP::Connect()
{
	char *strIP;
	static struct sockaddr_in sockAddr;

	if(m_iStatus==SS_UNUSED)
	{
		if(CreateSocket()<0)
			return -1;
	}

	if(m_iStatus!=SS_CONNECT)
	{
		if(m_iStatus!=SS_BOUND)
		{
			if(Bind()<0)
				return -1;
		}

		memset(&sockAddr, 0, sizeof(sockAddr));
		sockAddr.sin_family = AF_INET;
		sockAddr.sin_addr.s_addr = inet_addr(m_strPeerIP);
		sockAddr.sin_port = htons(m_iPeerPort);

		if(connect(m_iSocket, (struct sockaddr *)&sockAddr, sizeof(sockAddr))<0)
		{
#ifdef _WIN32
			m_iSocketError=GetLastError();
//			if(!(m_iSocketError==WSAEINPROGRESS || m_iSocketError==WAEISCONN))
			if(m_iSocketError!=WSAEISCONN)
			{
				if(m_iSocketError==WSAEINVAL)
					CloseSocket();
#else
			m_iSocketError=errno;
//			if(!(m_iSocketError==EINPROGRESS || m_iSocketError==EISCONN))
			if(m_iSocketError!=EISCONN)
			{
				if(m_iSocketError==EINVAL)
					CloseSocket();
#endif
				return -3;
			}
		}

		if(!(m_strLocalIP[0]||m_iLocalPort))
		{
			strIP=GetLocalHost(&m_iLocalPort);
			if(strIP)
				strcpy(m_strLocalIP, strIP);
		}
		m_iSocketError=0;
		m_iStatus=SS_CONNECT;
	}

	return 0;
}

// ------------------------------------------------------------------------
//
//  Revision list.
//  ==============
//
//  1.0,        2003-05-05,     Yang guangliang
//      Initial version.
//
// ------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?