Socket编程应用——开发聊天软件

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,单击【连接】按钮进行测试运行,效果如下:






本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/492417.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

MFC中给单文档程序添加背景图片

1、在OnDraw函数中修改如下&#xff1a; void CBitmapView::OnDraw(CDC* pDC) {CBitmapDoc* pDoc GetDocument();ASSERT_VALID(pDoc);// TODO: add draw code for native data hereCBitmap bitmap; //位图类对象bitmap.LoadBitmap(IDB_BITMAP1); //从资源中装载入位图CDC dc…

详解|清华大学100页PPT:工业机器人技术详解

来源&#xff1a;清华大学未来智能实验室是人工智能学家与科学院相关机构联合成立的人工智能&#xff0c;互联网和脑科学交叉研究机构。未来智能实验室的主要工作包括&#xff1a;建立AI智能系统智商评测体系&#xff0c;开展世界人工智能智商评测&#xff1b;开展互联网&#…

2019年,中国要推进这70个工程项目

来源&#xff1a;人民日报客户端摘要&#xff1a;近日&#xff0c;《关于2018年国民经济和社会发展计划执行情况与2019年国民经济和社会发展计划草案的报告》正式发布。报告详尽地对2019年我国经济社会的发展做出了安排。围绕基础设施建设、创新发展、社会民生、生态治理、文化…

判断101-200之间有多少个素数,并输出所有素数。

1、代码如下&#xff1a; // test.cpp : Defines the entry point for the console application. // /* 判断101-200之间有多少个素数&#xff0c;并输出所有素数。*/ #include "stdafx.h" #include <iostream> #include <cmath> using namespace std;in…

输入一个十进制数,转化为二进制

1、代码如下&#xff1a; // test.cpp : Defines the entry point for the console application. // /* 输入一个十进制数&#xff0c;转化为二进制。*/ #include "stdafx.h" #include <iostream> using namespace std;int main(int argc, char* argv[]) {cout…

超越“机器人三定律” 人工智能期待新伦理

来源&#xff1a;新华网人工智能的伦理原则近来备受关注。联合国教科文组织总干事阿祖莱在3月初举行的“推动人性化人工智能全球会议”上就表示&#xff0c;目前还没有适用于所有人工智能开发和应用的国际伦理规范框架。对于科幻作家阿西莫夫上世纪设计、防止机器人失控的著名“…

求5阶矩阵其对角线上所有元素之和

1、代码如下&#xff1a; // test.cpp : Defines the entry point for the console application. // /* 输入一个5*5的矩阵&#xff0c;然后输出其对角线上所有元素之和。 当求N阶矩阵其对角线上所有元素之和时&#xff0c;只要把以下程序中所有的5改成N,4改成N-1即可。*/ #inc…

DARPA“终身学习机器”项目取得重大进展

来源&#xff1a;DARPA网站2019年3月&#xff0c;美国防高级研究计划局&#xff08;DARPA&#xff09;“终身学习机器”&#xff08;L2M&#xff09;项目研究人员在《自然机器智能》杂志发表了其有关人工智能算法的研究结果&#xff0c;介绍了一种由类似动物肌腱驱动的人工智能…

《自然》,工程学突破!仿生物细胞群体机器人问世

来源&#xff1a;科技日报摘要&#xff1a;北京3月20日&#xff0c;英国《自然》杂志20日发表了一项工程学最新突破&#xff1a;美国科学家团队研发了一种能模拟生物细胞集体迁移的机器人&#xff0c;可实现移动、搬运物体及向光刺激移动。北京3月20日&#xff0c;英国《自然》…

白宫启动AI.GOV计划,呼吁各界携手共同推进AI发展

来源&#xff1a;网络大数据摘要&#xff1a;近日&#xff0c;白宫启动了 ai.gov 计划&#xff0c;列出了特朗普政府与美国联邦机构采取的一系列人工智能举措&#xff0c;如美国国立卫生研究院(NIH)利用 AI 展开的生物医学研究项目以及美国交通部近期发布的关于自动驾驶汽车的报…

