⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ysocket.h

📁 非常方便的网络Socket类
💻 H
📖 第 1 页 / 共 3 页
字号:
void YSocket<STRING>::Bind (const SOCKADDR* lpSockAddr,int mode)
{
	ASSERT(m_hSocket != NULL);
	ASSERT(lpSockAddr != NULL) ;
	switch(mode){
	case 1:
		BOOL tp = TRUE;
		setsockopt(m_hSocket,SOL_SOCKET ,SO_REUSEADDR,(char*)&tp,sizeof(BOOL));
		break;
	}	
	if ( bind(m_hSocket,lpSockAddr,sizeof(struct sockaddr_in)) == SOCKET_ERROR )
	{
		STRING tp;tp.Format("Bind.no.2:%s",GetErrorStr());
		throw tp ;	
	}
	GetIpAdd(1);
}

template <class STRING>
BOOL YSocket<STRING>::Close(int mode )
{
	if(IsLive){
		//if(JugeTime(1,1)!=1){
		//Sleep(1);
		try{
			if(nSTP == SOCK_STREAM){
				//char tpbuffer[1000];
				//Read(tpbuffer,1000,0);
				//Sleep(50);
				
				//struct linger Linger; 
				//Linger.l_onoff = 1;   /* 开启 linger 设定*/ 
				//Linger.l_linger = 5;  /* 设定 linger 时间为 5 秒 */ 
				//
				if(shutdown(m_hSocket,SD_BOTH)!=0||
					closesocket(m_hSocket)!=0){
					return FALSE;
				};
				IsLive = 0;
				m_hSocket = NULL;
			} 
		}catch(...){
			return FALSE;
		}
	}
	return TRUE;
}

template <class STRING>
void YSocket<STRING>::Connect(const SOCKADDR* lpSockAddr)
{
	YASSERT(m_hSocket != NULL);
	if(connect(m_hSocket, lpSockAddr, sizeof(SOCKADDR)) == SOCKET_ERROR) {
		STRING tp;tp.Format("Connect.no.1:%s",GetErrorStr());
		throw tp ;
	}
	sfGetP = 0;
	tpserver=*lpSockAddr;
}
// 可以直接用IP地址或域名
template <class STRING>
void YSocket<STRING>::Connect(LPCTSTR lpszHostAddress, UINT nHostPort)
{
	YASSERT(m_hSocket != NULL);
	SOCKADDR_IN server;
    server.sin_family = AF_INET;
    server.sin_port = htons(nHostPort);
	if((server.sin_addr.s_addr=inet_addr(lpszHostAddress)) == INADDR_NONE)
	{   // lpszHostAddress 不是*.*.*.*格式的IP地址
		struct hostent *hp;
        // 通过主机名获取主机地址
		if((hp = gethostbyname(lpszHostAddress)) != NULL)
		{
			memcpy(&(server.sin_addr),hp->h_addr_list[0],hp->h_length);
			server.sin_family = hp->h_addrtype;
		}else {
			STRING tp;tp.Format("Connect.no.2:%s",GetErrorStr());
			throw tp ;
		}
		//delete hp;
	}
	Connect((const SOCKADDR*)&server);
}

template <class STRING>
void YSocket<STRING>::Create(int nSocketType){
	int nProtocal ;
	if (nSocketType == SOCK_DGRAM){
		nProtocal = IPPROTO_UDP ;
	}else if(nSocketType == SOCK_STREAM){
		nProtocal = IPPROTO_TCP ;
	}else if(nSocketType == SOCK_RAW){
		nProtocal = IPPROTO_ICMP ;
	}

	if ( (m_hSocket = socket(AF_INET, nSocketType, nProtocal)) == INVALID_SOCKET){
	//	prerrlog("Creat Faild\n:");
	//	prerrlog(WSAGetLastError());
		STRING tp;tp.Format("Create.no.1:%s",GetErrorStr());
		throw tp ;
	}
	/*
	if(setsockopt(m_hSocket, SOL_SOCKET, SO_LINGER, (const char*)&lg, sizeof(struct linger))==-1){
		STRING tp;tp.Format("Create.no.2:%s",GetErrorStr());
		throw tp ;
	} 
	//*/
	IsLive = 1;
	nSTP=nSocketType;
}

template <class STRING>
void YSocket<STRING>::Listen(int nConnectionBacklog)
{
	if (nSTP == SOCK_DGRAM){
		;//do nothing
	}else if(nSTP== SOCK_STREAM){
		//int hh = SOMAXCONN;
		listen(m_hSocket,nConnectionBacklog);
	}else if(nSTP== SOCK_RAW){
		;//do nothing
	}

}

template <class STRING>
int YSocket<STRING>::Receive(void* lpBuf,const int nBufLen, 
					   const int nSecs  )
{
	if(JugeTime(nSecs,1)!=1){
		return 0;
	}
	int nBytesReceived;
	if((nBytesReceived=recv(m_hSocket,(char *)lpBuf,nBufLen,0))==SOCKET_ERROR){
		STRING tp;tp.Format("Receive.no.1:%s",GetErrorStr());
		throw tp ;
	}
	SHUT_MODE = SD_SEND ;
	return nBytesReceived;
}

template <class STRING>
int YSocket<STRING>::ReceiveFrom(void* lpBuf, const int nBufLen,
						   LPCTSTR pSocketAddress,UINT& rSocketPort,
						   const int nSecs)
{
	struct sockaddr_in sinAddress ;
	GetSockAddr(sinAddress,pSocketAddress,rSocketPort);
	ReceiveFrom(lpBuf,nBufLen,(sockaddr *)&sinAddress,nSecs);
	return 0; 
}

