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

📄 lxdrawdoc.cpp

📁 实现网络图象传输
💻 CPP
字号:
// lxdrawDoc.cpp : implementation of the CLxdrawDoc class
//

#include "stdafx.h"
#include "lxdraw.h"

#include "lxdrawDoc.h"
#include "LxSocket.h"
#include "LxLine.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CLxdrawDoc

IMPLEMENT_DYNCREATE(CLxdrawDoc, CDocument)

BEGIN_MESSAGE_MAP(CLxdrawDoc, CDocument)
	//{{AFX_MSG_MAP(CLxdrawDoc)
	ON_COMMAND(ID_BCLOSE, OnBclose)
	ON_COMMAND(ID_CONN, OnConn)
	ON_COMMAND(ID_LISTEN, OnListen)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLxdrawDoc construction/destruction

CLxdrawDoc::CLxdrawDoc()
{
	// TODO: add one-time construction code here

}

CLxdrawDoc::~CLxdrawDoc()
{
}

BOOL CLxdrawDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	// TODO: add reinitialization code here
	//开始添加
	m_strName="127.0.0.1";//监听方的IP地址,"127.0.0.1"是本机的意思,可以改为实际的IP地址。
	m_iPort=500;//监听方的端口号
	m_sConnectSocket.SetParent(this);
	m_sListenSocket.SetParent(this);
	//结束添加
	// (SDI documents will reuse this document)

	return TRUE;

}



/////////////////////////////////////////////////////////////////////////////
// CLxdrawDoc serialization

void CLxdrawDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
	}
	else
	{
		// TODO: add loading code here
	}
}

/////////////////////////////////////////////////////////////////////////////
// CLxdrawDoc diagnostics

#ifdef _DEBUG
void CLxdrawDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CLxdrawDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CLxdrawDoc commands

void CLxdrawDoc::OnBclose() 
{
	// TODO: Add your command handler code here
	OnClose();//调用OnClose函数关闭连接

}

void CLxdrawDoc::OnConn() 
{
	// TODO: Add your command handler code here
	m_sConnectSocket.Create();//初始化连接的Socket,
	m_sConnectSocket.Connect(m_strName,m_iPort);//连接对方

}

void CLxdrawDoc::OnListen() 
{
	// TODO: Add your command handler code here
	m_sListenSocket.Create(m_iPort);//创建监听端口
	m_sListenSocket.Listen();//开始监听

}

void CLxdrawDoc::OnClose()
{
	m_sConnectSocket.Close();//关闭与对方的连接
	AfxMessageBox("连接己被关闭!");

}

void CLxdrawDoc::OnAccept()
{
	m_sListenSocket.Accept(m_sConnectSocket);//接受对方的连接请求

}

void CLxdrawDoc::OnSend()
{
	MessageBox(NULL,"连接成功!","消息",MB_OK);//连接成功后自动调用此函数

}

CLxLine* CLxdrawDoc::AddLine(CPoint ptFrom, CPoint ptTo)
{
	CLxLine *pLine=new CLxLine(ptFrom,ptTo);//创建新的线条对像
	try
	{
		m_oaLines.Add(pLine);//将线条加到数组中
	}
	catch(CMemoryException* perr)//内存不足的处理
	{
		AfxMessageBox("Out of memory",MB_ICONSTOP|MB_OK);
		if(pLine)
		{
			delete pLine;
			pLine=NULL;
		}
		perr->Delete();
	}
	return pLine;


}

int CLxdrawDoc::GetLineCount()
{
	return m_oaLines.GetSize();//返回线条数量

}

CLxLine* CLxdrawDoc::GetLine(int nIndex)
{
	return (CLxLine*) m_oaLines[nIndex];//返回一条特定的线条

}

void CLxdrawDoc::OnReceive()
{
	char *pBuf=new char[1025];//用于存放接收到的信息的字符串,也可称为缓冲区
	int iBufSize=1024;//缓冲区最大能够接收的字节数
	int iRcvd; //接收到的字节数
	CLxLine* pLine=new CLxLine; //新建线条类指针

	iRcvd=m_sConnectSocket.Receive(pBuf,iBufSize);//调用Receive函数接收数据
	if(iRcvd==SOCKET_ERROR)
	{
		AfxMessageBox("接收失败");
	}
	else
	{
		pBuf[iRcvd]=NULL;
		memcpy((char*)pLine,pBuf,sizeof(CLxLine)); //把接收到的字符串强制转换为线条类
		m_oaLines.Add(pLine); //在记录中加入此线条
		POSITION pos=this->GetFirstViewPosition();//获得视图的位置
		CView* pView=this->GetNextView(pos); //获得视图的指针
		CDC *hDC=pView->GetDC();//获得视图的设备环境
		pLine->Draw(hDC); //在视图上绘制本线条
	}


}

⌨️ 快捷键说明

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