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

📄 tcpip.cpp

📁 这个是应用了很多工程的TCPIP通讯库
💻 CPP
📖 第 1 页 / 共 5 页
字号:
					this->Open();
					break;

				//	The requested address is a broadcast address, 
				//	but the appropriate flag was not set. 				
				case WSAEACCES			:
					sprintf(szMsg, "Send Error %ud Requested address flag incorrect for Ip Address %s", nError, this->m_szIpAddress);
					DisplayMessage(szMsg);
					this->Close();
					this->Open();
					break;

				//	A blocking Windows Sockets call was canceled through 
				//	WSACancelBlockingCall
				case WSAEINTR	:
					sprintf(szMsg, "Send Error %ud Socket call cancelled for Ip Address %s", nError, this->m_szIpAddress);
					DisplayMessage(szMsg);
					this->Close();
					this->Open();
					break;

				//	The lpBuffers, lpNumberOfBytesSent, lpOverlapped,
				//	lpCompletionRoutine argument is not totally contained 
				//	in a valid part of the user address space.
				//	
				case WSAEFAULT			:	
					sprintf(szMsg, "Send Error %ud Arguement not valid part of user address space for Ip Address %s", nError, this->m_szIpAddress);
					DisplayMessage(szMsg);
					this->Close();
					this->Open();
					break;
				
				//	The connection has been broken due to "keep-alive" activity 
				//	detecting a failure while the operation was in progress.
				case WSAENETRESET		:
					sprintf(szMsg, "Send Error %ud Connection was broken due to failure in operation for Ip Address %s", nError, this->m_szIpAddress);
					DisplayMessage(szMsg);
					this->Close();
					this->Open();
					break;

				//	The socket is not connected.
				case WSAENOTCONN		:	
					sprintf(szMsg, "Send Error %ud Socket not connected for Ip Address %s", nError, this->m_szIpAddress);
					DisplayMessage(szMsg);
					this->Close();
					this->Open();
					break;

				//	The descriptor is not a socket.
				case WSAENOTSOCK		:	
					sprintf(szMsg, "Send Error %ud Descriptor not a socket for Ip Address %s", nError, this->m_szIpAddress);
					DisplayMessage(szMsg);
					this->Close();
					this->Open();
					break;

				//	The referenced socket is not a type that supports 
				//	connection-oriented service.
				case WSAEOPNOTSUPP		:	
					sprintf(szMsg, "Send Error %ud Socket incorrect type for Ip Address %s", nError, this->m_szIpAddress);
					DisplayMessage(szMsg);
					this->Close();
					this->Open();
					break;

				//	The socket has been shut down; it is not possible 
				//	to WSASend on a socket after shutdown has been invoked 
				//	with how set to SD_SEND or SD_BOTH.
				case WSAESHUTDOWN		:
					sprintf(szMsg, "Send Error %ud Socket has been shut down for Ip Address %s", nError, this->m_szIpAddress);
					DisplayMessage(szMsg);
					this->Close();
					this->Open();
					break;

				//	The socket has not been bound with bind, 
				//	or the socket is not created with the overlapped flag.
				case WSAEINVAL			:	
					sprintf(szMsg, "Send Error %ud Socket has not been bound for Ip Address %s", nError, this->m_szIpAddress);
					DisplayMessage(szMsg);
					this->Close();
					this->Open();
					break;

				//	The virtual circuit was terminated 
				//	due to a time-out or other failure.
				case WSAECONNABORTED	:
					sprintf(szMsg, "Send Error %ud Connection was terminated for Ip Address %s", nError, this->m_szIpAddress);
					DisplayMessage(szMsg);
					this->Close();
					this->Open();
					break;

				//	The overlapped operation has been canceled due 
				//	to the closure of the socket, or the execution 
				//	of the SIO_FLUSH command in WSAIoctl.
				case WSA_OPERATION_ABORTED	:
					sprintf(szMsg, "Send Error %ud Operation has been aborted for Ip Address %s", nError, this->m_szIpAddress);
					DisplayMessage(szMsg);
					this->Close();
					this->Open();
					break;

				//	The virtual circuit was reset by the remote side.
				case WSAECONNRESET:
					sprintf(szMsg, "Send Error %ud Connection was reset by peer for Ip Address %s", nError, this->m_szIpAddress);
					DisplayMessage(szMsg);
					this->Close();
					this->Open();
					break;

				//	A blocking Windows Sockets call is in progress, 
				//	or the service provider is still processing a callback function.
				case WSAEINPROGRESS		:
					sprintf(szMsg, "Send still in progress for Ip Address %s", this->m_szIpAddress);
//					DisplayMessage(szMsg);
					break;
			
				//	Overlapped sockets: 
				//	There are too many outstanding overlapped I/O requests. 
				//	Nonoverlapped sockets: 
				//	The socket is marked as nonblocking and the send operation 
				//	cannot be completed immediately.
				case WSAEWOULDBLOCK		:
					sprintf(szMsg, "Send Would Block for Ip Address %s", this->m_szIpAddress);
//					DisplayMessage(szMsg);
					break;

				//	The socket is message oriented, and the message 
				//	is larger than the maximum supported by the 
				//	underlying transport.
				case WSAEMSGSIZE		:
					sprintf(szMsg, "Send Message larger than that supported for Ip Address %s", this->m_szIpAddress);
					DisplayMessage(szMsg);
					break;

				//	No buffer space is available.
				case WSAENOBUFS	:
					sprintf(szMsg, "Send Message No Buffer space for Ip Address %s", this->m_szIpAddress);
					DisplayMessage(szMsg);
					break;

				//	An overlapped operation was successfully initiated and 
				//	completion will be indicated at a later time.
				case WSA_IO_PENDING		:
					sprintf(szMsg, "Send Message I/O still pending for Ip Address %s", this->m_szIpAddress);
//					DisplayMessage(szMsg);
					break;

				//	default. return value unknown. 
				//	just go on.
				default:
					sprintf(szMsg, "Send Message error unknownfor Ip Address %s", this->m_szIpAddress);
//					DisplayMessage(szMsg);
					TRACE("TCP: Send attempt failed with error: %d\n", nError);
					break;
			}

			dwBytesLeftToSend = 0;
			dwErrorCode = IO_SEND_ERROR;
		}
		else
		{
					sprintf(szMsg, "Error Send %ud Unknown for Ip Address %s", nError, this->m_szIpAddress);
					DisplayMessage(szMsg);
					this->Close();
					this->Open();
		}
	}
	else
	{
		sprintf(szMsg, "Tried to send but connection not established for Ip Address %s", this->m_szIpAddress);
		DisplayMessage(szMsg);
		dwErrorCode = IO_SEND_ERROR;
		TRACE("TCP: Tried to send but connection not established\n");
	}

	return(dwErrorCode);  
}

