LogOn.cpp
上传用户:hnzycx
上传日期:2022-08-09
资源大小:12567k
文件大小:3k
- // LogOn.cpp : implementation file
- //
- #include "stdafx.h"
- #include "MobileClient.h"
- #include "LogOn.h"
- #include "TCP.h"
- // CLogOn dialog
- IMPLEMENT_DYNAMIC(CLogOn, CDialog)
- //CLogOn 构造函数
- CLogOn::CLogOn(CWnd* pParent /*=NULL*/)
- : CDialog(CLogOn::IDD, pParent)
- , m_remoteHost(GetLocalIP())
- , m_remotePort(2000)
- {
- }
- //CLogOn 析构
- CLogOn::~CLogOn()
- {
- }
- void CLogOn::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- DDX_Text(pDX, IDC_EDTREMOTEHOST, m_remoteHost);
- DDX_Text(pDX, IDC_EDTREMOTEPORT, m_remotePort);
- DDX_Control(pDX, IDC_EDIT_NAME, m_editName);
- DDX_Control(pDX, IDC_EDIT_PASSWORD, m_editPwd);
- }
- BEGIN_MESSAGE_MAP(CLogOn, CDialog)
- ON_BN_CLICKED(IDOK, &CLogOn::OnBnClickedOk)
- END_MESSAGE_MAP()
- //回调函数,连接与断开
- void CALLBACK CLogOn::OnDisConnect(CWnd* pWnd)
- {
- CLogOn *pDlg = (CLogOn*)pWnd;
-
- CButton * pBtnConn=(CButton*)pDlg->GetDlgItem(IDOK);
- ASSERT(pBtnConn !=NULL);
- pBtnConn->EnableWindow(TRUE);
- }
- //回调函数,线程错误
- void CALLBACK CLogOn::OnError(CWnd* pWnd,int nErrorCode)
- {
- AfxMessageBox(_T("客户端socket发生错误"));
- }
- //回调函数,读线程
- void CALLBACK CLogOn::OnRead(CWnd* pWnd,const char * buf,int len)
- {
- CMobileClientApp* App=(CMobileClientApp*)AfxGetApp();
- CLogOn *pDlg = (CLogOn*)pWnd;
- CString strRecv(buf);
- if(strRecv==_T("D@1"))
- {
- App->plg = 2;
- }
- else
- {
- AfxMessageBox(_T("登陆失败"));
- }
- }
- //点击登陆到主界面
- void CLogOn::OnBnClickedOk()
- {
- UpdateData(TRUE);
- //设置m_socketComm属性
- CMobileClientApp* App=(CMobileClientApp*)AfxGetApp();
- App->m_tcp.m_remoteHost = m_remoteHost;
- App->m_tcp.m_port = m_remotePort;
- App->m_tcp.OnDisConnect = OnDisConnect;
- App->m_tcp.OnRead = OnRead;
- App->m_tcp.OnError = OnError;
- //打开客户端socket
- App->m_tcp.Open(this);
- //建立与服务器端的连接
- if (App->m_tcp.Connect())
- {
- CString name;
- m_editName.GetWindowTextW(name);
- CMobileClientApp* App = (CMobileClientApp*)AfxGetApp();;
- App->sername = name;//用户名
- CString pwd;//密码
- m_editPwd.GetWindowTextW(pwd);
- CString out;
- out=_T("D@")+name+_T("@")+pwd+_T("@END");
- char * sendBuf;
- int sendLen=0;
- sendLen=out.GetLength();
- sendBuf=new char[sendLen*2];
- WideCharToMultiByte(CP_OEMCP,NULL,out,-1,sendBuf,sendLen*2,NULL,FALSE);
- if (!App->m_tcp.SendData(sendBuf,sendLen*2))
- {
- AfxMessageBox(_T("发送失败"));
- }
- delete []sendBuf;
- sendBuf=NULL;
- Sleep(3000);
- if(App->plg==2)
- OnOK();
- }
- else
- {
- AfxMessageBox(_T("建立连接失败"));
- return;
- }
- }
- //得到本地ip
- CString CLogOn::GetLocalIP()
- {
- HOSTENT *LocalAddress;
- char *Buff;
- TCHAR *wBuff;
- CString strReturn=_T("");
- Buff=new char[256];
- wBuff=new TCHAR[256];
- memset(Buff,' ',256);
- memset(wBuff,TEXT(' '),256*sizeof(TCHAR));
- if(gethostname(Buff,256)==0)
- {
- mbstowcs(wBuff,Buff,256);
- LocalAddress=gethostbyname(Buff);
- memset(Buff,' ',256);
- sprintf(Buff,"%d.%d.%d.%d ",LocalAddress->h_addr_list[0][0]&0xFF,LocalAddress->h_addr_list[0][1]&0x00FF,
- LocalAddress->h_addr_list[0][2]&0x0000FF,LocalAddress->h_addr_list[0][3]&0x000000FF);
- memset(wBuff,TEXT(' '),256*sizeof(TCHAR));
- mbstowcs(wBuff,Buff,256);
- strReturn=wBuff;
- }
- else
- {
- }
- delete[] Buff;
- delete[] wBuff;
- return strReturn;
- }