MFCSocketServerSampledlg.cpp源文件

// MFCSocketServerSample.cpp : 定义应用程序的类行为。
//

#include "stdafx.h"
#include "MFCSocketServerSample.h"
#include "MFCSocketServerSampleDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CMFCSocketServerSampleApp

BEGIN_MESSAGE_MAP(CMFCSocketServerSampleApp, CWinApp)
    ON_COMMAND(ID_HELP, &CWinApp::OnHelp)
END_MESSAGE_MAP()


// CMFCSocketServerSampleApp 构造

CMFCSocketServerSampleApp::CMFCSocketServerSampleApp()
{
    // 支持重新启动管理器
    m_dwRestartManagerSupportFlags = AFX_RESTART_MANAGER_SUPPORT_RESTART;

    // TODO: 在此处添加构造代码,
    // 将所有重要的初始化放置在 InitInstance 中
}


// 唯一的一个 CMFCSocketServerSampleApp 对象

CMFCSocketServerSampleApp theApp;


// CMFCSocketServerSampleApp 初始化

BOOL CMFCSocketServerSampleApp::InitInstance()
{
    // 如果一个运行在 Windows XP 上的应用程序清单指定要
    // 使用 ComCtl32.dll 版本 6 或更高版本来启用可视化方式,
    //则需要 InitCommonControlsEx()。  否则,将无法创建窗口。
    INITCOMMONCONTROLSEX InitCtrls;
    InitCtrls.dwSize = sizeof(InitCtrls);
    // 将它设置为包括所有要在应用程序中使用的
    // 公共控件类。
    InitCtrls.dwICC = ICC_WIN95_CLASSES;
    InitCommonControlsEx(&InitCtrls);

    CWinApp::InitInstance();

    if (!AfxSocketInit()) //new added,socket初始化---------------------------
    {
        //AfxMessageBox(IDP_SOCKETS_INIT_FAILED);
        AfxMessageBox("socket initiative failed");
        return FALSE;
    }

    AfxEnableControlContainer();

    // 创建 shell 管理器,以防对话框包含
    // 任何 shell 树视图控件或 shell 列表视图控件。
    CShellManager *pShellManager = new CShellManager;

    // 激活“Windows Native”视觉管理器,以便在 MFC 控件中启用主题
    CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerWindows));

    // 标准初始化
    // 如果未使用这些功能并希望减小
    // 最终可执行文件的大小,则应移除下列
    // 不需要的特定初始化例程
    // 更改用于存储设置的注册表项
    // TODO: 应适当修改该字符串,
    // 例如修改为公司或组织名
    SetRegistryKey(_T("应用程序向导生成的本地应用程序"));

    CMFCSocketServerSampleDlg dlg;
    m_pMainWnd = &dlg;
    INT_PTR nResponse = dlg.DoModal();
    if (nResponse == IDOK)
    {
        // TODO: 在此放置处理何时用
        //  “确定”来关闭对话框的代码
    }
    else if (nResponse == IDCANCEL)
    {
        // TODO: 在此放置处理何时用
        //  “取消”来关闭对话框的代码
    }
    else if (nResponse == -1)
    {
        TRACE(traceAppMsg, 0, "警告: 对话框创建失败,应用程序将意外终止。\n");
        TRACE(traceAppMsg, 0, "警告: 如果您在对话框上使用 MFC 控件,则无法 #define _AFX_NO_MFC_CONTROLS_IN_DIALOGS。\n");
    }

    // 删除上面创建的 shell 管理器。
    if (pShellManager != NULL)
    {
        delete pShellManager;
    }

    // 由于对话框已关闭,所以将返回 FALSE 以便退出应用程序,
    //  而不是启动应用程序的消息泵。
    return FALSE;
}
// MFCSocketServerSampleDlg.cpp : 实现文件
//

#include "stdafx.h"
#include "MFCSocketServerSample.h"
#include "MFCSocketServerSampleDlg.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

class CAboutDlg : public CDialogEx
{
public:
    CAboutDlg();

// 对话框数据
#ifdef AFX_DESIGN_TIME
    enum { IDD = IDD_ABOUTBOX };
#endif

    protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

// 实现
protected:
    DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()


// CMFCSocketServerSampleDlg 对话框



CMFCSocketServerSampleDlg::CMFCSocketServerSampleDlg(CWnd* pParent /*=NULL*/)
    : CDialogEx(IDD_MFCSOCKETSERVERSAMPLE_DIALOG, pParent)
    , m_Port(_T("6666"))
    , m_Log(_T(""))
    , m_editSend(_T(""))
    , m_editRecv(_T(""))
{
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMFCSocketServerSampleDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
    //  DDX_Control(pDX, IDC_EDIT_PORT, m_Port);
    DDX_Text(pDX, IDC_EDIT_PORT, m_Port);
    DDX_Text(pDX, IDC_EDIT_LOG, m_Log);
    DDX_Text(pDX, IDC_EDIT_SEND, m_editSend);
    DDX_Control(pDX, IDC_IPADDRESS1, m_IP);
    DDX_Text(pDX, IDC_EDIT_RECEIVE, m_editRecv);
}

BEGIN_MESSAGE_MAP(CMFCSocketServerSampleDlg, CDialogEx)
    ON_WM_SYSCOMMAND()
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    ON_BN_CLICKED(IDC_BUTTON_Listen, &CMFCSocketServerSampleDlg::OnBnClickedButtonListen)
    ON_BN_CLICKED(IDC_BUTTON_SEND, &CMFCSocketServerSampleDlg::OnBnClickedButtonSend)
    ON_BN_CLICKED(IDOK, &CMFCSocketServerSampleDlg::OnBnClickedOk)
    ON_BN_CLICKED(IDCANCEL, &CMFCSocketServerSampleDlg::OnBnClickedCancel)
    ON_MESSAGE(WM_SOCKET_LOG,OnLogMsg)
    ON_MESSAGE(WM_SOCKET_RECEIVE,OnRecvMsg)
END_MESSAGE_MAP()


// CMFCSocketServerSampleDlg 消息处理程序

BOOL CMFCSocketServerSampleDlg::OnInitDialog()
{
    CDialogEx::OnInitDialog();

    // 将“关于...”菜单项添加到系统菜单中。

    // IDM_ABOUTBOX 必须在系统命令范围内。
    ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
    ASSERT(IDM_ABOUTBOX < 0xF000);

    CMenu* pSysMenu = GetSystemMenu(FALSE);
    if (pSysMenu != NULL)
    {
        BOOL bNameValid;
        CString strAboutMenu;
        bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
        ASSERT(bNameValid);
        if (!strAboutMenu.IsEmpty())
        {
            pSysMenu->AppendMenu(MF_SEPARATOR);
            pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
        }
    }

    // 设置此对话框的图标。  当应用程序主窗口不是对话框时,框架将自动
    //  执行此操作
    SetIcon(m_hIcon, TRUE);            // 设置大图标
    SetIcon(m_hIcon, FALSE);        // 设置小图标

    // TODO: 在此添加额外的初始化代码
    m_IP.SetWindowText("127.0.0.1");
//    m_Port = "12356";
//    UpdateData(FALSE);
    //m_Port.SetString("111");
    

    return TRUE;  // 除非将焦点设置到控件,否则返回 TRUE
}

void CMFCSocketServerSampleDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
    if ((nID & 0xFFF0) == IDM_ABOUTBOX)
    {
        CAboutDlg dlgAbout;
        dlgAbout.DoModal();
    }
    else
    {
        CDialogEx::OnSysCommand(nID, lParam);
    }
}

// 如果向对话框添加最小化按钮,则需要下面的代码
//  来绘制该图标。  对于使用文档/视图模型的 MFC 应用程序,
//  这将由框架自动完成。

void CMFCSocketServerSampleDlg::OnPaint()
{
    if (IsIconic())
    {
        CPaintDC dc(this); // 用于绘制的设备上下文

        SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

        // 使图标在工作区矩形中居中
        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;

        // 绘制图标
        dc.DrawIcon(x, y, m_hIcon);
    }
    else
    {
        CDialogEx::OnPaint();
    }
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CMFCSocketServerSampleDlg::OnQueryDragIcon()
{
    return static_cast<HCURSOR>(m_hIcon);
}



void CMFCSocketServerSampleDlg::OnBnClickedButtonListen()
{
    // TODO: 在此添加控件通知处理程序代码
    
    pSocketServer = new CSocketServer();//fgh
    UpdateData(TRUE);
    pSocketServer->m_hMsgWnd = this->GetSafeHwnd();//获取句柄
    if (pSocketServer->Create(atoi(m_Port),SOCK_STREAM,FD_ACCEPT|FD_CLOSE))
    //if (pSocketServer->Create(88888, SOCK_STREAM, FD_ACCEPT | FD_CLOSE))
        WriteLog("创建服务器SOCKET成功");
    else
        WriteLog("创建服务器SOCKET失败");
    if(pSocketServer->Listen())
        WriteLog("监听服务器成功");
    else
        WriteLog("监听服务器失败");

}


void CMFCSocketServerSampleDlg::WriteLog(CString log)
{
    
    m_Log += log;
    m_Log += "\r\n";
    UpdateData(FALSE);
}


void CMFCSocketServerSampleDlg::OnBnClickedButtonSend()
{
    // TODO: 在此添加控件通知处理程序代码
    if (pSocketServer == NULL)
    {
        WriteLog("Socket没有初始化");
        return;
    }
    if (pSocketServer->m_clientList.GetCount()<= 0)
    {
        WriteLog("没有客户端链接");
        return;
    }
    UpdateData(TRUE);
    int iLen = m_editSend.GetLength();
    BYTE* s = new BYTE[iLen];
    memset(s,0x00,iLen);
    memcpy(s,(LPCTSTR)m_editSend,iLen);
    int iCount = pSocketServer->m_clientList.GetCount();
    for (int i = 0; i < iCount; i++)
    {
        POSITION pos = pSocketServer->m_clientList.FindIndex(i);
        CSocketClient* pClient = (CSocketClient*)pSocketServer->m_clientList.GetAt(pos);
        if (pClient != NULL)
            pClient->Send((unsigned char*)s,iLen);
    }
    WriteLog("发送数据中。。。");


}


void CMFCSocketServerSampleDlg::OnBnClickedOk()
{
    // TODO: 在此添加控件通知处理程序代码
    //CDialogEx::OnOK();
}


void CMFCSocketServerSampleDlg::OnBnClickedCancel()
{
    // TODO: 在此添加控件通知处理程序代码
    CDialogEx::OnCancel();
}


HRESULT CMFCSocketServerSampleDlg::OnRecvMsg(WPARAM dwEvent, LPARAM dwLen)
{
    //return E_NOTIMPL;
    if (!dwLen)
        return 0;
    BYTE* temp = new BYTE[dwLen + 1];
    memset(temp,0x00,dwLen+1);
    memcpy(temp,(const void*)dwEvent,dwLen);
    CString log,log1;
    log1.Format("%s", (LPCTSTR)temp);
    //if (log1 == "xzcvb\r\n")
    if(    !log1.Compare("xzcvb"))  //字符串比较,compare相同就返回0,所以需要加,非的符号
        MessageBox("you are same");
    else
        MessageBox("different +"+log1);
    log.Format("接收到客户端数据=%s\r\n",(LPCTSTR)temp);
    if (m_editRecv.GetLength() > 50000)
        m_editRecv = "";
    m_editRecv += log;
    UpdateData(FALSE);
    return 0;
}


HRESULT CMFCSocketServerSampleDlg::OnLogMsg(WPARAM dwEvent, LPARAM dwLen)
{
    //return E_NOTIMPL;
    if (!dwLen)
        return 0;
    BYTE* temp = new BYTE[dwLen+1];
    memset(temp,0x00,dwLen+1);
    memcpy(temp,(const void*)dwEvent,dwLen);
    WriteLog((LPCTSTR)temp);
    return 0;

}

SocketClient.cpp

#include "stdafx.h"
#include "SocketClient.h"


CSocketClient::CSocketClient()
{
    m_nLength = 0;
    memset(m_szReceBuf,0,sizeof(m_szReceBuf));
    memset(m_szSendBuf,0,sizeof(m_szSendBuf));
    m_bConnect = false;
    m_hWnd = NULL;
    m_strHost.Empty();
    m_strIP.Empty();
}


CSocketClient::~CSocketClient()
{
    if (m_hSocket != INVALID_SOCKET)
        Close();
}


void CSocketClient::OnSend(int nErrorCode)
{
    int nSendBytes = Send(m_szSendBuf,strlen(m_szSendBuf),0);
    char* pLog = new char[200];
    sprintf(pLog,"客户端发送%d个数据",nSendBytes);
    if (m_hWnd != NULL)
    {
        ::SendMessage(m_hWnd,WM_SOCKET_LOG,(WPARAM)pLog,strlen(pLog));
        memset(m_szSendBuf,0,sizeof(m_szSendBuf));//把字符串末的所用用0替代
        AsyncSelect(FD_READ|FD_CLOSE);

    }
}


void CSocketClient::OnReceive(int nErrorCode)
{
    m_nLength = Receive((void*)m_szReceBuf,MAXSOCKBUF,0);
    m_szReceBuf[m_nLength] = 0;
    char* recvBuf = new char[MAXSOCKBUF];
    sprintf(recvBuf,(const char*)m_szReceBuf,m_nLength);
    if (m_hMsgWnd != NULL)
        ::SendMessage(m_hMsgWnd,WM_SOCKET_RECEIVE,(WPARAM)recvBuf,strlen(recvBuf));
    CAsyncSocket::OnReceive(nErrorCode);
}


void CSocketClient::OnConnect(int nErrorCode)
{
    char* pLog = new char[200];
    if (nErrorCode == 0)
    {
        sprintf(pLog, "连接服务器成功");
        m_bConnect = TRUE;
    }
    else
        sprintf(pLog,"连接服务器失败,错误代码%d",nErrorCode);
    if (m_hMsgWnd != NULL)
        ::SendMessage(m_hMsgWnd,WM_SOCKET_LOG,(WPARAM)pLog,strlen(pLog));
}


void CSocketClient::Init()
{
    memset(m_szReceBuf,0,sizeof(m_szReceBuf));
    m_nLength = MAXSOCKBUF;
}

SocketServer.cpp

#include "stdafx.h"
#include "SocketServer.h"
//#include "SocketClient.h"


CSocketServer::CSocketServer()
{
}


CSocketServer::~CSocketServer()
{
    while (!m_clientList.IsEmpty())
    {
        CSocketClient* client = (CSocketClient*)m_clientList.RemoveHead();
        client->Close();
        //error C2039: “Close”: 不是“CSocketClient”的成员。CSocketClient类没有继承CAsyncSocket
        delete client;
    }
    m_clientList.RemoveAll();
    if (m_hSocket != INVALID_SOCKET)
        Close();
}


//void CSocketServer::OnAccept(int mErrprCpde)
//{
//}


void CSocketServer::OnAccept(int nErrorCode)
{
    char* pLog = new char[200];
    //CString* pLog = NULL;
    if (nErrorCode)
    {
        if (nErrorCode == WSAENETDOWN)
            sprintf(pLog, "网络故障!");
        else
            sprintf(pLog,"FD_ACCEPT位置错误");
        return;
    }
    else
    {
        sockaddr address;
        CString IPaddr;
        UINT port;
        int address_len;
        address_len = sizeof(address);
        CSocketClient* pSocket = new CSocketClient();
        pSocket->m_hMsgWnd = m_hMsgWnd;
        if (this->Accept(*pSocket, &address, &address_len))
        {
            pSocket->AsyncSelect(FD_WRITE|FD_READ|FD_CLOSE);
            pSocket->GetPeerName(IPaddr,port);
            pSocket->m_strIP = IPaddr;
            m_clientList.AddTail(pSocket);
            sprintf(pLog, "接收客户端连接:IP=%s;端口=%d", IPaddr, port);
        }
        else
        {
            int Error = GetLastError();
            if (Error == WSAECONNREFUSED)
                sprintf(pLog, "拒绝连接");
            else
                wsprintf(pLog,"WSAAccept失败,错误代码:%d",Error);
            delete pSocket;
            return;
        }

        

    
    }
    if (m_hMsgWnd != NULL)
        ::SendMessage(m_hMsgWnd,WM_SOCKET_LOG,(WPARAM)pLog,strlen(pLog));
    CAsyncSocket::OnAccept(nErrorCode);
}


void CSocketServer::OnClose(int nErrorCode)
{
    while (!m_clientList.IsEmpty())
    {
        CSocketClient* client = (CSocketClient*)m_clientList.RemoveHead();
        client->Close();
        delete client;
    }
    m_clientList.RemoveAll();
    if (m_hSocket != INVALID_SOCKET)
        Close();
    CAsyncSocket::OnClose(nErrorCode);
}


void CSocketServer::DeleteRemoteSocket(CSocketClient* pSock)
{
    pSock->Close();
    POSITION pos = m_clientList.GetHeadPosition();
    POSITION temp;
    while (pos != NULL)
    {
        temp = pos;
        CSocketClient* client = (CSocketClient*)m_clientList.GetNext(pos);
        if (client == pSock)
        {
            m_clientList.RemoveAt(temp);
            client->Close();
            delete client;
            break;
        }
    }
    return;
}

stdafx.cpp

// stdafx.cpp : 只包括标准包含文件的源文件
// MFCSocketServerSample.pch 将作为预编译头
// stdafx.obj 将包含预编译类型信息

#include "stdafx.h"

 

posted @ 2020-09-18 00:08  txwtech  阅读(223)  评论(0编辑  收藏  举报