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

📄 wbqqclientdlg.cpp

📁 基于TCP的局域网多用户通信、文件传送程序详解
💻 CPP
字号:
// wbQQClientDlg.cpp : implementation file
//

#include "stdafx.h"
#include "wbQQClient.h"
#include "wbQQClientDlg.h"

#include "NetSet.h"
#include "RegWizard.h"
#include "ChatDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define NO_FLAGS_SET 0
/////////////////////////////////////////////////////////////////////////////
// CWbQQClientDlg dialog

CWbQQClientDlg::CWbQQClientDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CWbQQClientDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CWbQQClientDlg)
	m_strUserName = _T("");
	m_strPassWd = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CWbQQClientDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CWbQQClientDlg)
	DDX_Text(pDX, IDC_EDIT_USERNAME, m_strUserName);
	DDX_Text(pDX, IDC_EDIT_PASSWD, m_strPassWd);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CWbQQClientDlg, CDialog)
	//{{AFX_MSG_MAP(CWbQQClientDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BTN_REG, OnBtnReg)
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWbQQClientDlg message handlers

BOOL CWbQQClientDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CWbQQClientDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CWbQQClientDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CWbQQClientDlg::OnBtnReg() 
{
	// TODO: Add your control notification handler code here
	OnCancel();

	CRegWizard regDlg;
	if ((regDlg.DoModal() == ID_WIZFINISH) && CRegNetSetDlg::bRegSuc)
	{
		MessageBox("您已经注册成功,请\n使用用户名和密码登录!", "闻博QQ", MB_OK);
		CWbQQClientDlg dlg;
		dlg.DoModal();
	}
	else
	{
		MessageBox("注册失败,请重新\n注册或按取消键退出!", "闻博QQ", MB_OK);
		CWbQQClientDlg dlg;
		dlg.DoModal();
	}
}

void CWbQQClientDlg::OnOK() 
{
	// TODO: Add extra validation here
	UpdateData(TRUE);
	if (m_strUserName.IsEmpty() || m_strPassWd.IsEmpty())
	{
		MessageBox("用户名和密码都不能为空!", "闻博QQ", MB_OK);
		return;
	}
////////////////////初始化网络////////////////////////////////////
	if (!InitNet())
	{
//		CDialog::OnCancel();
//		return;
	}
//////////////////登录服务器//////////////////////////////////////
	if (Load())
	{
		closesocket(destSocket);
		CDialog::OnOK();
		MessageBox("登录成功!", "闻博QQ", MB_OK);
	
		CChatDlg chat;
		chat.m_strUserName = m_strUserName;
		chat.DoModal();
	}
	else
	{
		MessageBox("还没有注册或者\n用户名/密码有误!\n请重新登录或注册!", "闻博QQ", MB_OK);
		return;
	}
}

void CWbQQClientDlg::OnCancel() 
{
	// TODO: Add extra cleanup here
	
	CDialog::OnCancel();
}

void CWbQQClientDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	
	CDialog::OnTimer(nIDEvent);
}

BOOL CWbQQClientDlg::InitNet()	//初始化网络
{
	char IP[20];	//用于从网络设置文件读取IP地址
	char port[8];	//用于从网络设置文件读取字符串端口号
	u_short nPort;
	FILE *pFile;
	CString fileName;
	fileName.Format(".\\data\\netset.cfg");
	if ((pFile=fopen(fileName, "r")) == NULL)
	{
		if (AfxMessageBox("找不到网络设置文件!\n未注册用户点是进行注册,\n已注册用户点否设置网络!", MB_YESNO) == IDYES)
		{
			OnBtnReg();
		}
		else
		{
			OnCancel();
			CNetSet set;
			if (set.DoModal() ==IDCANCEL)
			{
				return false;
			}
			pFile = fopen(fileName, "r");	//打开设置好的网络文件
		}
	}
///////////////从网络设置文件读取注册登录服务器IP地址////////////////
	char ch = 's';
	for (int i=0; ch != ','; i++)
	{
		ch = fgetc(pFile);
		IP[i] = ch;
	}
	IP[--i] = '\0';
////////////////从网络设置文件读取注册登录服务器端口///////////////////
	ch = 's';
	for (i=0; ch != ','; i++)
	{
		ch=fgetc(pFile);
		port[i] = ch;
	}
	port[--i] = '\0';
	nPort = atoi(port);

	fclose(pFile);
//////////////////初始化ws2_32.dll动态库,创建军套接字//////////////////
	status = WSAStartup(MAKEWORD(1, 1), &Data);
	if (status != 0)
	{
		MessageBox("ERROR:WSAStartup unsccessful!", "闻博QQ", MB_OK);
	}
	destAddr = inet_addr(IP);
	memcpy(&destSockAddr.sin_addr, &destAddr, sizeof(destAddr));
	destSockAddr.sin_port = htons(nPort);
	destSockAddr.sin_family = AF_INET;
	destSocket = socket(AF_INET, SOCK_STREAM, 0);
	if (destSocket == INVALID_SOCKET)
	{
		MessageBox("ERROR:socket unsccessful!", "闻博QQ", MB_OK);
		status = WSACleanup();
		if (status == SOCKET_ERROR)
		{
			MessageBox("ERROR:WSACleanup unsccessful!", "闻博QQ", MB_OK);
		}
		return false;
	}
/////////////////////用套接字连接服务器//////////////////////////////////////////
	status = connect(destSocket, (LPSOCKADDR)&destSockAddr, sizeof(destSockAddr));
	if (status == SOCKET_ERROR)
	{
		MessageBox("ERROR:connect unsccessful!", "闻博QQ", MB_OK);
		status = closesocket(destSocket);
		if (status == SOCKET_ERROR)
		{
			MessageBox("ERROR:closesocket unsccessful!", "闻博QQ", MB_OK);
		}
		status = WSACleanup();
		if (status == SOCKET_ERROR)
		{
			MessageBox("ERROR:WSACleanup unsccessful!", "闻博QQ", MB_OK);
		}
		return false;
	}

	return true;
}

BOOL CWbQQClientDlg::Load()
{
	char sendText[100];
	char recvText[5025];
	int numsnt;
	int numrcv;
	int status;

	sprintf(sendText, "Load:%s,%s", m_strUserName, m_strPassWd);
	numsnt = send(destSocket, sendText, strlen(sendText)+1, NO_FLAGS_SET);
	if (numsnt != (int)strlen(sendText) + 1)
	{
		MessageBox("ERROR:Connection terminated!", "闻博QQ", MB_OK);
		status = closesocket(destSocket);
		if (status == SOCKET_ERROR)
		{
			MessageBox("ERROR:closesocket unsuccessful!", "闻博QQ", MB_OK);
		}
		status = WSACleanup();
		if (status == SOCKET_ERROR)
		{
			MessageBox("ERROR:WSACleanup unsuccessful!", "闻博QQ", MB_OK);
		}
		return FALSE;
	}

	numrcv = recv(destSocket, recvText, 5025, NO_FLAGS_SET);
	if ((numrcv == 0) || (numrcv == SOCKET_ERROR))
	{
		MessageBox("ERROR:Connection terminated!", "闻博QQ", MB_OK);
		status = closesocket(destSocket);
		if (status == SOCKET_ERROR)
		{
			MessageBox("ERROR:closesocket unsuccessful!", "闻博QQ", MB_OK);
		}
		status = WSACleanup();
		if (status == SOCKET_ERROR)
		{
			MessageBox("ERROR:WSACleanup unsuccessful!", "闻博QQ", MB_OK);
		}
		return FALSE;
	}
	recvText[numrcv] = '\0';
///////////////分析注册登录服务器返回标志,如登录成功则写用户数据文件/////////
	char Flag[10];
	char *pos = strchr(recvText, '!');
	pos += 1;
	int lenText = strlen(recvText);
	int lenPos = strlen(pos);
	int len = lenText - lenPos;
	for (int i=0; i<len; i++)
	{
		Flag[i] = recvText[i];
	}
	Flag[len] = '\0';
	if (strcmp(Flag, "success!") != 0)
	{
		return FALSE;
	}
	else
	{
		CString filename;
		filename.Format(".\\data\\%s.db", m_strUserName);
		FILE* file;
		if ((file=fopen(filename, "w")) != NULL)
		{		
			fprintf(file, "%s", pos);
		}
		fclose(file);
	}
	return TRUE;
}

⌨️ 快捷键说明

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