TalkDlg.cpp
资源名称:多功能聊天程序.rar [点击查看]
上传用户:onsales
上传日期:2010-01-31
资源大小:224k
文件大小:7k
源码类别:
网络编程
开发平台:
Visual C++
- // TalkDlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "Talk.h"
- #include "TalkDlg.h"
- #include "fstream.h"
- #include "Message.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CAboutDlg dialog used for App About
- class CAboutDlg : public CDialog
- {
- public:
- CAboutDlg();
- // Dialog Data
- //{{AFX_DATA(CAboutDlg)
- enum { IDD = IDD_ABOUTBOX };
- //}}AFX_DATA
- // ClassWizard generated virtual function overrides
- //{{AFX_VIRTUAL(CAboutDlg)
- protected:
- virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
- //}}AFX_VIRTUAL
- // Implementation
- protected:
- //{{AFX_MSG(CAboutDlg)
- //}}AFX_MSG
- DECLARE_MESSAGE_MAP()
- };
- CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
- {
- //{{AFX_DATA_INIT(CAboutDlg)
- //}}AFX_DATA_INIT
- }
- void CAboutDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CAboutDlg)
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
- //{{AFX_MSG_MAP(CAboutDlg)
- // No message handlers
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CTalkDlg dialog
- CTalkDlg::CTalkDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CTalkDlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CTalkDlg)
- m_NAME = _T("");
- m_Talk = _T("");
- //}}AFX_DATA_INIT
- // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- }
- void CTalkDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CTalkDlg)
- DDX_Control(pDX, IDOK, m_SendMSG);
- DDX_Control(pDX, IDC_ServerIPADDRESS, m_ServerIPADDRESS);
- DDX_Text(pDX, IDC_NAME, m_NAME);
- DDX_Text(pDX, IDC_TALK, m_Talk);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CTalkDlg, CDialog)
- //{{AFX_MSG_MAP(CTalkDlg)
- ON_WM_SYSCOMMAND()
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CTalkDlg message handlers
- BOOL CTalkDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- // Add "About..." menu item to system menu.
- // IDM_ABOUTBOX must be in the system command range.
- ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
- ASSERT(IDM_ABOUTBOX < 0xF000);
- CMenu* pSysMenu = GetSystemMenu(FALSE);
- if (pSysMenu != NULL)
- {
- CString strAboutMenu;
- strAboutMenu.LoadString(IDS_ABOUTBOX);
- if (!strAboutMenu.IsEmpty())
- {
- pSysMenu->AppendMenu(MF_SEPARATOR);
- pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
- }
- }
- // 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
- IPstr="127.0.0.1";
- sign=FALSE;
- CFile MyFile;
- MyFile.Open("msg.txt",CFile::modeRead);
- int filelong=0;
- filelong=MyFile.GetLength();
- char pbuf[1000];
- MyFile.Read( pbuf, sizeof( pbuf ) );
- //if(filelong<256)
- MyFile.Close();
- pbuf[filelong]=' ';
- IPstr.Empty();
- IPstr="";
- m_NAME.Empty();
- m_NAME="";
- int i=2;
- while(pbuf[i]!='#')
- {
- IPstr+=pbuf[i];
- i++;
- }
- i++;
- while(pbuf[i]!='#')
- {
- m_NAME+=pbuf[i];
- i++;
- }
- if(pbuf[0]=='1')//说明传递的是消息
- { i++;
- while(pbuf[i]!='#')
- {
- m_Talk+=pbuf[i];
- i++;
- }
- CEdit* myedit;
- myedit=(CEdit*) GetDlgItem(IDC_TALK);
- myedit->SetReadOnly(TRUE);
- m_SendMSG.SetWindowText("回复消息");
- sign=TRUE;
- //SetDlgItemText(IDC_SendBTN,"回复消息");
- }
- //IPstr="192.168.1.98";
- msg.sign=1;
- this->UpdateData(false);
- m_ServerIPADDRESS.SetWindowText(IPstr);
- GetIP_Name();
- return TRUE; // return TRUE unless you set the focus to a control
- }
- void CTalkDlg::OnSysCommand(UINT nID, LPARAM lParam)
- {
- if ((nID & 0xFFF0) == IDM_ABOUTBOX)
- {
- CAboutDlg dlgAbout;
- dlgAbout.DoModal();
- }
- else
- {
- CDialog::OnSysCommand(nID, lParam);
- }
- }
- // 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 CTalkDlg::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 CTalkDlg::OnQueryDragIcon()
- {
- return (HCURSOR) m_hIcon;
- }
- BOOL CTalkDlg::DestroyWindow()
- {
- // TODO: Add your specialized code here and/or call the base class
- WSACleanup();//断开连接
- return CDialog::DestroyWindow();
- }
- void CTalkDlg::OnOK()
- {
- // TODO: Add extra validation here
- if(sign)
- {
- CEdit* myedit;
- myedit=(CEdit*) GetDlgItem(IDC_TALK);
- myedit->SetReadOnly(FALSE);
- m_SendMSG.SetWindowText("发送");
- sign=FALSE;
- m_Talk.Empty();
- UpdateData(FALSE);
- ((CEdit *)GetDlgItem(IDC_TALK))->SetFocus();
- return;
- }
- LPCTSTR ip;
- ip=IPstr;
- WSADATA wsaData;
- if(WSAStartup(MAKEWORD(2,1),&wsaData))//调用Windows Sockets DLL
- {
- MessageBox("WinSock 无法初始化!","错误信息",NULL);
- return;
- }
- DWORD dwip;
- if((dwip=inet_addr(ip))==INADDR_NONE)
- {
- MessageBox("IP有误","错误信息",NULL);
- return;
- }
- ServerSocket=socket(PF_INET,SOCK_STREAM,0);//创建服务器Socket
- if(ServerSocket==INVALID_SOCKET)
- {
- MessageBox("无法创建服务器Socket","错误信息",NULL);
- return;
- }
- sockStruct.sin_family=AF_INET;//TCP/IP 协议
- sockStruct.sin_port=htons(8001);//端口
- sockStruct.sin_addr.S_un.S_addr=dwip;
- if(connect(ServerSocket,(LPSOCKADDR)&sockStruct,sizeof(sockStruct))==SOCKET_ERROR)
- {
- MessageBox("对方已经下线!","错误信息",NULL);
- return;
- }
- //MessageBox("登录成功!","成功信息",NULL);
- //WSAAsyncSelect(ServerSocket,m_hWnd,WM_CLIENT_READCLOSE,FD_READ|FD_CLOSE);
- // 产生相应传递给窗口的消息为WM_SERVER_ACCEPT ,这是自定义消息
- UpdateData(TRUE);
- if(m_Talk.IsEmpty())
- {
- MessageBox("不允许发送空消息","错误信息",NULL);
- return;
- }
- msg.sign=1;
- m_Talk=IPName+"#"+HostName+"#"+m_Talk+"#";
- strcpy(msg.msg,(LPCTSTR)m_Talk);
- if(send(ServerSocket,(char *)&msg,sizeof(msg),0)==SOCKET_ERROR)
- {
- MessageBox("发送消息失败","错误信息",MB_OK);
- return;
- }
- //m_TalkList.InsertString(0,m_Talk);
- m_Talk.Empty();
- UpdateData(FALSE);
- CDialog::OnOK();
- }
- void CTalkDlg::GetIP_Name()
- {
- WORD wVersionRequested;
- WSADATA wsaData;
- char hostname[128];
- CString strip;
- PHOSTENT hostinfo;
- wVersionRequested=MAKEWORD(2,0);
- if(WSAStartup(wVersionRequested,&wsaData)==0)
- {
- if(gethostname(hostname,sizeof(hostname))==0)
- {
- if((hostinfo=gethostbyname(hostname))!=NULL)
- {
- strip=inet_ntoa(*(struct in_addr *)*hostinfo->h_addr_list);
- }
- }
- WSACleanup();
- }
- IPName=strip;
- HostName.Format("%s",hostname);
- }