www.pudn.com > TestIPMSG.rar > AcceptClient.cpp


// AcceptClient.cpp: implementation of the CAcceptClient class. 
// 
////////////////////////////////////////////////////////////////////// 
 
#include "stdafx.h" 
#include "TestIPMSG.h" 
#include "AcceptClient.h" 
#include "FastTransmit.h" 
 
#ifdef _DEBUG 
#undef THIS_FILE 
static char THIS_FILE[]=__FILE__; 
#define new DEBUG_NEW 
#endif 
 
////////////////////////////////////////////////////////////////////// 
// Construction/Destruction 
////////////////////////////////////////////////////////////////////// 
 
vector g_AcceptClient; //全局变量,用来管理客户端信息 
 
CAcceptClient::CAcceptClient(): 
	m_loginName(NULL), 
	m_groupName(NULL) 
{ 
 
} 
 
CAcceptClient::~CAcceptClient() 
{ 
	if ( m_loginName != NULL ) 
		delete m_loginName; 
	if ( m_groupName != NULL )  
		delete m_groupName; 
} 
 
BOOL CAcceptClient::IniListenSocket() 
{ 
	int istat = WSAStartup(MAKEWORD(2,2),&m_wsa); 
	 
	if ( istat != 0 ) 
	{ 
		AfxMessageBox("Can not Open Winsock!"); 
		return FALSE; 
	} 
 
	m_listenSocket = socket(AF_INET,SOCK_DGRAM,0); 
 
	if ( m_listenSocket == INVALID_SOCKET ) 
	{ 
		AfxMessageBox("Creating listenSocket is failed!"); 
		return FALSE; 
	} 
 
	GetLocalIP(m_dwLocalIP); 
 
	m_listenAddr.sin_addr.S_un.S_addr = INADDR_ANY; 
	m_listenAddr.sin_port = htons(NET_TRANSMIT_PORT); 
	m_listenAddr.sin_family = AF_INET; 
 
	int ibind = bind(m_listenSocket,(struct sockaddr FAR *) &m_listenAddr,sizeof(SOCKADDR_IN)); 
 
	if ( ibind == SOCKET_ERROR ) 
	{ 
		AfxMessageBox("binging is failed!"); 
		return FALSE ; 
	} 
 
	return TRUE; 
} 
 
void CAcceptClient::RecvClientData() 
{ 
	SOCKADDR_IN	from; 
	int addrLen = sizeof(SOCKADDR_IN); 
	char RecvDataInfo[NET_TRANSMIT_BUFFER]; 
	int irecv = recvfrom(m_listenSocket,RecvDataInfo,sizeof(RecvDataInfo),0,(struct sockaddr FAR *)&from,&addrLen); 
	 
	if ( irecv == SOCKET_ERROR ) 
	{ 
		//AfxMessageBox("Recving ClientInfo is failed!"); 
		return; 
	} 
 
	if ( from.sin_addr.S_un.S_addr != m_dwLocalIP ) 
	{ 
		ClientInfo *OneClientInfo = ( ClientInfo *)RecvDataInfo; 
 
		vector::iterator ts; 
		 
		for( ts = g_AcceptClient.begin(); ts != g_AcceptClient.end(); ts ++ ) 
		{ 
			if ( strcmp(ts->m_loginName,OneClientInfo->m_name) == 0 ) 
			{ 
				SendToClient();  
				return; 
			} 
		} 
		ManClientInfo OneManClientInfo ; 
		strcpy(OneManClientInfo.m_loginName,OneClientInfo->m_name); 
		strcpy(OneManClientInfo.m_groupName,OneClientInfo->m_group); 
 
		DWORD dwClientIPAddr = from.sin_addr.S_un.S_addr; 
		char n_ClientIPAddr[20]; 
		sprintf(n_ClientIPAddr,"%d.%d.%d.%d" 
						,dwClientIPAddr&0x000000ff 
						,dwClientIPAddr>>8&0x0000000ff 
						,dwClientIPAddr>>16&0x000000ff 
						,dwClientIPAddr>>24&0x000000ff 
						); 
		strcpy(OneManClientInfo.m_ClientIPAddr,n_ClientIPAddr); 
		 
		strcpy(OneManClientInfo.m_ComputerName,OneClientInfo->m_computerName); 
 
		g_AcceptClient.push_back(OneManClientInfo); 
 
		SendToClient();   // 
	} 
} 
 
void CAcceptClient::SendToClient() 
{ 
	char BroadCastIP[20]; 
	sprintf(BroadCastIP,"%d.%d.%d.255" 
						,m_dwLocalIP&0x000000ff 
						,m_dwLocalIP>>8&0x0000000ff 
						,m_dwLocalIP>>16&0x000000ff 
						); 
 
	SOCKADDR_IN AddrSendToClient; 
	AddrSendToClient.sin_addr.S_un.S_addr = inet_addr(BroadCastIP); 
	AddrSendToClient.sin_family = AF_INET; 
	AddrSendToClient.sin_port = htons(NET_TRANSMIT_PORT); 
 
 
	ClientInfo *SendingInfo = new ClientInfo; 
 
	memset(SendingInfo,0,sizeof(ClientInfo)); 
     
	struct hostent *thisHost = gethostbyname(m_hostName); 
	 
	if ( thisHost != NULL ) 
		strcpy(SendingInfo->m_computerName,thisHost->h_name); 
	if ( m_loginName == NULL ) 
		strcpy(SendingInfo->m_name,m_TLocalUserName); 
	else  
		strcpy(SendingInfo->m_name,m_loginName); 
	if ( m_groupName == NULL ) 
		strcpy(SendingInfo->m_group,""); 
	else 
		strcpy(SendingInfo->m_group,m_groupName); 
 
	int isend = sendto(m_listenSocket,(char*)SendingInfo,sizeof(ClientInfo),0,(struct sockaddr FAR *)&AddrSendToClient,sizeof(SOCKADDR_IN)); 
 
	int iError = WSAGetLastError(); 
 
	delete SendingInfo; 
	if ( isend == SOCKET_ERROR ) 
	{ 
		AfxMessageBox("sending initial info is failed!"); 
		return ; 
	} 
 
} 
 
void CAcceptClient::StartServerThread() 
{ 
	BOOL bIni = IniListenSocket(); 
	if ( bIni == FALSE ) 
		return ; 
 
	IniManClientInfo(); 
 
	HANDLE hThread = CreateThread(NULL,0,ServerAcceptThread,this,CREATE_SUSPENDED,&m_id); 
	ResumeThread(hThread); 
 
	CloseHandle(hThread); 
} 
 
DWORD WINAPI CAcceptClient::ServerAcceptThread(LPVOID lParam) 
{ 
	CAcceptClient *AccClient = (CAcceptClient *)lParam ; 
 
	AccClient->SendToClient(); 
 
	FD_SET fdR; 
	struct timeval timeouts; 
 
	while( true ) 
	{ 
		timeouts.tv_sec = 1; 
		timeouts.tv_usec = 0; 
		FD_ZERO(&fdR); 
		FD_SET(AccClient->m_listenSocket,&fdR); 
		switch( select ( AccClient->m_listenSocket + 1,&fdR,NULL,NULL,&timeouts) ) 
		{ 
		case -1: 
			break; 
		case 0: 
			{ 
				int iEr = WSAGetLastError(); 
				break; 
			} 
		default: 
			if ( FD_ISSET(AccClient->m_listenSocket,&fdR) ) 
				AccClient->RecvClientData(); 
			break; 
		} 
		Sleep(1); 
	}	 
	return 0L; 
} 
 
void CAcceptClient::GetLocalIP(DWORD &dwLocalIP) 
{ 
	struct hostent *thisHost; 
	struct in_addr in; 
 
	if ( gethostname(m_hostName,sizeof(m_hostName)) == SOCKET_ERROR ) 
	{ 
		AfxMessageBox("can not get local name!"); 
		return; 
	} 
 
	thisHost = gethostbyname(m_hostName); 
 
	if ( thisHost == NULL ) 
		return ; 
	 
	int i = 0; 
 
	while( thisHost->h_addr_list[i] ) 
	{ 
		in.S_un.S_addr = *(unsigned long *) thisHost->h_addr_list[i]; 
		i++; 
	} 
	dwLocalIP = in.S_un.S_addr; 
	sprintf(m_localIPAddr,"%d.%d.%d.%d" 
				,dwLocalIP&0x000000ff 
				,dwLocalIP>>8&0x0000000ff 
				,dwLocalIP>>16&0x000000ff 
				,dwLocalIP>>24&0x000000ff 
				); 
} 
 
void CAcceptClient::ProClientData(char *RecvDataInfo) 
{ 
 
} 
 
void CAcceptClient::IniManClientInfo() 
{ 
	GetLoginGroupFromINI(); 
 
	ManClientInfo LocalInfo; 
	strcpy(LocalInfo.m_ClientIPAddr,m_localIPAddr); 
	strcpy(LocalInfo.m_ComputerName,m_hostName); 
	 
	if ( m_loginName == NULL ) 
	{ 
		DWORD dwNameLen = sizeof ( m_TLocalUserName ); 
		GetUserName(m_TLocalUserName,&dwNameLen); 
		strcpy(LocalInfo.m_loginName,m_TLocalUserName); 
	} 
	else  
		strcpy(LocalInfo.m_loginName,m_loginName); 
	if ( m_groupName == NULL ) 
		strcpy(LocalInfo.m_groupName,""); 
	else 
		strcpy(LocalInfo.m_groupName,m_groupName); 
	g_AcceptClient.push_back(LocalInfo); 
} 
 
void CAcceptClient::GetLoginGroupFromINI() 
{ 
	char directoryName[MAX_PATH]; 
	::GetCurrentDirectory(MAX_PATH,directoryName); 
	//GetPrivateProfileString("OLEDB","IpAddress","192.168.0.1",IPAddr,1024,lpszFileName); 
 
	char FileName[MAX_PATH]; 
	WIN32_FIND_DATA finddata; 
	sprintf(FileName,"%s\\Record.INI",directoryName); 
	HANDLE hFindFile = FindFirstFile(FileName,&finddata); 
	 
	if ( hFindFile == INVALID_HANDLE_VALUE ) 
	{ 
		TRACE("Can not find the specified file!\n"); 
		return; 
	} 
	else 
	{ 
		m_loginName = new char[80]; 
		m_groupName = new char[80]; 
		GetPrivateProfileString("Record","LoginName","192.168.0.1",m_loginName,1024,FileName); 
		GetPrivateProfileString("Record","GroupName","192.168.0.1",m_groupName,1024,FileName); 
	}	 
} 
 
void CAcceptClient::DeleteOneClient(SOCKADDR_IN from) 
{ 
   
}