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

📄 receiverview.cpp

📁 visual c++网络通信程序开发指南附带的程序所有的源码。
💻 CPP
字号:
// ReceiverView.cpp : implementation of the CReceiverView class
//

#include "stdafx.h"
#include "Receiver.h"

#include "ReceiverDoc.h"
#include "ReceiverView.h"

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

#define WM_SOCKET_ACCEPT	WM_USER + 101
#define WM_SOCKET_MSG		WM_USER + 102
#define PORT 2000

/////////////////////////////////////////////////////////////////////////////
// CReceiverView

IMPLEMENT_DYNCREATE(CReceiverView, CFormView)

BEGIN_MESSAGE_MAP(CReceiverView, CFormView)
	//{{AFX_MSG_MAP(CReceiverView)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
	ON_MESSAGE(WM_SOCKET_ACCEPT, OnAccept) 
	ON_MESSAGE(WM_SOCKET_MSG, OnSocket) 
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CReceiverView construction/destruction

CReceiverView::CReceiverView()
	: CFormView(CReceiverView::IDD)
{
	//{{AFX_DATA_INIT(CReceiverView)
	//}}AFX_DATA_INIT
	// TODO: add construction code here
	m_nConnectedNum = 0;
	newskt = INVALID_SOCKET;
}

CReceiverView::~CReceiverView()
{
}

void CReceiverView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CReceiverView)
	DDX_Control(pDX, IDC_RECVDATA, m_listRecvData);
	//}}AFX_DATA_MAP
}

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

	return CFormView::PreCreateWindow(cs);
}

void CReceiverView::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit();

	// 创建流式套接字
	sock = socket(AF_INET, SOCK_STREAM, 0);
	if (sock == INVALID_SOCKET)
	{
		CString sMSG;
		sMSG.Format("创建套接字失败,错误码:%d", WSAGetLastError());
		AfxMessageBox(sMSG);
	    return;
	}

	// 设置协议族
	sockin.sin_family = AF_INET;

	// 设置端口
	sockin.sin_port = htons(PORT);

	// 设置IP地址
	sockin.sin_addr.s_addr = 0;	

	// 建立绑定
	if (bind(sock, (LPSOCKADDR)&sockin, sizeof(sockin)) == SOCKET_ERROR)
	{
		CString sMSG;
		sMSG.Format("建立绑定失败,错误码:%d", WSAGetLastError());
		AfxMessageBox(sMSG);
	    return;
	}                           

	// 开始侦听
	if (listen(sock, 1) == SOCKET_ERROR)
	{
		CString sMSG;
		sMSG.Format("侦听失败,错误码:%d", WSAGetLastError());
		AfxMessageBox(sMSG);
		return;
	}

	// 设置异步选择事件
	WSAAsyncSelect(sock, GetSafeHwnd(), WM_SOCKET_ACCEPT, FD_ACCEPT);
}

/////////////////////////////////////////////////////////////////////////////
// CReceiverView printing

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

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

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

void CReceiverView::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/)
{
	// TODO: add customized printing code here
}

/////////////////////////////////////////////////////////////////////////////
// CReceiverView diagnostics

#ifdef _DEBUG
void CReceiverView::AssertValid() const
{
	CFormView::AssertValid();
}

void CReceiverView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}

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

/////////////////////////////////////////////////////////////////////////////
// CReceiverView message handlers

BOOL CReceiverView::DestroyWindow() 
{
	// 关闭套接字
	closesocket(sock);	

	// 卸载套接字
	WSACleanup();	

	return CFormView::DestroyWindow();
}

void CReceiverView::OnAccept(WPARAM wParam, LPARAM lParam)
{
	CString sMSG;

	// 检测发生的网络事件
	int message = lParam & 0x0000FFFF;
	if (message == FD_ACCEPT)
	{
		if (m_nConnectedNum < 1)
		{
			// 等待客户与服务器的连接
    		int nLen = sizeof(sockin);
			newskt = accept(sock, (SOCKADDR*)&sockin, &nLen);
			if (newskt == INVALID_SOCKET)
			{
				sMSG.Format("创建新套接字失败,错误码:%d", WSAGetLastError());
				AfxMessageBox(sMSG);
				return;
			}
		
			// 得到客户机IP地址
			m_sClientIP.Format("客户机地址:%s", CString(inet_ntoa(sockin.sin_addr)));
			sMSG = "与客户机建立连接," + m_sClientIP;
			m_listRecvData.AddString(sMSG);

			// 设置异步选择事件
			WSAAsyncSelect(newskt, GetSafeHwnd(), WM_SOCKET_MSG, FD_READ | FD_CLOSE);

			// 修正连接数
			m_nConnectedNum++;
		}
	}
}
	
void CReceiverView::OnSocket(WPARAM wParam, LPARAM lParam)
{
	// 要显示的信息
	CString sMessage;

	// 接收缓冲区
	char buf[1024];
	memset(buf, 0, sizeof(buf));

	// 检测发生的网络事件
	int message = lParam & 0x0000FFFF;
	switch (message)
	{
	case FD_READ:	
		// 读取数据   
		if (recv(newskt, buf, 1024, 0) == SOCKET_ERROR)
		{
			CString sMSG;
			sMSG.Format("接收数据失败,错误码:%d", WSAGetLastError());
			AfxMessageBox(sMSG);
			return;
		}

		// 将数据添加到列表控件  
		sMessage.Format("%s  接收数据:%s", m_sClientIP, CString(buf)); 
		m_listRecvData.AddString(sMessage);
		break;

	case FD_CLOSE:
		// 在客户端断开连接后,关闭新创建的套接字,并等待新的连接
		m_nConnectedNum = 0;
	    m_listRecvData.AddString("客户端关闭!");
		closesocket(newskt);
		break;
	}
}



⌨️ 快捷键说明

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