![]() 图一、网络聊天程序界面效果图 |
CAsyncSocket sock; //使用默认参数产生一个字节流套接字 Sock.Create(); |
CAsyncSocket*pSocket=newCAsyncSocket; intnPort=27; pSocket->Create(nPort,SOCK-DGRAM); |
BOOL Create( UINT nSocketPort = 0, int nSocketType = SOCK_STREAM, LPCTSTR lpszSocketAddress = NULL ); |
////////////////////////////////////////////////// NewSocket.h : header file #if !defined(AFX_NEWSOCKET_H__8CE2ED73_1D56_11D3_9928_00A0C98F3E85__INCLUDED_) #define AFX_NEWSOCKET_H__8CE2ED73_1D56_11D3_9928_00A0C98F3E85__INCLUDED_ #if _MSC_VER >= 1000 #pragma once #endif // _MSC_VER >= 1000 class CTestDlg; #include <afxsock.h> class CNewSocket : public CSocket { // Attributes public: // Operations public: CNewSocket(); virtual ~CNewSocket(); // Overrides public: int m_Status; void GetDlg(CTestDlg *dlg); CTestDlg *m_dlg; // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CNewSocket) public: virtual void OnAccept(int nErrorCode); virtual void OnReceive(int nErrorCode); virtual void OnClose(int nErrorCode); //}}AFX_VIRTUAL // Generated message map functions //{{AFX_MSG(CNewSocket) // NOTE - the ClassWizard will add and remove member functions here. //}}AFX_MSG // Implementation protected: }; #endif //////////////////////////////////////////////////////// NewSocket.cpp : implementation file #include "stdafx.h" #include "Test.h" #include "NewSocket.h" #include "TestDlg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif CNewSocket::CNewSocket() {} CNewSocket::~CNewSocket() {} #if 0 BEGIN_MESSAGE_MAP(CNewSocket, CSocket) //{{AFX_MSG_MAP(CNewSocket) //}}AFX_MSG_MAP END_MESSAGE_MAP() #endif // 0 void CNewSocket::OnAccept(int nErrorCode) { if (m_dlg->m_ClientSocket==NULL) m_dlg->OnAccept(); CSocket::OnAccept(nErrorCode); } void CNewSocket::OnReceive(int nErrorCode) { m_dlg->OnReceive(); CSocket::OnReceive(nErrorCode); } void CNewSocket::GetDlg(CTestDlg *dlg) { m_dlg=dlg; } void CNewSocket::OnClose(int nErrorCode) { m_dlg->OnClose(); CSocket::OnClose(nErrorCode); } ///////////////////////////////////////////////////////////////// TestDlg.h : header file #if !defined(AFX_TESTDLG_H__EDDDE196_1BF1_11D3_BE77_0000B454AEE4__INCLUDED_) #define AFX_TESTDLG_H__EDDDE196_1BF1_11D3_BE77_0000B454AEE4__INCLUDED_ #if _MSC_VER >= 1000 #pragma once #endif // _MSC_VER >= 1000 #include "NewSocket.h" class CTestDlg : public CDialog { // Construction public: void SocketReset(); void OnClose(); void OnReceive(); void OnAccept(); CSocketFile *m_file; CArchive *m_arOut; CArchive *m_arIn; CNewSocket* m_ServerSocket; CNewSocket* m_ClientSocket; CTestDlg(CWnd* pParent = NULL); // standard constructor // Dialog Data //{{AFX_DATA(CTestDlg) enum { IDD = IDD_TEST_DIALOG }; CString m_Info; CString m_Output; CString m_Input; CString m_Connect; CString m_IPAddress; UINT m_Port; int m_Status; //}}AFX_DATA // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CTestDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL // Implementation protected: HICON m_hIcon; // Generated message map functions //{{AFX_MSG(CTestDlg) virtual BOOL OnInitDialog(); afx_msg void OnSysCommand(UINT nID, LPARAM lParam); afx_msg void OnPaint(); afx_msg HCURSOR OnQueryDragIcon(); afx_msg void OnConnect(); afx_msg void OnDisconnect(); afx_msg void OnSend(); afx_msg void OnServerradio(); afx_msg void OnClientradio(); afx_msg void OnSendclear(); afx_msg void OnReceiveclear(); //}}AFX_MSG DECLARE_MESSAGE_MAP() }; #endif //////////////////////////////////////////////////////////////// TestDlg.cpp : implementation file #include "stdafx.h" #include "Test.h" #include "TestDlg.h" #include <afxsock.h> #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif 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() CTestDlg::CTestDlg(CWnd* pParent /*=NULL*/) : CDialog(CTestDlg::IDD, pParent) { //{{AFX_DATA_INIT(CTestDlg) m_Info = _T(""); m_Output = _T(""); m_Input = _T(""); m_Connect = _T(""); m_IPAddress = _T(""); m_Port = 0; m_Status = -1; //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CTestDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CTestDlg) DDX_Text(pDX, IDC_OUTPUTEDIT, m_Output); DDX_Text(pDX, IDC_INPUTEDIT, m_Input); DDX_Text(pDX, IDC_CONNECTEDIT, m_Connect); DDX_Text(pDX, IDC_IPADDRESS, m_IPAddress); DDV_MaxChars(pDX, m_IPAddress, 15); DDX_Text(pDX, IDC_PORT, m_Port); DDX_Radio(pDX, IDC_SERVERRADIO, m_Status); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CTestDlg, CDialog) //{{AFX_MSG_MAP(CTestDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_CONNECTBUTTON, OnConnect) ON_BN_CLICKED(IDC_DISCONNECTBUTTON, OnDisconnect) ON_BN_CLICKED(IDC_SENDBUTTON, OnSend) ON_BN_CLICKED(IDC_SERVERRADIO, OnServerradio) ON_BN_CLICKED(IDC_CLIENTRADIO, OnClientradio) ON_BN_CLICKED(IDC_SENDCLEARBUTTON, OnSendclear) ON_BN_CLICKED(IDC_RECEIVECLEARBUTTON, OnReceiveclear) //}}AFX_MSG_MAP END_MESSAGE_MAP() BOOL CTestDlg::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 m_Status=-1; m_ServerSocket=NULL; m_ClientSocket=NULL; m_arIn=NULL; m_arOut=NULL; m_file=NULL; m_Connect=""; m_IPAddress="202.207.243.29"; m_Port=5000; GetDlgItem(IDC_IPADDRESS)->EnableWindow(FALSE); GetDlgItem(IDC_PORT)->EnableWindow(FALSE); UpdateData(FALSE); return TRUE; // return TRUE unless you set the focus to a control } void CTestDlg::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 CTestDlg::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 CTestDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } void CTestDlg::OnConnect() { CString msg; UpdateData(TRUE); if (m_Status==0 ) //server { if ( m_ServerSocket!=NULL) { m_Connect="Please disconnect!"; UpdateData(FALSE); } else { m_Connect="Waiting for Client..."; UpdateData(FALSE); if(!AfxSocketInit()) { MessageBox("WindowsSocket initial failed!","Send",MB_ICONSTOP); return; } m_ServerSocket=new CNewSocket; m_ServerSocket->m_Status=m_Status; m_ServerSocket->GetDlg(this); if(!m_ServerSocket->Create(m_Port)) MessageBox("SendSocket create failed!", "Send",MB_ICONSTOP); else { m_ServerSocket->Listen(); } } } else { if (m_Status==1) { if (m_ClientSocket!=NULL) { m_Connect="Please disconnect!"; UpdateData(FALSE); } else { m_Connect="Connect to the Server..."; UpdateData(FALSE); if(!AfxSocketInit()) { MessageBox("WindowsSocket initial failed!","Receive",MB_ICONSTOP); return; } m_ClientSocket=new CNewSocket; m_ClientSocket->GetDlg(this); m_ClientSocket->m_Status=m_Status; if(!m_ClientSocket->Create()) { MessageBox("ReceiveSocket create failed!","Receive",MB_ICONSTOP); return; } else { if (!m_ClientSocket->Connect(m_IPAddress,m_Port)) { CString str=m_Connect; SocketReset(); m_Connect=str; m_Connect+="Error!"; UpdateData(FALSE); } else { m_Connect+="OK!"; m_file=new CSocketFile(m_ClientSocket); m_arIn=new CArchive(m_file, CArchive::load); m_arOut=new CArchive(m_file, CArchive::store); } UpdateData(FALSE); } } } } if (m_Status==-1) { msg="Please choose the status!"; AfxMessageBox(msg); } } void CTestDlg::OnSend() { if (m_arOut) { if (m_Status==0) { UpdateData(TRUE); *m_arOut<<m_Output; m_arOut->Flush(); } else { UpdateData(TRUE); *m_arOut<<m_Output; m_arOut->Flush(); } } else AfxMessageBox("Not connected!"); } void CTestDlg::OnAccept() { m_Connect+="OK!"; UpdateData(FALSE); m_ClientSocket=new CNewSocket; m_ClientSocket->GetDlg(this); m_ServerSocket->Accept(*m_ClientSocket); m_ClientSocket->m_Status=m_ServerSocket->m_Status; m_file=new CSocketFile(m_ClientSocket); m_arIn=new CArchive(m_file, CArchive::load); m_arOut=new CArchive(m_file, CArchive::store); } void CTestDlg::OnReceive() { *m_arIn>>m_Input; UpdateData(FALSE); } void CTestDlg::OnDisconnect() { if (m_arOut!=NULL) { SocketReset(); m_Connect="Disconnected!"; UpdateData(FALSE); } } void CTestDlg::OnClose() { if (m_ClientSocket->m_Status==0) m_Connect="Client "; else m_Connect="Server "; m_Connect+="has disconnected!"; UpdateData(FALSE); } void CTestDlg::SocketReset() { if (m_arIn!=NULL) { delete m_arIn; m_arIn=NULL; } if (m_arOut!=NULL) { delete m_arOut; m_arOut=NULL; } if (m_file!=NULL) { delete m_file; m_file=NULL; } if (m_ClientSocket!=NULL) { delete m_ClientSocket; m_ClientSocket=NULL; } if (m_ServerSocket!=NULL) { delete m_ServerSocket; m_ServerSocket=NULL; } m_Connect=""; UpdateData(FALSE); } void CTestDlg::OnServerradio() { UpdateData(TRUE); GetDlgItem(IDC_IPADDRESS)->EnableWindow(FALSE); GetDlgItem(IDC_PORT)->EnableWindow(TRUE); UpdateData(FALSE); } void CTestDlg::OnClientradio() { UpdateData(TRUE); GetDlgItem(IDC_IPADDRESS)->EnableWindow(TRUE); GetDlgItem(IDC_PORT)->EnableWindow(TRUE); UpdateData(FALSE); } void CTestDlg::OnSendclear() { m_Output=""; UpdateData(FALSE); } void CTestDlg::OnReceiveclear() { m_Input=""; UpdateData(FALSE); } |