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

📄 winsocket.cpp

📁 一个winsock网络通信程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	* for the object. (wMsg) will be sent to the      *
	* Window Procedure of (hWnd) whenever one of the  *
	* events in (lEvent) has occurred. See MSDN docs  *
	* for WSAAsyncSelect() for more information.	  *
	*                                                 *
	* RETURNS: ERR_BADPARAM for invalid               *
	* parameters, ERR_WSAERROR upon error,            *
	* otherwise ERR_SUCCESS                           *
	*                                                 *
	***************************************************/

	if( !IsWindow( hWnd ) || wMsg == 0 || lEvent == 0 )
        return ERR_BADPARAM;

	if( WSAAsyncSelect( m_hSocket, hWnd, wMsg, lEvent ) == SOCKET_ERROR )
	{
        set_LastError( "WSAAsyncSelect() failed", WSAGetLastError() );
        return ERR_WSAERROR;
	}
	return ERR_SUCCESS;
}

int winSocket::get_RemoteIP( char* strIP )
{
	/*************************************************
	* FUNCTION: get_RemoteIP                         *
	*                                                *
	* PURPOSE: Copies the IP address for the remote  *
	* side on an established TCP/IP connection into  *
	* (strIP).                                       *
	*                                                *
	* RETURNS: ERR_BADPARAM for invalid parameters,  *
	* ERR_WSAERROR upon error, otherwise ERR_SUCCESS *
	*                                                *
	**************************************************/

	if( strIP == NULL )
        return ERR_BADPARAM;

	int namelen = sizeof( m_rsockaddr );

	if( getpeername( m_hSocket, (SOCKADDR*)&m_rsockaddr, &namelen ) == SOCKET_ERROR )
	{
        set_LastError( "getpeername() failed", WSAGetLastError() );
        return ERR_WSAERROR;
	}

	longToDottedQuad( m_rsockaddr.sin_addr.s_addr, strIP );

	return ERR_SUCCESS;
}

int winSocket::get_RemotePort( int* iPort )
{
	/*************************************************
	* FUNCTION: get_RemotePort                       *
	*                                                *
	* PURPOSE: Copies the port number for the remote *
	* side on an established TCP/IP connection into  *
	* (iPort).                                       *
	*                                                *
	* RETURNS: ERR_BADPARAM for invalid parameters,  *
	* ERR_WSAERROR upon error, otherwise ERR_SUCCESS *
	*                                                *
	**************************************************/

	if( iPort == NULL )
		return ERR_BADPARAM;

	int namelen = sizeof( m_rsockaddr );
	
	if( getpeername( m_hSocket, (SOCKADDR*)&m_rsockaddr, &namelen ) == SOCKET_ERROR )
	{
        set_LastError( "getpeername() failed", WSAGetLastError() );
        return ERR_WSAERROR;
	}

	*iPort = ntohs( m_rsockaddr.sin_port );

	return ERR_SUCCESS;
}

int winSocket::get_LocalHost( char* strBuffer, int iBufLen )
{
	/*************************************************
	* FUNCTION: get_LocalHost                        *
	*                                                *
	* PURPOSE: Copies the fully qualified host name  *
	* for the local machine into (strBuffer). Will   *
	* fail if returned data is longer than (iBufLen).*
	*                                                *
	* RETURNS: ERR_BADPARAM for invalid parameters,  *
	* ERR_WSAERROR upon error, otherwise ERR_SUCCESS *
	*                                                *
	**************************************************/

	if( strBuffer == NULL )
		return ERR_BADPARAM;

	char strHost[512] = {0};
	hostent* hostEnt = NULL;
	int iLen = 0;

	gethostname( strHost, 512 );
	hostEnt = gethostbyname( strHost );

	if( hostEnt == NULL )
		return ERR_WSAERROR;

	iLen = strlen( hostEnt->h_name );

	if( iLen > iBufLen )
		return ERR_BADPARAM;

	memset( strBuffer, 0, iBufLen );
	memcpy( strBuffer, hostEnt->h_name, iLen );

	return ERR_SUCCESS;
}

int winSocket::get_RemoteHost( char* strBuffer, int iBufLen )
{
	/*************************************************
	* FUNCTION: get_RemoteHost                       *
	*                                                *
	* PURPOSE: Copies the fully qualified host name  *
	* of the remote side (on a connected socket)     *
	* into (strBuffer). Will fail if data returned   *
	* is longer than iBufLen.                        *
	*                                                *
	* RETURNS: ERR_BADPARAM for invalid parameters,  *
	* ERR_WSAERROR upon error, otherwise ERR_SUCCESS *
	*                                                *
	**************************************************/

	if( strBuffer == NULL )
		return ERR_BADPARAM;

	hostent* hostEnt = NULL;
	int iLen = 0;
	int namelen = sizeof( m_rsockaddr );

	if( getpeername( m_hSocket, (SOCKADDR*)&m_rsockaddr, &namelen ) == SOCKET_ERROR )
		return ERR_WSAERROR;

	hostEnt = gethostbyaddr( (char*)&m_rsockaddr.sin_addr.s_addr, 4 ,PF_INET );

	if( hostEnt != NULL )
	{
		iLen = strlen( hostEnt->h_name );
		if( iLen > iBufLen )
			return ERR_BADPARAM;

		memcpy( strBuffer, hostEnt->h_name, iLen );
		return ERR_SUCCESS;
	}

	return ERR_WSAERROR;
}

int winSocket::get_LocalIP( char* strIP )
{
	/*************************************************
	* FUNCTION: get_LocalIP                          *
	*                                                *
	* PURPOSE: Copies the IP address for the local   *
	* machine into (strIP). Requires that Connect or *
	* Bind be called previously                      *
	*                                                *
	* RETURNS: ERR_BADPARAM for invalid parameters,  *
	* otherwise ERR_SUCCESS                          *
	*                                                *
	**************************************************/

	if( strIP == NULL )
		return ERR_BADPARAM;
	
	int  namelen = sizeof( m_sockaddr );
	HOSTENT* hEnt = NULL;
	char szHostName[512] = {0};
	char szIP[16] = {0};
	char szAddrField[4] = {0};
	unsigned int ufield = 0;

	if( getsockname( m_hSocket, (SOCKADDR*)&m_sockaddr, &namelen ) == SOCKET_ERROR )
		return ERR_WSAERROR;

	longToDottedQuad( m_sockaddr.sin_addr.s_addr, strIP );

	return ERR_SUCCESS;
}

int winSocket::get_LocalPort( int* iPort )
{
	/*****************************************************
	* FUNCTION: get_LocalPort                            *
	*                                                    *
	* PURPOSE: Copies the local port number associated   *
	* with the SOCKET object into (iPort).               *
	* Requires that Connect or Bind be called previously *
	*                                                    *
	* RETURNS: ERR_BADPARAM for invalid parameters,      *
	* otherwise ERR_SUCCESS                              *
	*                                                    *
	******************************************************/

	if( iPort == NULL )
        return ERR_BADPARAM;

	*iPort = ntohs(m_sockaddr.sin_port);

	return ERR_SUCCESS;
}

int winSocket::set_SendTimeout( int ms )
{
	/*****************************************************
	* FUNCTION: set_SendTimeout                          *
	*                                                    *
	* PURPOSE: Sets the amount of time the socket will   *
	* wait before returning WSAETIMEDOUT when calling    *
	* Send(). Set to 0 for infinite (default)            *
	*                                                    *
	* RETURNS: ERR_BADPARAM for invalid parameters,      *
	* ERR_WSAERROR for a winsock error,                  *
	* otherwise ERR_SUCCESS                              *
	*                                                    *
	******************************************************/
	
	if( ms < 0 )
		return ERR_BADPARAM;

	if( setsockopt( m_hSocket, SOL_SOCKET, SO_SNDTIMEO, (char*)&ms, sizeof( ms ) ) == SOCKET_ERROR )
	{
		set_LastError( "setsockopt() failed.", WSAGetLastError() );
		return ERR_WSAERROR;
	}

	return ERR_SUCCESS;
}

int winSocket::set_RecvTimeout( int ms )
{
	/*****************************************************
	* FUNCTION: set_RecvTimeout                          *
	*                                                    *
	* PURPOSE: Sets the amount of time the socket will   *
	* wait before returning WSAETIMEDOUT when calling    *
	* Receive(). Set to 0 for infinite (default)         *
	*                                                    *
	* RETURNS: ERR_BADPARAM for invalid parameters,      *
	* ERR_WSAERROR for a winsock error,                  *
	* otherwise ERR_SUCCESS                              *
	*                                                    *
	******************************************************/
	
	if( ms < 0 )
		return ERR_BADPARAM;

	if( setsockopt( m_hSocket, SOL_SOCKET, SO_RCVTIMEO, (char*)&ms, sizeof( ms ) ) == SOCKET_ERROR )
	{
		set_LastError( "setsockopt() failed.", WSAGetLastError() );
		return ERR_WSAERROR;
	}

	return ERR_SUCCESS;
}

void winSocket::set_LastError( char* newError, int errNum )
{
	/**************************************************
	* FUNCTION: set_LastError                         *
	*                                                 *
	* PURPOSE: Sets error information for the object. *
	*                                                 *
	* RETURNS: None.                                  *
	*                                                 *
	***************************************************/

	memset( m_LastError, 0, ERR_MAXLENGTH ); 
	memcpy( m_LastError, newError, strlen( newError ) );
	m_LastError[strlen(newError)+1] = '\0';
	m_ErrorNumber = errNum;
}

void winSocket::get_LastError( char* strBuffer, int* iErrNum )
{
	/***************************************************
	* FUNCTION: get_LastError                          *
	*                                                  *
	* PURPOSE: Retreives description and number of the *
	* last error that occurred. Copies into (strBuffer)*
	* and (iErrNum), repsectively.                     *
	*                                                  *
	* RETURNS: None.                                   *
	*                                                  *
	****************************************************/

	int len = strlen( m_LastError );

	if( len > 0 )
	{
        memset( strBuffer, 0, len );
        memcpy( strBuffer, m_LastError, len );
        strBuffer[len+1] = '\0';
        *iErrNum = m_ErrorNumber;
	}
}

void winSocket::longToDottedQuad( unsigned long ulLong, char* cBuffer )
{
	/*****************************************************
	* FUNCTION: longToDottedQuad                         *
	*                                                    *
	* PURPOSE: Translates an IP address from 32-bit long *
	* to dotted quad form (255.255.255.255). Translates  *
	* (ulLong) and copies results to (cBuffer).          *
	*                                                    *
	* RETURNS: None.                                     *
	*                                                    *
	******************************************************/

	wsprintf( cBuffer, "%d.%d.%d.%d",(int)((BYTE*)&ulLong)[0],
		(int)((BYTE*)&ulLong)[1],(int)((BYTE*)&ulLong)[2],(int)((BYTE*)&ulLong)[3] );
}

⌨️ 快捷键说明

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