Qt连接MySQL数据库

1、将MySQL安装目录下的libmysql.dll拷贝到Qt安装目录下的bin目录中。 2、准备数据库和数据表如下&#xff1a; 3、编写如下代码&#xff1a; #------------------------------------------------- # # Project created by QtCreator 2016-07-15T17:56:50 # #----------------…

边缘计算不再“边缘”

来源&#xff1a;中国科学报摘要&#xff1a;5G商用时代来临&#xff0c;数据量将更加巨大、复杂&#xff0c;对计算提出更高要求&#xff0c;同时也为发展人工智能、边缘计算带来了新机遇。5G商用时代来临&#xff0c;数据量将更加巨大、复杂&#xff0c;对计算提出更高要求&a…

Qt中修改应用程序和标题栏的图标

一、修改应用程序图标 1.新建一个my.txt文件&#xff0c;打开后在其中加一句 “IDI_ICON1 ICON DISCARDABLE "应用程 序图标.ico"”。&#xff08;“应用程序图标.ico”是要添加的图片名&#xff0c;图片格式一定要是.ico), 然后保存并退出&#xff0c;将文件格式改为…

人类“第六感”首次被证实,研究发现人脑具有磁场感应能力

新证据表明&#xff0c;人类磁感可以让大脑感应到地球磁场来源&#xff1a;神经科技摘要&#xff1a;科学界已经知道鸟类可以利用地磁场进行导航&#xff0c;除此之外&#xff0c;科学家在自然界许多物种中都发现了磁感应能力&#xff0c;生物的磁感受能力也一直在业内被称作生…

用S-函数编写Simulink中的正弦模块

1、用S-函数实现一个正弦波信号源。要求其幅度、频率和初始相位参数可由外部设置&#xff0c;并将这个信号源进行封装。 S-函数程序代码如下&#xff1a; function [sys,x0,str,ts] ch2example17Sfun(t,x,u,flag,Amp,Freq,Phase) % 正弦波信号源 switch flag, case 0 …

2019计算与系统神经科学大会Cosyne 前沿研究汇总

来源&#xff1a;混沌巡洋舰摘要&#xff1a;计算神经科学是一门超级跨学科的新兴学科&#xff0c;几乎综合信息科学&#xff0c;物理学&#xff0c; 数学&#xff0c;生物学&#xff0c;认知心理学等众多领域的最新成果。关注的是神经系统的可塑性与记忆&#xff0c;抑制神经元…

MATLAB中的S-Function的用法(C语言)

1. S-Function简介 S-Function是system-function的缩写。说得简单&#xff0c;S-Function就是用MATLAB所提供的模型不能完全满足用户&#xff0c;而提供给用户自己编写程序来满足自己要求模型的接口。 2. MEX函数与M文件的区别 第一&#xff0c; MEX 函数能实现的回调函数比…

一文读懂民航客机飞控系统

来源&#xff1a;传感器技术摘要&#xff1a;埃塞俄比亚航空公司波音737 MAX 8型客机当地时间10日坠毁&#xff0c;这是时隔不到5个月&#xff0c;波音同一型号飞机发生的第二起空难。鉴于两起事故具有明显的相似性&#xff0c;越来越多的将目标指向了该型号的设计缺陷——飞控…

VC创建DLL动态链接库及其调用

1.1 创建dll项目 1.2 为dll项目编写源文件 头文件dllDemo.hextern "C" _declspec(dllexport) int Sum(int a,int b);//加法函数。extern "C" _declspec(dllexport) int Max(int a, int b);//取较大值函数extern "C" _declspec(dllexport) int Mi…

10个免费开源的JS音乐播放器插件

点这里 音乐播放器在网页设计中有时候会用到&#xff0c;比如一些时尚类、音乐或影视类等项目&#xff0c;但这些 网页播放器 插件比较少见&#xff0c;所以这里为大家整理一个集合&#xff0c;也许会有用到的时候。 下面整理的播放器有些是支持自适应的&#xff0c;如果需要用…