//
// @mfunc   Send - (Overloaded) In TcpIp Server Mode 
//			This function sends data to the specified client socket
//
// @parm   IN SOCKET | ClientSocket | Socket Address of Client to send the data
//
// @parm   IN unsigned char * | pBuffer | Buffer to send
//
// @parm   IN DWORD | dwSize | Number of bytes to send
//
// @rvalue IO_SUCCESS | If successful
//
// @rvalue IO_SEND_ERROR | If send failed
//
// @rvalue IO_CLIENT_ERR | If object not in server mode
//
// @rvalue IO_SOCKET_ERROR | If Client Socket Invalid
//
EXPORT32 DWORD CTcpIp::Send(IN SOCKET ClientSocket, IN unsigned char *pBuffer, IN DWORD dwSize)
{
	int nError;

	DWORD dwBytesSent = 0, dwBytesLeftToSend, dwBytesTotalSent = 0,
		  dwErrorCode = IO_SUCCESS;

	WSABUF sNetBuffer;

	// This is a client not server. Just go away.
	if (this->m_bClient)
	{
		dwErrorCode = IO_CLIENT_ERR;
		return(dwErrorCode);
	}

	if (ClientSocket == INVALID_SOCKET)
	{
		dwErrorCode = IO_SOCKET_ERROR;
		return(dwErrorCode);
	}

		dwBytesLeftToSend = dwSize;

		sNetBuffer.len = dwSize;
		sNetBuffer.buf = (char *)pBuffer;
		
		//
		//	Now actually send data.
		//	Note:	This can be used for both TCP and UDP protocols.
		//			However, on connectionless sockets (UDP) only if they 
		//			have a stipulated default peer address established through 
		//			the connect or WSAConnect function.
		//
		nError = WSASend(ClientSocket, 
						 (LPWSABUF)&sNetBuffer, 
						 1, 
						 &dwBytesSent, 
						 0,
						 (WSAOVERLAPPED *)&this->m_WriteOverlapped, 
						 NULL);
		if (ERROR_SUCCESS == nError)
		{
			dwErrorCode = IO_SUCCESS;
			// calculate what's left to send 
			dwBytesTotalSent += dwBytesSent;
			dwBytesLeftToSend = dwSize - dwBytesTotalSent;
		}
		else if (nError == SOCKET_ERROR)
		{
			TCHAR szMsg[256];

			nError = WSAGetLastError();

			switch (nError)
			{
				//	A successful WSAStartup must occour
				case WSANOTINITIALISED	:	

				//	The network subsystem has failed
				case WSAENETDOWN		:

				//	The requested address is a broadcast address, 
				//	but the appropriate flag was not set. 				
				case WSAEACCES			:

				//	A blocking Windows Sockets call was canceled through 
				//	WSACancelBlockingCall
				case WSAEINTR	:

				//	The lpBuffers, lpNumberOfBytesSent, lpOverlapped,
				//	lpCompletionRoutine argument is not totally contained 
				//	in a valid part of the user address space.
				//	
				case WSAEFAULT			:	
				
				//	The connection has been broken due to "keep-alive" activity 
				//	detecting a failure while the operation was in progress.
				case WSAENETRESET		:

				//	The socket is not connected.
				case WSAENOTCONN		:	

				//	The descriptor is not a socket.
				case WSAENOTSOCK		:	

				//	The referenced socket is not a type that supports 
				//	connection-oriented service.
				case WSAEOPNOTSUPP		:	

				//	The socket has been shut down; it is not possible 
				//	to WSASend on a socket after shutdown has been invoked 
				//	with how set to SD_SEND or SD_BOTH.
				case WSAESHUTDOWN		:

				//	The socket has not been bound with bind, 
				//	or the socket is not created with the overlapped flag.
				case WSAEINVAL			:	

				//	The virtual circuit was terminated 
				//	due to a time-out or other failure.
				case WSAECONNABORTED	:

				//	The overlapped operation has been canceled due 
				//	to the closure of the socket, or the execution 
				//	of the SIO_FLUSH command in WSAIoctl.
				case WSA_OPERATION_ABORTED	:

				//	The virtual circuit was reset by the remote side.
				case WSAECONNRESET:
					sprintf(szMsg, "Server: Connection was reset by peer for Socket Address %d", ClientSocket);
					DisplayMessage(szMsg);
					this->CloseConnection(ClientSocket);
					break;


				//	A blocking Windows Sockets call is in progress, 
				//	or the service provider is still processing a callback function.
				case WSAEINPROGRESS		:
			
				//	Overlapped sockets: 
				//	There are too many outstanding overlapped I/O requests. 
				//	Nonoverlapped sockets: 
				//	The socket is marked as nonblocking and the send operation 
				//	cannot be completed immediately.
				case WSAEWOULDBLOCK		:

				//	The socket is message oriented, and the message 
				//	is larger than the maximum supported by the 
				//	underlying transport.
				case WSAEMSGSIZE		:

				//	No buffer space is available.
				case WSAENOBUFS	:

				//	An overlapped operation was successfully initiated and 
				//	completion will be indicated at a later time.
				case WSA_IO_PENDING		:

				//	default. return value unknown. 
				//	just go on.
				default:
					TRACE("TCP: Send attempt failed  with error: %d\n",nError);
					break;
			}

			dwBytesLeftToSend = 0;
			dwErrorCode = IO_SEND_ERROR;
		}

	return(dwErrorCode);  
}

