WSAAsyncSelect model instance in C++

  • 2020-04-02 02:49:54
  • OfStack

This article illustrates the use of the WSAAsyncSelect model in C++. Share with you for your reference. The specific implementation method is as follows:

Tcpserver.cpp source file is as follows:

#include "TCPServer.h"  
#include "resource.h" 
 
#define WM_SOCKET WM_USER+1 
 
CMyApp theApp; 
 
BOOL CMyApp::InitInstance() 

    //Initializing the socket & NBSP; < br / >     WSADATA wsaData; 
    WORD wVersionRequested = MAKEWORD(2,0); 
    ::WSAStartup(wVersionRequested, &wsaData); 
    //Displays the dialog box & cake; < br / >     CMainDialog dlg; 
    m_pMainWnd = &dlg; 
    dlg.DoModal(); 
    //Release socket & NBSP; < br / >     ::WSACleanup(); 
    return FALSE; 

 
//CMainDialog 
CMainDialog::CMainDialog(CWnd* pParentWnd):CDialog(IDD_MAINDIALOG,pParentWnd) 

 

BEGIN_MESSAGE_MAP(CMainDialog, CDialog) 
ON_BN_CLICKED(IDC_START, OnStart) 
ON_BN_CLICKED(IDC_CLEAR, OnClear) 
ON_MESSAGE(WM_SOCKET, OnSocket) 
END_MESSAGE_MAP() 
 
void CMainDialog::OnCancel() 

    this->CloseAllSocket(); 
    CDialog::OnCancel(); 

 
BOOL CMainDialog::OnInitDialog() 

    CDialog::OnInitDialog(); 
 
    //Set icon & cake; < br / >     SetIcon(theApp.LoadIconA(IDI_MAIN), FALSE); 
 
    //Creates the status bar and sets its property & NBSP; < br / >     m_bar.Create(WS_CHILD|WS_VISIBLE|SBS_SIZEGRIP, CRect(0,0,0,0), this, 101); 
    m_bar.SetBkColor(RGB(0xa6, 0xca, 0xfa)); 
    int arWidth[]={200,-1}; 
    m_bar.SetParts(2, arWidth); 
    m_bar.SetText("windows The program design ", 1, 0); 
    m_bar.SetText(" free ", 0, 0); 
    //Associated list control & NBSP; < br / >     m_listInfo.SubclassDlgItem(IDC_LIST, this); 
 
    //Initializes the list of sockets and connections & NBSP; < br / >     m_socket = INVALID_SOCKET; 
    m_nClient = 0; 
 
    //Gets the native IP and displays & NBSP; in the status bar; < br / >     char szHostName[MAX_PATH] = {0}; 
    ::gethostname(szHostName, MAX_PATH); 
    hostent *pHost = gethostbyname(szHostName); 
    if (pHost != NULL) 
    { 
        CString strIP; 
        in_addr* addr = (in_addr*)*pHost->h_addr_list; 
        strIP.Format(" This machine IP:%s",inet_ntoa(addr[0])); 
        m_bar.SetText(strIP, 0, 0); 
    } 
    return TRUE; 

 
BOOL CMainDialog::CreateAndListen(int nPort) 

    if (m_socket == INVALID_SOCKET) 
    { 
        ::closesocket(m_socket); 
    } 
    //Create a socket & NBSP; < br / >     m_socket = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 
    if (m_socket == INVALID_SOCKET) 
    { 
        return FALSE; 
    } 
    //Bind port & NBSP; < br / >     sockaddr_in sin; 
    sin.sin_family = AF_INET; 
    sin.sin_port = htons(nPort); 
    //sin.sin_addr.S_un.S_addr = INADDR_ANY; 
    sin.sin_addr.s_addr = INADDR_ANY; 
    int nErr = GetLastError(); 
    if (::bind(m_socket, (sockaddr*)&sin, sizeof(sin)) == SOCKET_ERROR) 
    { 
        nErr = GetLastError(); 
        return FALSE; 
    } 
    ::WSAAsyncSelect(m_socket, m_hWnd, WM_SOCKET, FD_ACCEPT|FD_CLOSE|FD_READ); 
 
    //Enter the listening mode & NBSP; < br / >     ::listen(m_socket, 5); 
 
    return TRUE; 

 
BOOL CMainDialog::AddClient(SOCKET s) 

     
    if (m_nClient < MAX_SOCKET) 
    { 
        m_arClient[m_nClient++] = s; 
        return TRUE; 
    } 
    return FALSE; 
     

 
void CMainDialog::RemoveClient(SOCKET s) 

    BOOL bFound = FALSE; 
    int i; 
    for (i=0;i<m_nClient;i++) 
    { 
        if (m_arClient[i] == s) 
        { 
            bFound = TRUE; 
            break; 
        } 
    } 
 
    //Find & have spent < br / >     if (bFound) 
    { 
        m_nClient--; 
        for (int j=i;j<m_nClient;j++) 
        { 
            m_arClient[j] = m_arClient[j+1]; 
        } 
    } 

void CMainDialog::CloseAllSocket() 

    if (m_socket != INVALID_SOCKET) 
    { 
        ::closesocket(m_socket); 
        m_socket = INVALID_SOCKET; 
    } 
    for (int i=0;i<m_nClient;i++) 
    { 
        ::closesocket(m_arClient[i]); 
    } 
    m_nClient = 0; 

 
void CMainDialog::OnStart() 

    if (m_socket == INVALID_SOCKET) //Turn on the service & cake; < br / >     { 
        CString strPort; 
        GetDlgItem(IDC_PORT)->GetWindowText(strPort); 
        int nPort = atoi(strPort); 
        if (nPort < 1 || nPort >65535) 
        { 
            MessageBox("port error"); 
            return; 
        } 
        //Create a socket & NBSP; < br / >         if (!this->CreateAndListen(nPort)) 
        { 
            MessageBox("create socket error"); 
            return; 
        } 
        //Setting control state & NBSP; < br / >         GetDlgItem(IDC_START)->SetWindowTextA(" Stop the service "); 
        m_bar.SetText(" Is listening ...", 0, 0); 
        GetDlgItem(IDC_PORT)->EnableWindow(FALSE); 
    } 
    else //Close the service & cake; < br / >     { 
        CloseAllSocket(); 
        GetDlgItem(IDC_START)->SetWindowTextA(" Open the service "); 
        m_bar.SetText(" free ", 0, 0); 
        GetDlgItem(IDC_PORT)->EnableWindow(TRUE); 
    } 
    return ; 

void CMainDialog::OnClear() 

    m_listInfo.ResetContent(); 
    return ; 

 
long CMainDialog::OnSocket(WPARAM wParam, LPARAM lParam) 

    //Getting a handle & NBSP; & have spent < br / >     SOCKET s = wParam; 
    //Check if anything is wrong & sponge; < br / >     if (WSAGETSELECTERROR(lParam)) 
    { 
        RemoveClient(s); 
        ::closesocket(s); 
        return 0; 
    } 
    //Dealing with the events that occur & NBSP; < br / >     switch (WSAGETSELECTEVENT(lParam)) 
    { 
    case FD_ACCEPT: //Listen for a connection to enter & NBSP in a socket; < br / >         { 
            MessageBox("server:accept"); 
            if (m_nClient < MAX_SOCKET) 
            { 
                SOCKET client = ::accept(s, NULL, NULL); 
                this->AddClient(client); 
            } 
            else 
            { 
                MessageBox("too many connection"); 
            } 
        } 
        break; 
    case FD_CLOSE: 
        { 
            MessageBox("server:close"); 
            RemoveClient(s); 
            closesocket(s); 
        } 
        break; 
    case FD_READ: //Receiving a packet from the other party < br / >         { 
            MessageBox("server:read"); 
            //Get your partner's address & NBSP; < br / >             sockaddr_in sockAddr; 
            memset(&sockAddr, 0, sizeof(sockAddr)); 
            int nSockAddrLength = sizeof(sockAddr); 
            ::getpeername(s, (sockaddr*)&sockAddr, &nSockAddrLength); 
 
            int nPeerPort = ntohs(sockAddr.sin_port); 
            CString strIP = inet_ntoa(sockAddr.sin_addr);  // strIP 
 
            //Get the host name & NBSP; < br / >             DWORD dwIP = ::inet_addr(strIP); 
            hostent* pHost = ::gethostbyaddr((LPSTR)&dwIP, 4, AF_INET); 
            char szHostName[256]={0}; 
            strncpy(szHostName, pHost->h_name, 256); 
 
            //Getting network data & NBSP; < br / >             char szContent[1024]={0}; 
            ::recv(s, szContent, 1024, 0); 
 
            //Display & have spent < br / >             CString strItem = CString(szHostName) + "[" + strIP + "]:" + CString(szContent); 
            m_listInfo.InsertString(0, strItem); 
        } 
        break; 
    } 
    return 0; 
}

Tcpserver.h header file is as follows:

#include <afxwin.h>  
#include <afxext.h>  //CStatusBar 
#include <WinSock2.h> 
#include <afxcmn.h> 
 
#pragma comment(lib, "WS2_32.lib") 
#define  MAX_SOCKET 56 //Maximum number of clients & NBSP; < br / >  
class CMyApp:public  CWinApp 

public: 
    BOOL InitInstance(); 
}; 
 
//CMainDialog 
class CMainDialog:public CDialog 

public: 
    CMainDialog(CWnd* pParentWnd=NULL); 
 
protected: 
    virtual BOOL OnInitDialog(); 
    virtual void OnCancel(); 
    //Start or stop service & cake; < br / >     afx_msg void OnStart(); 
    afx_msg void OnClear(); 
    afx_msg long OnSocket(WPARAM wParam, LPARAM lParam); 
 
    BOOL CreateAndListen(int nPort); 
 
    //Add a customer & NBSP; to the customer connection list; < br / >     BOOL AddClient(SOCKET s); 
    //Removes a client & NBSP; from the client connection list; < br / >     void RemoveClient(SOCKET s); 
    //Close all connections & NBSP; < br / >     void CloseAllSocket(); 
 
protected: 
    SOCKET m_socket; 
    //Two child window controls & NBSP; < br / >     CListBox m_listInfo; 
    CStatusBarCtrl m_bar; 
 
    //Client connection list & NBSP; < br / >     SOCKET m_arClient[MAX_SOCKET]; //Socket list & NBSP; < br / >     int m_nClient; //The size of the above array & NBSP; < br / >  
    DECLARE_MESSAGE_MAP() 
};

Tcpclient.cpp source file is as follows:

#include "TCPClient.h"  
#include "resource.h" 
 
#define WM_SOCKET WM_USER+1 
 
CMyApp theApp; 
 
BOOL CMyApp::InitInstance() 

    //Initializing the socket & NBSP; < br / >     WSADATA wsaData; 
    WORD wVersionRequested = MAKEWORD(2,0); 
    ::WSAStartup(wVersionRequested, &wsaData); 
    //Displays the dialog box & cake; < br / >     CMainDialog dlg; 
    m_pMainWnd = &dlg; 
    dlg.DoModal(); 
    //Release socket & NBSP; < br / >     ::WSACleanup(); 
    return FALSE; 

 
//CMainDialog 
CMainDialog::CMainDialog(CWnd* pParentWnd):CDialog(IDD_MAINDIALOG,pParentWnd) 

 

BEGIN_MESSAGE_MAP(CMainDialog, CDialog) 
    ON_BN_CLICKED(IDC_CONNECT, OnConnect) 
    ON_BN_CLICKED(IDC_SEND, OnSend) 
    ON_MESSAGE(WM_SOCKET, OnSocket) 
END_MESSAGE_MAP() 
 
void CMainDialog::OnCancel() 

     
    CDialog::OnCancel(); 

 
BOOL CMainDialog::OnInitDialog() 

    CDialog::OnInitDialog(); 
 
    //Set icon & cake; < br / >     SetIcon(theApp.LoadIconA(IDI_MAIN), FALSE); 
 
    //Associated control & NBSP; < br / >     m_edit_text.SubclassDlgItem(IDC_EDIT_CONTENT, this); 
    //The status bar & have spent < br / >     m_bar.Create(WS_CHILD|WS_VISIBLE|SBS_SIZEGRIP, CRect(0, 0, 0,0), this, NULL); 
    int nWidth[]={100,-1}; 
    m_bar.SetParts(2, nWidth); 
    m_bar.SetText("windows The program design ", 1, 0); 
    m_bar.SetText(" free ", 0, 0); 
 
    GetDlgItem(IDC_ADDR)->SetWindowTextA("192.168.19.143"); 
    GetDlgItem(IDC_PORT)->SetWindowTextA("9999"); 
 
    // 
    m_socket = INVALID_SOCKET; 
     
    return TRUE; 

void CMainDialog::AddStringToList(CString strText) 

    CString strContent; 
    GetDlgItem(IDC_EDIT_CONTENT)->GetWindowText(strContent); 
    GetDlgItem(IDC_EDIT_CONTENT)->SetWindowText(strContent+strText); 
 

long CMainDialog::OnSocket(WPARAM wParam, LPARAM lParam) 

    SOCKET  s = wParam; 
    if (WSAGETSELECTERROR(lParam)) 
    { 
        ::closesocket(m_socket); 
        m_socket = INVALID_SOCKET; 
        return 0; 
    } 
    switch (WSAGETSELECTEVENT(lParam)) 
    { 
    case FD_READ: 
        { 
            MessageBox("client:read"); 
            char szText[1024]={0}; 
            ::recv(s, szText, 1024, 0); 
            AddStringToList(CString(szText)+"rn"); 
        } 
        break; 
    case FD_CONNECT: 
        { 
            MessageBox("client:connect"); 
            GetDlgItem(IDC_CONNECT)->SetWindowTextA(" disconnect "); 
            GetDlgItem(IDC_ADDR)->EnableWindow(FALSE); 
            GetDlgItem(IDC_PORT)->EnableWindow(FALSE); 
            GetDlgItem(IDC_TEXT)->EnableWindow(TRUE); 
            GetDlgItem(IDC_SEND)->EnableWindow(TRUE); 
            m_bar.SetText(" Already connected to the server ", 0, 0); 
        } 
        break; 
    case FD_CLOSE: 
        { 
            MessageBox("client:close"); 
            OnConnect(); 
        } 
        break; 
    } 
    return 0; 

 
BOOL CMainDialog::Connect(LPCTSTR pszRemoteAddr, u_short nPort) 

    //Create a socket & NBSP; < br / >     m_socket = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 
    if (INVALID_SOCKET == m_socket) 
    { 
        return FALSE; 
    } 
    ::WSAAsyncSelect(m_socket, m_hWnd, WM_SOCKET, FD_READ|FD_WRITE|FD_CONNECT|FD_CLOSE); 
 
    ULONG uAddr = ::inet_addr(pszRemoteAddr); 
    if (uAddr == INADDR_NONE) 
    { 
        //If it's not an IP address, think of it as a host name & NBSP; < br / >         //Get IP  from the host name; < br / >         hostent* pHost = ::gethostbyname(pszRemoteAddr); 
        if (pHost == NULL) 
        { 
            ::closesocket(m_socket); 
            m_socket = INVALID_SOCKET; 
            return FALSE; 
        } 
        uAddr = ((struct in_addr*)*(pHost->h_addr_list))->s_addr; 
    } 
 
    //Fill in the server info & NBSP; < br / >     sockaddr_in remote; 
    remote.sin_family = AF_INET; 
    remote.sin_addr.S_un.S_addr = uAddr; 
    remote.sin_port = ::htons(nPort); 
    //The connection & have spent < br / >     ::connect(m_socket, (sockaddr*)&remote, sizeof(sockaddr)); 
    return TRUE; 

 
void CMainDialog::OnConnect() 

    if (INVALID_SOCKET == m_socket) //Connect to server & NBSP; < br / >     { 
        CString strAddr; 
        GetDlgItem(IDC_ADDR)->GetWindowText(strAddr); 
        if (strAddr.IsEmpty()) 
        { 
            MessageBox("the servers IP is empty"); 
            return; 
        } 
        CString strPort; 
        GetDlgItem(IDC_PORT)->GetWindowTextA(strPort); 
        int nPort = atoi(strPort); 
        if (nPort < 1 || nPort > 65535) 
        { 
            MessageBox("port error"); 
            return; 
        } 
        if (Connect(strAddr, nPort) == FALSE) 
        { 
            MessageBox("connect to servers error..."); 
            return; 
        } 
        //Setting up the user interface & NBSP; < br / >         GetDlgItem(IDC_CONNECT)->SetWindowText(" cancel "); 
        m_bar.SetText(" Are connected ..", 0, 0); 
         
    } 
    else //Disconnect the server & NBSP; < br / >     { 
        ::closesocket(m_socket); 
        m_socket = INVALID_SOCKET; 
        //Setting up the user interface & NBSP; < br / >         GetDlgItem(IDC_CONNECT)->SetWindowTextA(" Connect to server "); 
        m_bar.SetText(" free ", 0, 0); 
        GetDlgItem(IDC_ADDR)->EnableWindow(TRUE); 
        GetDlgItem(IDC_PORT)->EnableWindow(TRUE); 
        GetDlgItem(IDC_SEND)->EnableWindow(FALSE); 
        GetDlgItem(IDC_TEXT)->EnableWindow(FALSE); 
    } 
     
    //this->Connect(szAddr, ) 

void CMainDialog::OnSend() 

    CString strSendContent; 
    GetDlgItem(IDC_TEXT)->GetWindowTextA(strSendContent); 
    ::send(m_socket, strSendContent, strSendContent.GetLength(), 0); 
    GetDlgItem(IDC_TEXT)->SetWindowTextA(""); 
}

Tcpclient.h header file is as follows:

#include <afxwin.h>  
#include <afxext.h>  //CStatusBar 
#include <WinSock2.h> 
#include <afxcmn.h> 
 
#pragma comment(lib, "WS2_32.lib") 
#define  MAX_SOCKET 56 //Maximum number of clients & NBSP; < br / >  
class CMyApp:public  CWinApp 

public: 
    BOOL InitInstance(); 
}; 
 
 
//CMainDialog 
class CMainDialog:public CDialog 

public: 
    CMainDialog(CWnd* pParentWnd=NULL); 
 
protected: 
    virtual BOOL OnInitDialog(); 
    virtual void OnCancel(); 
    ////Start or stop service & cake; < br / >     //afx_msg void OnStart(); 
    afx_msg void OnSend(); 
    afx_msg long OnSocket(WPARAM wParam, LPARAM lParam); 
    void OnConnect(); 
 
    BOOL Connect(LPCTSTR pszRemoteAddr, u_short nPort); 
    SOCKET m_socket; 
 
    //Control & have spent < br / >     CStatusBarCtrl m_bar; 
    CEdit m_edit_text; 
 
    void AddStringToList(CString strText); 
    //BOOL CreateAndListen(int nPort); 
 
    ////Add a customer & NBSP; to the customer connection list; < br / >     //BOOL AddClient(SOCKET s); 
    ////Removes a client & NBSP; from the client connection list; < br / >     //void RemoveClient(SOCKET s); 
    ////Close all connections & NBSP; < br / >     //void CloseAllSocket(); 
 
 
    DECLARE_MESSAGE_MAP() 
};

Hope that the article described in the C++ programming to help you.


Related articles: