LogOn.cpp
上传用户:hnzycx
上传日期:2022-08-09
资源大小:12567k
文件大小:3k
开发平台:

Visual C++

  1. // LogOn.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "MobileClient.h"
  5. #include "LogOn.h"
  6. #include "TCP.h"
  7. // CLogOn dialog
  8. IMPLEMENT_DYNAMIC(CLogOn, CDialog)
  9. //CLogOn 构造函数
  10. CLogOn::CLogOn(CWnd* pParent /*=NULL*/)
  11. : CDialog(CLogOn::IDD, pParent)
  12. , m_remoteHost(GetLocalIP())
  13. , m_remotePort(2000)
  14. {
  15. }
  16. //CLogOn 析构
  17. CLogOn::~CLogOn()
  18. {
  19. }
  20. void CLogOn::DoDataExchange(CDataExchange* pDX)
  21. {
  22. CDialog::DoDataExchange(pDX);
  23. DDX_Text(pDX, IDC_EDTREMOTEHOST, m_remoteHost);
  24. DDX_Text(pDX, IDC_EDTREMOTEPORT, m_remotePort);
  25. DDX_Control(pDX, IDC_EDIT_NAME, m_editName);
  26. DDX_Control(pDX, IDC_EDIT_PASSWORD, m_editPwd);
  27. }
  28. BEGIN_MESSAGE_MAP(CLogOn, CDialog)
  29. ON_BN_CLICKED(IDOK, &CLogOn::OnBnClickedOk)
  30. END_MESSAGE_MAP()
  31. //回调函数,连接与断开
  32. void CALLBACK CLogOn::OnDisConnect(CWnd* pWnd)
  33. {
  34. CLogOn *pDlg = (CLogOn*)pWnd;
  35. CButton * pBtnConn=(CButton*)pDlg->GetDlgItem(IDOK);
  36. ASSERT(pBtnConn !=NULL);
  37. pBtnConn->EnableWindow(TRUE);
  38. }
  39. //回调函数,线程错误
  40. void CALLBACK CLogOn::OnError(CWnd* pWnd,int nErrorCode)
  41. {
  42. AfxMessageBox(_T("客户端socket发生错误"));
  43. }
  44. //回调函数,读线程
  45. void CALLBACK CLogOn::OnRead(CWnd* pWnd,const char * buf,int len)
  46. {
  47. CMobileClientApp* App=(CMobileClientApp*)AfxGetApp();
  48. CLogOn *pDlg = (CLogOn*)pWnd;
  49. CString strRecv(buf);
  50. if(strRecv==_T("D@1"))
  51. {
  52. App->plg = 2;
  53. }
  54. else 
  55. {
  56. AfxMessageBox(_T("登陆失败"));
  57. }
  58. }
  59. //点击登陆到主界面
  60. void CLogOn::OnBnClickedOk()
  61. {
  62.     UpdateData(TRUE);
  63. //设置m_socketComm属性
  64. CMobileClientApp* App=(CMobileClientApp*)AfxGetApp();
  65. App->m_tcp.m_remoteHost = m_remoteHost;
  66. App->m_tcp.m_port = m_remotePort;
  67. App->m_tcp.OnDisConnect = OnDisConnect;
  68. App->m_tcp.OnRead = OnRead;
  69. App->m_tcp.OnError = OnError;
  70. //打开客户端socket
  71. App->m_tcp.Open(this);
  72. //建立与服务器端的连接
  73. if (App->m_tcp.Connect())
  74. {
  75. CString name;
  76. m_editName.GetWindowTextW(name);
  77. CMobileClientApp* App = (CMobileClientApp*)AfxGetApp();;
  78. App->sername = name;//用户名
  79. CString pwd;//密码
  80. m_editPwd.GetWindowTextW(pwd);
  81. CString out;
  82. out=_T("D@")+name+_T("@")+pwd+_T("@END");
  83. char * sendBuf;
  84. int sendLen=0;
  85. sendLen=out.GetLength();
  86. sendBuf=new char[sendLen*2];
  87. WideCharToMultiByte(CP_OEMCP,NULL,out,-1,sendBuf,sendLen*2,NULL,FALSE);
  88. if (!App->m_tcp.SendData(sendBuf,sendLen*2))
  89. {
  90. AfxMessageBox(_T("发送失败"));
  91. }
  92. delete []sendBuf;
  93. sendBuf=NULL;
  94. Sleep(3000);
  95.     if(App->plg==2)
  96.         OnOK();
  97. }
  98. else
  99. {
  100. AfxMessageBox(_T("建立连接失败"));
  101. return;
  102. }
  103. }
  104. //得到本地ip
  105. CString CLogOn::GetLocalIP()
  106. {
  107. HOSTENT *LocalAddress;
  108. char *Buff;
  109. TCHAR *wBuff;
  110. CString strReturn=_T("");
  111. Buff=new char[256];
  112. wBuff=new TCHAR[256];
  113. memset(Buff,'',256);
  114. memset(wBuff,TEXT(''),256*sizeof(TCHAR));
  115. if(gethostname(Buff,256)==0)
  116. {
  117. mbstowcs(wBuff,Buff,256);
  118. LocalAddress=gethostbyname(Buff);
  119. memset(Buff,'',256);
  120. sprintf(Buff,"%d.%d.%d.%d",LocalAddress->h_addr_list[0][0]&0xFF,LocalAddress->h_addr_list[0][1]&0x00FF,
  121. LocalAddress->h_addr_list[0][2]&0x0000FF,LocalAddress->h_addr_list[0][3]&0x000000FF);
  122. memset(wBuff,TEXT(''),256*sizeof(TCHAR));
  123. mbstowcs(wBuff,Buff,256);
  124. strReturn=wBuff;
  125. }
  126. else
  127. {
  128. }
  129. delete[] Buff;
  130. delete[] wBuff;
  131. return strReturn;
  132. }