ChatDlg.cpp
上传用户:dmjnay
上传日期:2022-08-02
资源大小:9k
文件大小:6k
源码类别:

网络编程

开发平台:

Visual C++

  1. // ChatDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "Chat.h"
  5. #include "ChatDlg.h"
  6. #include<Winsock2.h>
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CAboutDlg dialog used for App About
  14. class CAboutDlg : public CDialog
  15. {
  16. public:
  17. CAboutDlg();
  18. // Dialog Data
  19. //{{AFX_DATA(CAboutDlg)
  20. enum { IDD = IDD_ABOUTBOX };
  21. //}}AFX_DATA
  22. // ClassWizard generated virtual function overrides
  23. //{{AFX_VIRTUAL(CAboutDlg)
  24. protected:
  25. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  26. //}}AFX_VIRTUAL
  27. // Implementation
  28. protected:
  29. //{{AFX_MSG(CAboutDlg)
  30. //}}AFX_MSG
  31. DECLARE_MESSAGE_MAP()
  32. };
  33. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  34. {
  35. //{{AFX_DATA_INIT(CAboutDlg)
  36. //}}AFX_DATA_INIT
  37. }
  38. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  39. {
  40. CDialog::DoDataExchange(pDX);
  41. //{{AFX_DATA_MAP(CAboutDlg)
  42. //}}AFX_DATA_MAP
  43. }
  44. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  45. //{{AFX_MSG_MAP(CAboutDlg)
  46. // No message handlers
  47. //}}AFX_MSG_MAP
  48. END_MESSAGE_MAP()
  49. /////////////////////////////////////////////////////////////////////////////
  50. // CChatDlg dialog
  51. CChatDlg::CChatDlg(CWnd* pParent /*=NULL*/)
  52. : CDialog(CChatDlg::IDD, pParent)
  53. {
  54. //{{AFX_DATA_INIT(CChatDlg)
  55. // NOTE: the ClassWizard will add member initialization here
  56. //}}AFX_DATA_INIT
  57. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  58. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  59. }
  60. void CChatDlg::DoDataExchange(CDataExchange* pDX)
  61. {
  62. DDX_Control(pDX,IDC_EDIT_RECV,m_yourEdit);
  63. CDialog::DoDataExchange(pDX);
  64. //{{AFX_DATA_MAP(CChatDlg)
  65. // NOTE: the ClassWizard will add DDX and DDV calls here
  66. //}}AFX_DATA_MAP
  67. }
  68. BEGIN_MESSAGE_MAP(CChatDlg, CDialog)
  69. //{{AFX_MSG_MAP(CChatDlg)
  70. ON_WM_SYSCOMMAND()
  71. ON_WM_PAINT()
  72. ON_WM_QUERYDRAGICON()
  73. ON_BN_CLICKED(IDC_BTN_SEND, OnBtnSend)
  74. ON_MESSAGE(WM_RECVDATA,OnRecvData)
  75. //}}AFX_MSG_MAP
  76. ON_BN_CLICKED(IDCANCEL, &CChatDlg::OnBnClickedCancel)
  77. //ON_EN_CHANGE(IDC_EDIT_RECV, &CChatDlg::OnEnChangeEditRecv)
  78. END_MESSAGE_MAP()
  79. /////////////////////////////////////////////////////////////////////////////
  80. // CChatDlg message handlers
  81. BOOL CChatDlg::OnInitDialog()
  82. {
  83. CDialog::OnInitDialog();
  84. // Add "About..." menu item to system menu.
  85. // IDM_ABOUTBOX must be in the system command range.
  86. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  87. ASSERT(IDM_ABOUTBOX < 0xF000);
  88. CMenu* pSysMenu = GetSystemMenu(FALSE);
  89. if (pSysMenu != NULL)
  90. {
  91. CString strAboutMenu;
  92. strAboutMenu.LoadString(IDS_ABOUTBOX);
  93. if (!strAboutMenu.IsEmpty())
  94. {
  95. pSysMenu->AppendMenu(MF_SEPARATOR);
  96. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  97. }
  98. }
  99. // Set the icon for this dialog.  The framework does this automatically
  100. //  when the application's main window is not a dialog
  101. SetIcon(m_hIcon, TRUE); // Set big icon
  102. SetIcon(m_hIcon, FALSE); // Set small icon
  103. // TODO: Add extra initialization here
  104. InitSocket();
  105. RECVPARAM *pRecvParam=new RECVPARAM;
  106. pRecvParam->sock=m_socket;
  107. pRecvParam->hwnd=m_hWnd;
  108. HANDLE hThread=CreateThread(NULL,0,RecvProc,(LPVOID)pRecvParam,0,NULL);
  109. CloseHandle(hThread);
  110. return TRUE;  // return TRUE  unless you set the focus to a control
  111. }
  112. void CChatDlg::OnSysCommand(UINT nID, LPARAM lParam)
  113. {
  114. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  115. {
  116. CAboutDlg dlgAbout;
  117. dlgAbout.DoModal();
  118. }
  119. else
  120. {
  121. CDialog::OnSysCommand(nID, lParam);
  122. }
  123. }
  124. void CChatDlg::OnPaint() 
  125. {
  126. if (IsIconic())
  127. {
  128. CPaintDC dc(this); // device context for painting
  129. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  130. // Center icon in client rectangle
  131. int cxIcon = GetSystemMetrics(SM_CXICON);
  132. int cyIcon = GetSystemMetrics(SM_CYICON);
  133. CRect rect;
  134. GetClientRect(&rect);
  135. int x = (rect.Width() - cxIcon + 1) / 2;
  136. int y = (rect.Height() - cyIcon + 1) / 2;
  137. // Draw the icon
  138. dc.DrawIcon(x, y, m_hIcon);
  139. }
  140. else
  141. {
  142. CDialog::OnPaint();
  143. }
  144. }
  145. HCURSOR CChatDlg::OnQueryDragIcon()
  146. {
  147. return (HCURSOR) m_hIcon;
  148. }
  149. BOOL CChatDlg::InitSocket()
  150. {
  151. m_socket=socket(AF_INET,SOCK_DGRAM,0);
  152. if(INVALID_SOCKET==m_socket)
  153. {
  154. MessageBox("套接字创建失败!");
  155. return FALSE;
  156. }
  157. SOCKADDR_IN addrSock;
  158. addrSock.sin_family=AF_INET;
  159. addrSock.sin_port=htons(6000);
  160. addrSock.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
  161. int retval;
  162. BOOL val=TRUE;
  163. setsockopt(m_socket,SOL_SOCKET,SO_REUSEADDR,(char*)&val,sizeof(val));
  164. retval=bind(m_socket,(SOCKADDR*)&addrSock,sizeof(SOCKADDR));
  165. if(SOCKET_ERROR==retval)
  166. {
  167. closesocket(m_socket);
  168. MessageBox("绑定失败!");
  169. return FALSE;
  170. }
  171. return TRUE;
  172. }
  173. DWORD WINAPI CChatDlg::RecvProc(LPVOID lpParameter)
  174. {
  175. SOCKET sock=((RECVPARAM*)lpParameter)->sock;
  176. HWND hwnd=((RECVPARAM*)lpParameter)->hwnd;
  177. delete lpParameter;
  178. SOCKADDR_IN addrFrom;
  179. int len=sizeof(SOCKADDR);
  180. char recvBuf[200];
  181. char tempBuf[300];
  182. int retval;
  183. while(TRUE)
  184. {
  185. retval=recvfrom(sock,recvBuf,200,0,(SOCKADDR*)&addrFrom,&len);
  186. if(SOCKET_ERROR==retval)
  187. {
  188. int code=WSAGetLastError ();
  189. break;}
  190. sprintf(tempBuf,"%s说: %s",inet_ntoa(addrFrom.sin_addr),recvBuf);
  191. ::PostMessage(hwnd,WM_RECVDATA,0,(LPARAM)tempBuf);
  192. }
  193. return 0;
  194. }
  195. LRESULT CChatDlg::OnRecvData(WPARAM wParam,LPARAM lParam)
  196. {
  197. CString str=(char*)lParam;
  198. CString strTemp;
  199. GetDlgItemText(IDC_EDIT_RECV,strTemp);
  200. str+="rn";
  201. str+=strTemp;
  202. SetDlgItemText(IDC_EDIT_RECV,str);
  203.     return 1;
  204. }
  205. void CChatDlg::OnBtnSend() 
  206. {
  207. // TODO: Add your control notification handler code here
  208. DWORD dwIP;
  209. ((CIPAddressCtrl*)GetDlgItem(IDC_IPADDRESS1))->GetAddress(dwIP);
  210. SOCKADDR_IN addrTo;
  211. addrTo.sin_family=AF_INET;
  212. addrTo.sin_port=htons(6000);
  213. addrTo.sin_addr.S_un.S_addr=htonl(dwIP);
  214. CString strSend;
  215. //CString str;
  216. CString strRecv;
  217. GetDlgItemText(IDC_EDIT_SEND,strSend);
  218. sendto(m_socket,strSend,strSend.GetLength()+1,0,
  219. (SOCKADDR*)&addrTo,sizeof(SOCKADDR));
  220. /*SetDlgItemText(IDC_EDIT_RECV,"rn");
  221. SetDlgItemText(IDC_EDIT_RECV,"我说:");*/
  222. GetDlgItemText(IDC_EDIT_RECV,strRecv);
  223. strRecv+="我说:";
  224. strSend+="rn";
  225. //strSend+=strRecv;
  226. strRecv+=strSend;
  227. SetDlgItemText(IDC_EDIT_RECV,strRecv);
  228. SetDlgItemText(IDC_EDIT_SEND,"");
  229. m_yourEdit.LineScroll(m_yourEdit.GetLineCount()-1,0);
  230. }
  231. void CChatDlg::OnBnClickedCancel()
  232. {
  233. // TODO: 在此添加控件通知处理程序代码
  234. OnCancel();
  235. }