//
// @mfunc   Send - (Overloaded) In UdpIp Server Mode 
//			This function sends data to the specified client socket
//
// @parm   IN SOCKADDR | ClientSockAddr| SockAddr Address of Client to send the data
//
// @parm   IN unsigned char * | pBuffer | Buffer to send
//
// @parm   IN DWORD | dwSize | Number of bytes to send
//
// @rvalue IO_SUCCESS | If successful
//
// @rvalue IO_SEND_ERROR | If send failed
//
// @rvalue IO_CLIENT_ERR | If object not in server mode
//
// @rvalue IO_SOCKET_ERROR | If Client Socket Invalid
//
EXPORT32 DWORD CTcpIp::Send(IN SOCKADDR ClientSockAddr, IN unsigned char *pBuffer, IN DWORD dwSize)
{
	int nError;

	DWORD dwBytesSent = 0, dwBytesLeftToSend, dwBytesTotalSent = 0,
		  dwErrorCode = IO_SUCCESS;

	WSABUF sNetBuffer;

	// This is a client not server. Just go away.
	if (this->m_bClient)
	{
		dwErrorCode = IO_CLIENT_ERR;
		return(dwErrorCode);
	}

	if (this->m_Socket == INVALID_SOCKET)
	{
		dwErrorCode = IO_SOCKET_ERROR;
		return(dwErrorCode);
	}

		dwBytesLeftToSend = dwSize;

		sNetBuffer.len = dwSize;
		sNetBuffer.buf = (char *)pBuffer;
		
		//
		//	Now actually send data.
		//	Note:	This can be used for both TCP and UDP protocols.
		//			However, on connectionless sockets (UDP) only if they 
		//			have a stipulated default peer address established through 
		//			the connect or WSAConnect function.
		//
			nError = WSASendTo(this->m_Socket,
							(LPWSABUF)&sNetBuffer, 
							1, 
							&dwBytesSent, 
							0,
							(LPSOCKADDR)&ClientSockAddr,
							m_nSocketAddressSize,
							(WSAOVERLAPPED *)&this->m_WriteOverlapped, 
							NULL);
		if (ERROR_SUCCESS == nError)
		{
			dwErrorCode = IO_SUCCESS;
			// calculate what's left to send 
			dwBytesTotalSent += dwBytesSent;
			dwBytesLeftToSend = dwSize - dwBytesTotalSent;
		}
		else if (nError == SOCKET_ERROR)
		{
			TCHAR szMsg[256];

			nError = WSAGetLastError();

			switch (nError)
			{
				//	A successful WSAStartup must occour
				case WSANOTINITIALISED	:	

				//	The network subsystem has failed
				case WSAENETDOWN		:

				//	The requested address is a broadcast address, 
				//	but the appropriate flag was not set. 				
				case WSAEACCES			:

				//	A blocking Windows Sockets call was canceled through 
				//	WSACancelBlockingCall
				case WSAEINTR	:

				//	The lpBuffers, lpNumberOfBytesSent, lpOverlapped,
				//	lpCompletionRoutine argument is not totally contained 
				//	in a valid part of the user address space.
				//	
				case WSAEFAULT			:	
				
				//	The connection has been broken due to "keep-alive" activity 
				//	detecting a failure while the operation was in progress.
				case WSAENETRESET		:

				//	The socket is not connected.
				case WSAENOTCONN		:	

				//	The descriptor is not a socket.
				case WSAENOTSOCK		:	

				//	The referenced socket is not a type that supports 
				//	connection-oriented service.
				case WSAEOPNOTSUPP		:	

				//	The socket has been shut down; it is not possible 
				//	to WSASend on a socket after shutdown has been invoked 
				//	with how set to SD_SEND or SD_BOTH.
				case WSAESHUTDOWN		:

				//	The socket has not been bound with bind, 

⌨️ 快捷键说明

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