TalkDlg.cpp
上传用户:onsales
上传日期:2010-01-31
资源大小:224k
文件大小:7k
源码类别:

网络编程

开发平台:

Visual C++

  1. // TalkDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "Talk.h"
  5. #include "TalkDlg.h"
  6. #include "fstream.h"
  7. #include "Message.h"
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13. /////////////////////////////////////////////////////////////////////////////
  14. // CAboutDlg dialog used for App About
  15. class CAboutDlg : public CDialog
  16. {
  17. public:
  18. CAboutDlg();
  19. // Dialog Data
  20. //{{AFX_DATA(CAboutDlg)
  21. enum { IDD = IDD_ABOUTBOX };
  22. //}}AFX_DATA
  23. // ClassWizard generated virtual function overrides
  24. //{{AFX_VIRTUAL(CAboutDlg)
  25. protected:
  26. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  27. //}}AFX_VIRTUAL
  28. // Implementation
  29. protected:
  30. //{{AFX_MSG(CAboutDlg)
  31. //}}AFX_MSG
  32. DECLARE_MESSAGE_MAP()
  33. };
  34. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  35. {
  36. //{{AFX_DATA_INIT(CAboutDlg)
  37. //}}AFX_DATA_INIT
  38. }
  39. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  40. {
  41. CDialog::DoDataExchange(pDX);
  42. //{{AFX_DATA_MAP(CAboutDlg)
  43. //}}AFX_DATA_MAP
  44. }
  45. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  46. //{{AFX_MSG_MAP(CAboutDlg)
  47. // No message handlers
  48. //}}AFX_MSG_MAP
  49. END_MESSAGE_MAP()
  50. /////////////////////////////////////////////////////////////////////////////
  51. // CTalkDlg dialog
  52. CTalkDlg::CTalkDlg(CWnd* pParent /*=NULL*/)
  53. : CDialog(CTalkDlg::IDD, pParent)
  54. {
  55. //{{AFX_DATA_INIT(CTalkDlg)
  56. m_NAME = _T("");
  57. m_Talk = _T("");
  58. //}}AFX_DATA_INIT
  59. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  60. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  61. }
  62. void CTalkDlg::DoDataExchange(CDataExchange* pDX)
  63. {
  64. CDialog::DoDataExchange(pDX);
  65. //{{AFX_DATA_MAP(CTalkDlg)
  66. DDX_Control(pDX, IDOK, m_SendMSG);
  67. DDX_Control(pDX, IDC_ServerIPADDRESS, m_ServerIPADDRESS);
  68. DDX_Text(pDX, IDC_NAME, m_NAME);
  69. DDX_Text(pDX, IDC_TALK, m_Talk);
  70. //}}AFX_DATA_MAP
  71. }
  72. BEGIN_MESSAGE_MAP(CTalkDlg, CDialog)
  73. //{{AFX_MSG_MAP(CTalkDlg)
  74. ON_WM_SYSCOMMAND()
  75. ON_WM_PAINT()
  76. ON_WM_QUERYDRAGICON()
  77. //}}AFX_MSG_MAP
  78. END_MESSAGE_MAP()
  79. /////////////////////////////////////////////////////////////////////////////
  80. // CTalkDlg message handlers
  81. BOOL CTalkDlg::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. IPstr="127.0.0.1";
  105. sign=FALSE;
  106. CFile MyFile;
  107. MyFile.Open("msg.txt",CFile::modeRead);
  108.     int filelong=0;
  109. filelong=MyFile.GetLength();
  110. char pbuf[1000];
  111. MyFile.Read( pbuf, sizeof( pbuf ) ); 
  112. //if(filelong<256) 
  113. MyFile.Close();
  114. pbuf[filelong]='';
  115. IPstr.Empty();
  116. IPstr="";
  117. m_NAME.Empty();
  118. m_NAME="";
  119. int i=2;
  120. while(pbuf[i]!='#')
  121. {
  122. IPstr+=pbuf[i];
  123. i++;
  124. }
  125. i++;
  126. while(pbuf[i]!='#')
  127. {
  128. m_NAME+=pbuf[i];
  129. i++;
  130. }
  131. if(pbuf[0]=='1')//说明传递的是消息
  132. { i++;
  133. while(pbuf[i]!='#')
  134. {
  135. m_Talk+=pbuf[i];
  136. i++;
  137. }
  138. CEdit* myedit;
  139. myedit=(CEdit*) GetDlgItem(IDC_TALK);
  140. myedit->SetReadOnly(TRUE);
  141. m_SendMSG.SetWindowText("回复消息");
  142. sign=TRUE;
  143. //SetDlgItemText(IDC_SendBTN,"回复消息");
  144. }
  145. //IPstr="192.168.1.98";
  146. msg.sign=1;
  147. this->UpdateData(false);
  148. m_ServerIPADDRESS.SetWindowText(IPstr);
  149. GetIP_Name();
  150. return TRUE;  // return TRUE  unless you set the focus to a control
  151. }
  152. void CTalkDlg::OnSysCommand(UINT nID, LPARAM lParam)
  153. {
  154. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  155. {
  156. CAboutDlg dlgAbout;
  157. dlgAbout.DoModal();
  158. }
  159. else
  160. {
  161. CDialog::OnSysCommand(nID, lParam);
  162. }
  163. }
  164. // If you add a minimize button to your dialog, you will need the code below
  165. //  to draw the icon.  For MFC applications using the document/view model,
  166. //  this is automatically done for you by the framework.
  167. void CTalkDlg::OnPaint() 
  168. {
  169. if (IsIconic())
  170. {
  171. CPaintDC dc(this); // device context for painting
  172. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  173. // Center icon in client rectangle
  174. int cxIcon = GetSystemMetrics(SM_CXICON);
  175. int cyIcon = GetSystemMetrics(SM_CYICON);
  176. CRect rect;
  177. GetClientRect(&rect);
  178. int x = (rect.Width() - cxIcon + 1) / 2;
  179. int y = (rect.Height() - cyIcon + 1) / 2;
  180. // Draw the icon
  181. dc.DrawIcon(x, y, m_hIcon);
  182. }
  183. else
  184. {
  185. CDialog::OnPaint();
  186. }
  187. }
  188. // The system calls this to obtain the cursor to display while the user drags
  189. //  the minimized window.
  190. HCURSOR CTalkDlg::OnQueryDragIcon()
  191. {
  192. return (HCURSOR) m_hIcon;
  193. }
  194. BOOL CTalkDlg::DestroyWindow() 
  195. {
  196. // TODO: Add your specialized code here and/or call the base class
  197. WSACleanup();//断开连接
  198. return CDialog::DestroyWindow();
  199. }
  200. void CTalkDlg::OnOK() 
  201. {
  202. // TODO: Add extra validation here
  203. if(sign)
  204. {
  205. CEdit* myedit;
  206. myedit=(CEdit*) GetDlgItem(IDC_TALK);
  207. myedit->SetReadOnly(FALSE);
  208. m_SendMSG.SetWindowText("发送");
  209. sign=FALSE;
  210. m_Talk.Empty();
  211. UpdateData(FALSE);
  212. ((CEdit *)GetDlgItem(IDC_TALK))->SetFocus();
  213. return;
  214. }
  215. LPCTSTR ip;
  216. ip=IPstr;
  217. WSADATA wsaData;
  218. if(WSAStartup(MAKEWORD(2,1),&wsaData))//调用Windows Sockets DLL
  219. {
  220. MessageBox("WinSock 无法初始化!","错误信息",NULL);
  221. return;
  222. }
  223. DWORD dwip;
  224. if((dwip=inet_addr(ip))==INADDR_NONE)
  225. {
  226. MessageBox("IP有误","错误信息",NULL);
  227. return;
  228. }
  229. ServerSocket=socket(PF_INET,SOCK_STREAM,0);//创建服务器Socket
  230. if(ServerSocket==INVALID_SOCKET)
  231. {
  232. MessageBox("无法创建服务器Socket","错误信息",NULL);
  233. return;
  234. }
  235. sockStruct.sin_family=AF_INET;//TCP/IP 协议
  236. sockStruct.sin_port=htons(8001);//端口
  237. sockStruct.sin_addr.S_un.S_addr=dwip;
  238. if(connect(ServerSocket,(LPSOCKADDR)&sockStruct,sizeof(sockStruct))==SOCKET_ERROR)
  239. {
  240. MessageBox("对方已经下线!","错误信息",NULL);
  241. return;
  242. }
  243. //MessageBox("登录成功!","成功信息",NULL);
  244. //WSAAsyncSelect(ServerSocket,m_hWnd,WM_CLIENT_READCLOSE,FD_READ|FD_CLOSE);
  245. // 产生相应传递给窗口的消息为WM_SERVER_ACCEPT ,这是自定义消息
  246. UpdateData(TRUE);
  247. if(m_Talk.IsEmpty())
  248. {
  249. MessageBox("不允许发送空消息","错误信息",NULL);
  250. return;
  251. }
  252. msg.sign=1;
  253. m_Talk=IPName+"#"+HostName+"#"+m_Talk+"#";
  254. strcpy(msg.msg,(LPCTSTR)m_Talk);
  255. if(send(ServerSocket,(char *)&msg,sizeof(msg),0)==SOCKET_ERROR)
  256. {
  257. MessageBox("发送消息失败","错误信息",MB_OK);
  258. return;
  259. }
  260. //m_TalkList.InsertString(0,m_Talk);
  261. m_Talk.Empty();
  262. UpdateData(FALSE);
  263. CDialog::OnOK();
  264. }
  265. void CTalkDlg::GetIP_Name()
  266. {
  267. WORD wVersionRequested;
  268. WSADATA wsaData;
  269. char hostname[128];
  270. CString strip;
  271. PHOSTENT hostinfo;
  272. wVersionRequested=MAKEWORD(2,0);
  273. if(WSAStartup(wVersionRequested,&wsaData)==0)
  274. {
  275. if(gethostname(hostname,sizeof(hostname))==0)
  276. {
  277. if((hostinfo=gethostbyname(hostname))!=NULL)
  278. {
  279. strip=inet_ntoa(*(struct in_addr *)*hostinfo->h_addr_list);
  280. }
  281. }
  282. WSACleanup();
  283. }
  284. IPName=strip;
  285. HostName.Format("%s",hostname);
  286. }