csocketfun.cpp

来自「几年前用vc6写的漏洞扫描器」· C++ 代码 · 共 138 行

CPP
138
字号
// 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;


}

⌨️ 快捷键说明

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