template <class STRING>
int YSocket<STRING>::ReceiveFrom(void* lpBuf,const int nBufLen,const int nSecs)
{
	SOCKADDR* lpSockAddr=&tpserver;
	int ret,nBytesReceived=0,len;
	sockaddr_in addrfrom;
	ret=JugeTime(nSecs,1);
	if(ret==1){
		len=sizeof(addrfrom);
		if((nBytesReceived = recvfrom(m_hSocket, (char*)lpBuf, nBufLen, 0,(sockaddr *)&addrfrom,&len)) == SOCKET_ERROR)
			STRING tp;tp.Format("ReceiveFrom.no.1:%s",GetErrorStr());
			throw tp ;
	}
	return nBytesReceived;
}

template <class STRING>
int YSocket<STRING>::ReceiveFrom(void* lpBuf, const int nBufLen,SOCKADDR* lpSockAddr, 
						   const int nSecs=DEFAULT_TIMEOUT)
{
	int ret,nBytesReceived=0,len;
	sockaddr_in addrfrom;
	ret=JugeTime(nSecs,1);
	if(ret==1){
		len=sizeof(addrfrom);
		if((nBytesReceived = recvfrom(m_hSocket, (char*)lpBuf, nBufLen, 0,(sockaddr *)&addrfrom,&len)) == SOCKET_ERROR){
			STRING tp;tp.Format("ReceiveFrom.no.1:%s",GetErrorStr());
			throw tp ;	
		}
	}
	return nBytesReceived;
}

template <class STRING>
int YSocket<STRING>::ReceiveTest(void* lpBuf, const int nBufLen)
{
	int ret,nBytesReceived=0;
	ret=JugeTime(0);
	if(ret==1){//可读,有数据要发送过来	//接收sock发送来的数据			
		if((nBytesReceived = recv(m_hSocket, (char*)lpBuf, nBufLen, 0)) == SOCKET_ERROR){
			STRING tp;tp.Format("ReceiveTest.no.2:%s",GetErrorStr());
			throw tp ;	
		}
		/*注释掉了,2004-10-1 因为这个仅仅是测试,所以可能是什么都收不到,但是连接没有断
		if(nBytesReceived==0){		//如果对方断开	
			STRING tp;tp.Format("ReceiveTest.no.3:%s",GetErrorStr());
			throw tp ;	
		}
		*/
	}
	return nBytesReceived;
}
template <class STRING>
int YSocket<STRING>::Send(const void* lpBuf, int nBufLen, 
					const int nSecs)
{
	int ret,nBytesReceived=0;
	ret=JugeTime(nSecs,2);
	if(ret==2){//可以写
		if((nBytesReceived=send(m_hSocket,(char*)lpBuf,nBufLen,0))==SOCKET_ERROR){
			STRING tp;tp.Format("Send.no.1:%s",GetErrorStr());
			throw tp ;	
		}
	}else return 0;
	SHUT_MODE = SD_RECEIVE  ;
	return nBytesReceived;
}

template <class STRING>
int YSocket<STRING>::Write(const void* lpBuf, int nBufLen, const int nSecs)
{
	if(JugeTime(nSecs,2)!=2){
		return 0;	//不可以写则返回
	}
	//TRACE("begin_Write:%d   ",nBufLen);
	int send_size = 0;
	int tpsize = 0;
	while(send_size<nBufLen){
		tpsize=nBufLen-send_size;
		if((tpsize=send(m_hSocket,(char*)lpBuf+send_size,tpsize,0))==SOCKET_ERROR){
			STRING tp;tp.Format("Write.no.1:%s",GetErrorStr());
			throw tp ;	
		}
		send_size+=tpsize;
	}
	//TRACE("end_Write:%d   \n",send_size);
	SHUT_MODE = SD_RECEIVE ; 
	return send_size;
}

template <class STRING>
int YSocket<STRING>::Read(void* lpBuf, int nBufLen,const int nSecs,int mode)
{
//TRACE("JugeTime Begin On Read!\n");
	int tpsize=0;
  if(!nBufLen)return 0;
	if(JugeTime(nSecs,1)!=1){
//TRACE("JugeTime Failed On Read!\n");
		return 0;
		char h[1];
		if((tpsize=recv(m_hSocket,h,0,0))==SOCKET_ERROR)	{
			STRING tp;tp.Format("Read.no.0:%s",GetErrorStr());
			throw tp ;	
		}
	}
//TRACE("JugeTime Success On Read!\n");
	int recv_size=0;
	int timeNum=0;bool timebool=0;
	while(recv_size<nBufLen){
		tpsize=nBufLen-recv_size;
		if(timeNum!=0&&JugeTime(timeNum,1)!=1){
			if(mode){//强制读取
				if(JugeTime(60,1)!=1){
					return recv_size;
				};
			}else{
				timeNum++;
				if(JugeTime(timeNum,1)!=1){
					return recv_size;
				}
			}
		}
		if((tpsize=recv(m_hSocket,(char *)lpBuf+recv_size,tpsize,0))==SOCKET_ERROR)	{
			STRING tp;tp.Format("Read.no.1:%s",GetErrorStr());
			throw tp ;	
		}else if(tpsize==0){
			if(nSecs==-1){//如果为尽量读的模式,则这个表示无法继续读取了,不抱错误就返回
				break;
			}else{
				STRING tp;tp.Format("Read.no.2:%s",GetErrorStr());
				throw tp ;	
			}
		}
		recv_size += tpsize;
		if(mode){
		}else{
			if(timebool)timeNum++;
			timebool = !timebool;
		}
	}
	SHUT_MODE = SD_SEND ;
	return recv_size;
}

template <class STRING>
int YSocket<STRING>::SendTo(const void* lpBuf, int nBufLen,UINT nHostPort, 
					  LPCTSTR lpszHostAddress , const int nSecs)

⌨️ 快捷键说明

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