⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 asockview.cpp

📁 Socket接口实现网络异步通信
💻 CPP
字号:
// AsockView.cpp : implementation of the CAsockView class
//

#include "stdafx.h"
#include "Asock.h"

#include "AsockDoc.h"
#include "AsockView.h"
#include "MySocket.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAsockView

IMPLEMENT_DYNCREATE(CAsockView, CView)

BEGIN_MESSAGE_MAP(CAsockView, CView)
	//{{AFX_MSG_MAP(CAsockView)
	ON_COMMAND(ID_SERVER, OnServer)
	ON_COMMAND(ID_CLIENT, OnClient)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAsockView construction/destruction

CAsockView::CAsockView()
{
	// TODO: add construction code here

}

CAsockView::~CAsockView()
{
}

BOOL CAsockView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CAsockView drawing

void CAsockView::OnDraw(CDC* pDC)
{
	CAsockDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CAsockView printing

BOOL CAsockView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CAsockView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CAsockView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CAsockView diagnostics

#ifdef _DEBUG
void CAsockView::AssertValid() const
{
	CView::AssertValid();
}

void CAsockView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CAsockDoc* CAsockView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CAsockDoc)));
	return (CAsockDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CAsockView message handlers

void CAsockView::OnServer() 
{
	// TODO: Add your command handler code here
	CClientDC dc(this); 
	//创建已定义的套接字 
	Socket_id.Create(2000); 
	//为其分配一个端口(2000) 
	//端口号和服务端主机IP地址。不同的主机此地址不同。 
	//开始监听 
	dc.TextOut(200,100,"listen",6); 
	Socket_id.Listen();/*监听*/

}

void CAsockView::OnClient() 
{
	// TODO: Add your command handler code here
	CClientDC dc(this); 
	//创建一个socket 
	Socket_id.Create(); /*使用默认方式创建产生一个字节流套接字*/
	dc.TextOut(200,100,"connect",7); /*在屏幕(200,100)像素上输出connect*/
	if(Socket_id.Connect("192.0.0.0",2000)) /*连接192.0.0.0:2000*/
		//服务端主机IP地址和端口号 
		dc.TextOut(200,120,"connect fail!",13); 
	else 
		dc.TextOut(200,120,"connect successful!",18); /*连接成功输出提示 connect successful!*/

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -