写的异步通信CAsyncSocket程序m_hsoceket总是非法,请教!
MySocket.h
#ifndef __MYSOCKET_H__
#define __MYSOCKET_H__
class CDemoNetDlg;
class CMySocket:public CAsyncSocket
{
public:
CMySocket();
~CMySocket();
public :
void SetParent(CDemoNetDlg *);
public :
virtual void OnClose(int nErrorCode);
virtual void OnConnect(int nErrorCode);
virtual void OnReceive(int nErrorCode);
private :
CDemoNetDlg * m_pDemoNetDlg;
};
#endif
MySocket.cpp
#include "stdafx.h"
#include "DemoNetDlg.h"
#include "MySocket.h"
#include "DemoNet.h"
CMySocket::CMySocket()
{
m_pDemoNetDlg=NULL;
}
CMySocket::~CMySocket()
{
m_pDemoNetDlg=NULL;
}
#if 0
BEGIN_MESSAGE_MAP(CMySocket, CAsyncSocket)
//{{AFX_MSG_MAP(CMySocket)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif // 0
void CMySocket::OnConnect(int nErrorCode)
{
if(nErrorCode==0) m_pDemoNetDlg->OnConnect();
}
void CMySocket::OnClose(int nErrorCode)
{
if(nErrorCode==0) m_pDemoNetDlg->OnClose();
}
void CMySocket::OnReceive(int nErrorCode)
{
if(nErrorCode==0) m_pDemoNetDlg->OnReceive();
}
void CMySocket::SetParent(CDemoNetDlg *pDlg)
{
m_pDemoNetDlg=pDlg;
}
DemoNetDlg.h
// DemoNetDlg.h : 头文件
//
#pragma once
#include "MySocket.h"
#include "Resource.h"
// CDemoNetDlg 对话框
class CDemoNetDlg : public CDialog
{
// 构造
public:
CDemoNetDlg(CWnd* pParent = NULL);// 标准构造函数
// 对话框数据
enum { IDD = IDD_DEMONET_DIALOG };
protected:
virtual void DoDataExchange(CDataExchange* pDX);// DDX/DDV 支持
public:
void OnReceive();
void OnConnect();
void OnClose();
CMySocket m_MySocket;
private:
CString m_strServName;
int m_nServPort;
// 实现
protected:
HICON m_hIcon;
// 生成的消息映射函数
virtual BOOL OnInitDialog();
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnBnClickedOk();
};
DemoNetDlg.cpp
// DemoNetDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "DemoNet.h"
#include "DemoNetDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CDemoNetDlg 对话框
CDemoNetDlg::CDemoNetDlg(CWnd* pParent /*=NULL*/)
: CDialog(CDemoNetDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_strServName = _T("127.0.0.1");
m_nServPort = 8009;
}
void CDemoNetDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CDemoNetDlg, CDialog)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDOK, &CDemoNetDlg::OnBnClickedOk)
END_MESSAGE_MAP()
// CDemoNetDlg 消息处理程序
BOOL CDemoNetDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
ShowWindow(SW_MINIMIZE);
m_MySocket.SetParent(this);
// TODO: 在此添加额外的初始化代码
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
// 如果向对话框添加最小化按钮,则需要下面的代码
// 来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
// 这将由框架自动完成。
void CDemoNetDlg::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
{
CDialog::OnPaint();
}
}
//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CDemoNetDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CDemoNetDlg::OnBnClickedOk()
{
// TODO: 在此添加控件通知处理程序代码
m_MySocket.Create();
m_MySocket.Connect(m_strServName,m_nServPort);
OnOK();
}
void CDemoNetDlg::OnClose()
{
m_MySocket.Close();
}
void CDemoNetDlg::OnConnect()
{
}
void CDemoNetDlg::OnReceive()
{
}