1、客户端应用程序开发
建立一个基于对话框的MFC应用程序,创建的时候记得勾选【Windows Sockets】,其
他的默认就行。
(1)、对话框如图所示:
(2)代码如下:
// ChatClientDlg.cpp : implementation file
//#include "stdafx.h"
#include "ChatClient.h"
#include "ChatClientDlg.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/
// CAboutDlg dialog used for App Aboutclass 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_MSGDECLARE_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()/
// CChatClientDlg dialogCChatClientDlg::CChatClientDlg(CWnd* pParent /*=NULL*/): CDialog(CChatClientDlg::IDD, pParent)
{//{{AFX_DATA_INIT(CChatClientDlg)m_sWords = _T("");//}}AFX_DATA_INIT// Note that LoadIcon does not require a subsequent DestroyIcon in Win32m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}void CChatClientDlg::DoDataExchange(CDataExchange* pDX)
{CDialog::DoDataExchange(pDX);//{{AFX_DATA_MAP(CChatClientDlg)DDX_Control(pDX, IDC_LIST_WORDS, m_ListWords);DDX_Control(pDX, IDC_IPADDRESS_SERVER, ServerIP);DDX_Control(pDX, IDC_BUTTON_SEND, m_ButtonSend);DDX_Control(pDX, IDC_BUTTON_CONNECT, m_ButtonConnect);DDX_Text(pDX, IDC_EDIT_WORDS, m_sWords);//}}AFX_DATA_MAP
}BEGIN_MESSAGE_MAP(CChatClientDlg, CDialog)//{{AFX_MSG_MAP(CChatClientDlg)ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_BN_CLICKED(IDC_BUTTON_CONNECT, OnButtonConnect)ON_BN_CLICKED(IDC_BUTTON_SEND, OnButtonSend)ON_BN_CLICKED(IDC_BUTTON_CLEAR, OnButtonClear)ON_BN_CLICKED(IDC_BUTTON_ABOUT, OnButtonAbout)ON_BN_CLICKED(IDC_BUTTON_EXIT, OnButtonExit)//}}AFX_MSG_MAP
END_MESSAGE_MAP()/
// CChatClientDlg message handlersBOOL CChatClientDlg::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 dialogSetIcon(m_hIcon, TRUE); // Set big iconSetIcon(m_hIcon, FALSE); // Set small icon// TODO: Add extra initialization herereturn TRUE; // return TRUE unless you set the focus to a control
}void CChatClientDlg::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 CChatClientDlg::OnPaint()
{if (IsIconic()){CPaintDC dc(this); // device context for paintingSendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);// Center icon in client rectangleint 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 icondc.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 CChatClientDlg::OnQueryDragIcon()
{return (HCURSOR) m_hIcon;
}void CChatClientDlg::OnButtonConnect()
{// TODO: Add your control notification handler code hereBYTE nFild[4];UpdateData();ServerIP.GetAddress(nFild[0],nFild[1],nFild[2],nFild[3]);CString sIP;sIP.Format("%d.%d.%d.%d",nFild[0],nFild[1],nFild[2],nFild[3]);m_ClientSocket.Create(); // 创建客户端Socketm_ClientSocket.Connect(sIP,14875);}void CChatClientDlg::OnButtonSend()
{// TODO: Add your control notification handler code hereUpdateData();m_ClientSocket.Send(m_sWords,m_sWords.GetLength()); //获取文本长度m_ListWords.AddString("发送:"+m_sWords); //显示发送文件长度记录m_ListWords.SetTopIndex(m_ListWords.GetCount()-1); }void CChatClientDlg::OnButtonClear()
{// TODO: Add your control notification handler code herem_ListWords.ResetContent(); //清空聊天记录}void CChatClientDlg::OnButtonAbout()
{// TODO: Add your control notification handler code hereCAboutDlg dlgAbout; dlgAbout.DoModal(); //显示"关于"对话框}void CChatClientDlg::OnButtonExit()
{// TODO: Add your control notification handler code hereCDialog::OnCancel();}
// ClientSocket.cpp : implementation file
//#include "stdafx.h"
#include "ChatClient.h"
#include "ClientSocket.h"
#include "ChatClientDlg.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/
// CClientSocketCClientSocket::CClientSocket()
{
}CClientSocket::~CClientSocket()
{
}// Do not edit the following lines, which are needed by ClassWizard.
#if 0
BEGIN_MESSAGE_MAP(CClientSocket, CAsyncSocket)//{{AFX_MSG_MAP(CClientSocket)//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif // 0/
// CClientSocket member functionsvoid CClientSocket::OnClose(int nErrorCode)
{// TODO: Add your specialized code here and/or call the base class((CChatClientDlg *)(AfxGetApp()->m_pMainWnd))->m_ListWords.AddString("服务器端已经断开");((CChatClientDlg *)(AfxGetApp()->m_pMainWnd))->m_ListWords.SetTopIndex(((CChatClientDlg *)(AfxGetApp()->m_pMainWnd))->m_ListWords.GetCount()-1);((CChatClientDlg *)(AfxGetApp()->m_pMainWnd))->ServerIP.EnableWindow();((CChatClientDlg *)(AfxGetApp()->m_pMainWnd))->m_ButtonConnect.EnableWindow();((CChatClientDlg *)(AfxGetApp()->m_pMainWnd))->m_ButtonSend.EnableWindow(FALSE);Close();CAsyncSocket::OnClose(nErrorCode);
}void CClientSocket::OnConnect(int nErrorCode)
{// TODO: Add your specialized code here and/or call the base classif(nErrorCode)
{AfxMessageBox("连接出现错误,请您重新连接!");return;
}AsyncSelect(FD_READ|FD_WRITE|FD_CLOSE);((CChatClientDlg *)(AfxGetApp()->m_pMainWnd))->ServerIP.EnableWindow(FALSE);((CChatClientDlg *)(AfxGetApp()->m_pMainWnd))->m_ButtonConnect.EnableWindow(FALSE);((CChatClientDlg *)(AfxGetApp()->m_pMainWnd))->m_ButtonSend.EnableWindow();((CChatClientDlg *)(AfxGetApp()->m_pMainWnd))->m_ListWords.AddString("连接上服务器端");((CChatClientDlg *)(AfxGetApp()->m_pMainWnd))->m_ListWords.SetTopIndex(((CChatClientDlg *)(AfxGetApp()->m_pMainWnd))->m_ListWords.GetCount()-1);CAsyncSocket::OnConnect(nErrorCode);
}void CClientSocket::OnReceive(int nErrorCode)
{// TODO: Add your specialized code here and/or call the base classchar szTemp[250];int n=Receive(szTemp,250);szTemp[n]=0;CString sTemp;sTemp.Format("收到:%s",szTemp);((CChatClientDlg *)(AfxGetApp()->m_pMainWnd))->m_ListWords.AddString(sTemp);((CChatClientDlg *)(AfxGetApp()->m_pMainWnd))->m_ListWords.SetTopIndex(((CChatClientDlg *)(AfxGetApp()->m_pMainWnd))->m_ListWords.GetCount()-1);CAsyncSocket::OnReceive(nErrorCode);
}
// ChatClientDlg.h : header file
//#if !defined(AFX_CHATCLIENTDLG_H__69330A11_2EA5_4EAA_81ED_36AFA20E1E03__INCLUDED_)
#define AFX_CHATCLIENTDLG_H__69330A11_2EA5_4EAA_81ED_36AFA20E1E03__INCLUDED_#include "ClientSocket.h"
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000/
// CChatClientDlg dialogclass CChatClientDlg : public CDialog
{
// Construction
public:CClientSocket m_ClientSocket;CChatClientDlg(CWnd* pParent = NULL); // standard constructor// Dialog Data//{{AFX_DATA(CChatClientDlg)enum { IDD = IDD_CHATCLIENT_DIALOG };CListBox m_ListWords;CIPAddressCtrl ServerIP;CButton m_ButtonSend;CButton m_ButtonConnect;CString m_sWords;//}}AFX_DATA// ClassWizard generated virtual function overrides//{{AFX_VIRTUAL(CChatClientDlg)protected:virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support//}}AFX_VIRTUAL// Implementation
protected:HICON m_hIcon;// Generated message map functions//{{AFX_MSG(CChatClientDlg)virtual BOOL OnInitDialog();afx_msg void OnSysCommand(UINT nID, LPARAM lParam);afx_msg void OnPaint();afx_msg HCURSOR OnQueryDragIcon();afx_msg void OnButtonConnect();afx_msg void OnButtonSend();afx_msg void OnButtonClear();afx_msg void OnButtonAbout();afx_msg void OnButtonExit();//}}AFX_MSGDECLARE_MESSAGE_MAP()
};//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_CHATCLIENTDLG_H__69330A11_2EA5_4EAA_81ED_36AFA20E1E03__INCLUDED_)
#if !defined(AFX_CLIENTSOCKET_H__D5AD111C_C151_4927_B75A_F84B49698C4B__INCLUDED_)
#define AFX_CLIENTSOCKET_H__D5AD111C_C151_4927_B75A_F84B49698C4B__INCLUDED_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// ClientSocket.h : header file
///
// CClientSocket command targetclass CClientSocket : public CAsyncSocket
{
// Attributes
public:// Operations
public:CClientSocket();virtual ~CClientSocket();// Overrides
public:// ClassWizard generated virtual function overrides//{{AFX_VIRTUAL(CClientSocket)public:virtual void OnClose(int nErrorCode);virtual void OnConnect(int nErrorCode);virtual void OnReceive(int nErrorCode);//}}AFX_VIRTUAL// Generated message map functions//{{AFX_MSG(CClientSocket)// NOTE - the ClassWizard will add and remove member functions here.//}}AFX_MSG// Implementation
protected:
};///{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_CLIENTSOCKET_H__D5AD111C_C151_4927_B75A_F84B49698C4B__INCLUDED_)
2、服务端应用程序开发
建立一个基于对话框的MFC应用程序,创建的时候记得勾选【Windows Sockets】,其
他的默认就行。
(1)、对话框如图所示:
(2)代码如下:
// ChatServerDlg.cpp : implementation file
//#include "stdafx.h"
#include "ChatServer.h"
#include "ChatServerDlg.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/
// CAboutDlg dialog used for App Aboutclass 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_MSGDECLARE_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()/
// CChatServerDlg dialogCChatServerDlg::CChatServerDlg(CWnd* pParent /*=NULL*/): CDialog(CChatServerDlg::IDD, pParent)
{//{{AFX_DATA_INIT(CChatServerDlg)m_sWords = _T("");//}}AFX_DATA_INIT// Note that LoadIcon does not require a subsequent DestroyIcon in Win32m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}void CChatServerDlg::DoDataExchange(CDataExchange* pDX)
{CDialog::DoDataExchange(pDX);//{{AFX_DATA_MAP(CChatServerDlg)DDX_Control(pDX, IDC_LIST_WORDS, m_ListWords);DDX_Control(pDX, IDC_BUTTON_SEND, m_ButtonSend);DDX_Text(pDX, IDC_EDIT_WORDS, m_sWords);//}}AFX_DATA_MAP
}BEGIN_MESSAGE_MAP(CChatServerDlg, CDialog)//{{AFX_MSG_MAP(CChatServerDlg)ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_BN_CLICKED(IDC_BUTTON_SEND, OnButtonSend)ON_BN_CLICKED(IDC_BUTTON_CLEAR, OnButtonClear)ON_BN_CLICKED(IDC_BUTTON_ABOUT, OnButtonAbout)ON_BN_CLICKED(IDC_BUTTON_EXIT, OnButtonExit)//}}AFX_MSG_MAP
END_MESSAGE_MAP()/
// CChatServerDlg message handlersBOOL CChatServerDlg::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 dialogSetIcon(m_hIcon, TRUE); // Set big iconSetIcon(m_hIcon, FALSE); // Set small icon// TODO: Add extra initialization herem_ListenSocket.Create(14875);//创建监听Socket,端口号为14875m_ListenSocket.Listen(1);//开始监听,只接收一个客户端return TRUE; // return TRUE unless you set the focus to a control
}void CChatServerDlg::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 CChatServerDlg::OnPaint()
{if (IsIconic()){CPaintDC dc(this); // device context for paintingSendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);// Center icon in client rectangleint 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 icondc.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 CChatServerDlg::OnQueryDragIcon()
{return (HCURSOR) m_hIcon;
}void CChatServerDlg::OnButtonSend()
{// TODO: Add your control notification handler code hereUpdateData();m_ServerSocket.Send(m_sWords,m_sWords.GetLength()); //获取文本长度m_ListWords.AddString("发送:"+m_sWords); //显示发送文件长度记录m_ListWords.SetTopIndex(m_ListWords.GetCount()-1); }void CChatServerDlg::OnButtonClear()
{// TODO: Add your control notification handler code herem_ListWords.ResetContent();}void CChatServerDlg::OnButtonAbout()
{// TODO: Add your control notification handler code hereCAboutDlg dlgAbout;dlgAbout.DoModal();}void CChatServerDlg::OnButtonExit()
{// TODO: Add your control notification handler code hereCDialog::OnCancel();}
// ServerSocket.cpp : implementation file
//#include "stdafx.h"
#include "ChatServer.h"
#include "ServerSocket.h"
#include "ChatServerDlg.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/
// CServerSocketCServerSocket::CServerSocket()
{
}CServerSocket::~CServerSocket()
{
}// Do not edit the following lines, which are needed by ClassWizard.
#if 0
BEGIN_MESSAGE_MAP(CServerSocket, CAsyncSocket)//{{AFX_MSG_MAP(CServerSocket)//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif // 0/
// CServerSocket member functionsvoid CServerSocket::OnReceive(int nErrorCode)
{// TODO: Add your specialized code here and/or call the base classchar szTemp[250];int n=Receive(szTemp,250);szTemp[n]=0;CString sTemp;sTemp.Format("收到:%s",szTemp);((CChatServerDlg *)(AfxGetApp()->m_pMainWnd))->m_ListWords.AddString(sTemp);((CChatServerDlg *)(AfxGetApp()->m_pMainWnd))->m_ListWords.SetTopIndex(((CChatServerDlg *)(AfxGetApp()->m_pMainWnd))->m_ListWords.GetCount()-1);CAsyncSocket::OnReceive(nErrorCode);
}void CServerSocket::OnClose(int nErrorCode)
{// TODO: Add your specialized code here and/or call the base class((CChatServerDlg *)(AfxGetApp()->m_pMainWnd))->m_ListWords.AddString("客户端已经断开");((CChatServerDlg *)(AfxGetApp()->m_pMainWnd))->m_ListWords.SetTopIndex(((CChatServerDlg *)(AfxGetApp()->m_pMainWnd))->m_ListWords.GetCount()-1);((CChatServerDlg *)(AfxGetApp()->m_pMainWnd))->m_ButtonSend.EnableWindow(FALSE);Close();CAsyncSocket::OnClose(nErrorCode);
}
// ListenSocket.cpp : implementation file
//#include "stdafx.h"
#include "ChatServer.h"
#include "ListenSocket.h"
#include "ChatServerDlg.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/
// CListenSocketCListenSocket::CListenSocket()
{
}CListenSocket::~CListenSocket()
{
}// Do not edit the following lines, which are needed by ClassWizard.
#if 0
BEGIN_MESSAGE_MAP(CListenSocket, CAsyncSocket)//{{AFX_MSG_MAP(CListenSocket)//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif // 0/
// CListenSocket member functionsvoid CListenSocket::OnAccept(int nErrorCode)
{// TODO: Add your specialized code here and/or call the base class// 接受客户端连接请求Accept(((CChatServerDlg *)(AfxGetApp()->m_pMainWnd))->m_ServerSocket);// 启用"发送"按钮((CChatServerDlg *)(AfxGetApp()->m_pMainWnd))->m_ButtonSend.EnableWindow();((CChatServerDlg *)(AfxGetApp()->m_pMainWnd))->m_ServerSocket.AsyncSelect(FD_READ|FD_WRITE|FD_CLOSE);((CChatServerDlg *)(AfxGetApp()->m_pMainWnd))->m_ListWords.AddString("接受客户端连接请求");((CChatServerDlg *)(AfxGetApp()->m_pMainWnd))->m_ListWords.SetTopIndex(((CChatServerDlg *)(AfxGetApp()->m_pMainWnd))->m_ListWords.GetCount()-1);CAsyncSocket::OnAccept(nErrorCode);
}
// ChatServerDlg.h : header file
//#if !defined(AFX_CHATSERVERDLG_H__C592DFC5_F66D_405E_84CB_F9AD0C5B55E1__INCLUDED_)
#define AFX_CHATSERVERDLG_H__C592DFC5_F66D_405E_84CB_F9AD0C5B55E1__INCLUDED_#include "ListenSocket.h" // Added by ClassView
#include "ServerSocket.h" // Added by ClassView
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000/
// CChatServerDlg dialogclass CChatServerDlg : public CDialog
{
// Construction
public:CServerSocket m_ServerSocket;CListenSocket m_ListenSocket;CChatServerDlg(CWnd* pParent = NULL); // standard constructor// Dialog Data//{{AFX_DATA(CChatServerDlg)enum { IDD = IDD_CHATSERVER_DIALOG };CListBox m_ListWords;CButton m_ButtonSend;CString m_sWords;//}}AFX_DATA// ClassWizard generated virtual function overrides//{{AFX_VIRTUAL(CChatServerDlg)protected:virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support//}}AFX_VIRTUAL// Implementation
protected:HICON m_hIcon;// Generated message map functions//{{AFX_MSG(CChatServerDlg)virtual BOOL OnInitDialog();afx_msg void OnSysCommand(UINT nID, LPARAM lParam);afx_msg void OnPaint();afx_msg HCURSOR OnQueryDragIcon();afx_msg void OnButtonSend();afx_msg void OnButtonClear();afx_msg void OnButtonAbout();afx_msg void OnButtonExit();//}}AFX_MSGDECLARE_MESSAGE_MAP()
};//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_CHATSERVERDLG_H__C592DFC5_F66D_405E_84CB_F9AD0C5B55E1__INCLUDED_)
#if !defined(AFX_SERVERSOCKET_H__2CC46D77_F46D_4068_8FB4_A831DF5A0201__INCLUDED_)
#define AFX_SERVERSOCKET_H__2CC46D77_F46D_4068_8FB4_A831DF5A0201__INCLUDED_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// ServerSocket.h : header file
///
// CServerSocket command targetclass CServerSocket : public CAsyncSocket
{
// Attributes
public:// Operations
public:CServerSocket();virtual ~CServerSocket();// Overrides
public:// ClassWizard generated virtual function overrides//{{AFX_VIRTUAL(CServerSocket)public:virtual void OnReceive(int nErrorCode);virtual void OnClose(int nErrorCode);//}}AFX_VIRTUAL// Generated message map functions//{{AFX_MSG(CServerSocket)// NOTE - the ClassWizard will add and remove member functions here.//}}AFX_MSG// Implementation
protected:
};///{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_SERVERSOCKET_H__2CC46D77_F46D_4068_8FB4_A831DF5A0201__INCLUDED_)
#if !defined(AFX_LISTENSOCKET_H__15870D2E_7268_4EC7_9B2F_DCA2BFBE89BC__INCLUDED_)
#define AFX_LISTENSOCKET_H__15870D2E_7268_4EC7_9B2F_DCA2BFBE89BC__INCLUDED_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// ListenSocket.h : header file
///
// CListenSocket command targetclass CListenSocket : public CAsyncSocket
{
// Attributes
public:// Operations
public:CListenSocket();virtual ~CListenSocket();// Overrides
public:// ClassWizard generated virtual function overrides//{{AFX_VIRTUAL(CListenSocket)public:virtual void OnAccept(int nErrorCode);//}}AFX_VIRTUAL// Generated message map functions//{{AFX_MSG(CListenSocket)// NOTE - the ClassWizard will add and remove member functions here.//}}AFX_MSG// Implementation
protected:
};///{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_LISTENSOCKET_H__15870D2E_7268_4EC7_9B2F_DCA2BFBE89BC__INCLUDED_)
3、在客户端填写本机的地址127.0.0.1,单击【连接】按钮进行测试运行,效果如下: