www.pudn.com > rbScanner.rar > CSocketFun.cpp


// CSocketFun.cpp: implementation of the CSocketFun class. 
// 
////////////////////////////////////////////////////////////////////// 
 
#include "stdafx.h" 
#include "try4.h" 
#include "CSocketFun.h" 
 
#ifdef _DEBUG 
#undef THIS_FILE 
static char THIS_FILE[]=__FILE__; 
#define new DEBUG_NEW 
#endif 
 
////////////////////////////////////////////////////////////////////// 
// Construction/Destruction 
////////////////////////////////////////////////////////////////////// 
 
CSocketFun::CSocketFun() 
{ 
 
} 
 
CSocketFun::~CSocketFun() 
{ 
 
} 
 
int CSocketFun::StartUp() 
{ 
	WORD    wVersionRequested; 
	WSADATA wsaData; 
	int     err;  
	 
	wVersionRequested = MAKEWORD( 2, 0 );  
	err = WSAStartup( wVersionRequested, &wsaData ); 
	if ( err != 0 ) { 
		return err; 
	}  
 
	if ( LOBYTE( wsaData.wVersion ) != 2 || 
        HIBYTE( wsaData.wVersion ) != 0 ) { 
		WSACleanup( ); 
		return WSAVERNOTSUPPORTED;  
	}  
	 
	return 0; 
} 
 
 
int CSocketFun::CleanUp() 
{ 
	int nRetCode; 
 
	nRetCode = WSACleanup(); 
	if (nRetCode != 0) { 
		return WSAGetLastError(); 
	} 
 
	return 0;  
} 
 
 
int CSocketFun::GetLocalHostName(CString& sHostName) 
{ 
 
	char szHostName[256]; 
	int  nRetCode; 
 
	nRetCode = gethostname(szHostName, sizeof(szHostName)); 
 
	if (nRetCode != 0) { 
		sHostName = _T("Not available");; 
		return WSAGetLastError(); 
	} 
 
	sHostName = szHostName; 
	return 0; 
} 
 
int CSocketFun::GetLocalIPAddr(CString& sIPAddress) 
{ 
	 
	char szHostName[256],*sHostName; 
	int  nRetCode; 
 
	nRetCode = gethostname(szHostName, sizeof(szHostName)); 
 
	if (nRetCode != 0) { 
	    sHostName = _T("Not available");; 
		return WSAGetLastError(); 
	} 
 
	struct hostent FAR *lpHostEnt = gethostbyname (szHostName); 
 
	if (lpHostEnt == NULL){ 
		sIPAddress = _T(""); 
		return WSAGetLastError(); 
	} 
 
	LPSTR lpAddr = lpHostEnt->h_addr_list[0]; 
 
	if (lpAddr) { 
		struct in_addr  inAddr; 
		memmove (&inAddr, lpAddr, 4); 
		sIPAddress = inet_ntoa (inAddr); 
		if (sIPAddress.IsEmpty()) 
			sIPAddress = _T("Not available"); 
	}		 
	 
	return 0; 
} 
 
CString CSocketFun::GetIPFromNETName(CString& sNETName) 
{ 
	CString sIP; 
 
	struct hostent FAR *lpHostent=gethostbyname(sNETName); 
 
	if(lpHostent==NULL){ 
		sIP=_T("ÄúÊäÈë´íÎó»òÍøÖ·²»´æÔÚ!"); 
		return sIP; 
	} 
 
	LPSTR lpAddr = lpHostent->h_addr_list[0]; 
 
	if(lpAddr){ 
		struct in_addr inAddr; 
		memmove(&inAddr,lpAddr,4); 
		sIP=inet_ntoa(inAddr); 
		if (sIP.IsEmpty()) 
			sIP = _T("Not available"); 
	} 
 
	return sIP; 
 
 
}