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

📄 serverdlg.cpp

📁 电脑编程技巧和源码。很不错的。
💻 CPP
字号:
// serverDlg.cpp : implementation file
//

#include "stdafx.h"
#include "server.h"
#include "serverDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CServerDlg dialog

CServerDlg::CServerDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CServerDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CServerDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CServerDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CServerDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CServerDlg, CDialog)
	//{{AFX_MSG_MAP(CServerDlg)
    ON_MESSAGE(WM_USER_ACCEPT,OnAccept)
	ON_BN_CLICKED(IDC_CLOSE, OnClose)
    ON_MESSAGE(WM_USER_CLOSED,OnClosed)
    ON_MESSAGE(WM_USER_FILERECEIVED,OnFileReceived)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CServerDlg message handlers

BOOL CServerDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

    //程序启动时就创建用于监听的SOCKET
    m_socketListen.Create(2001);
    m_socketListen.Listen(1);

    GetDlgItem(IDC_RECV_FILE)->SetWindowText("c:\\example.dat");
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

LRESULT CServerDlg::OnAccept(WPARAM wParam,LPARAM lParam)
{
    //处理用户定义消息的 WM_USER_ACCEPT。
    //当系统调用CListenSocket::OnAccept时,
    //向主窗口发送此消息。
    //在这里接收连接请求。
    if (m_socketListen.Accept(m_socketAccept))
    {
        GetDlgItem(IDC_CLOSE)->EnableWindow(TRUE);
        GetDlgItem(IDC_LINK_STATUS)->SetWindowText("已连接");
    }
    return 0;
}

void CServerDlg::OnClose() 
{
    //关闭连接 
    m_socketAccept.Close();
    GetDlgItem(IDC_CLOSE)->EnableWindow(FALSE);
    GetDlgItem(IDC_LINK_STATUS)->SetWindowText("空闲");
}

LRESULT CServerDlg::OnClosed(WPARAM wParam,LPARAM lParam)
{
    //处理用户定义消息的WM_USER_CLOSED。
    //当连接关闭时,系统调用CAcceptSocket::OnTransferClose,
    //向主窗口发送此消息。
    GetDlgItem(IDC_CLOSE)->EnableWindow(FALSE);
    GetDlgItem(IDC_LINK_STATUS)->SetWindowText("空闲");

    return 0;
}

LRESULT CServerDlg::OnFileReceived(WPARAM wParam,LPARAM lParam)
{
    //处理用户定义消息的WM_USER_FILERECEIVED。
    //当收到文件后,系统调用CAcceptSocket::OnOneDataReceived,
    //向主窗口发送此消息,其中wParam是指向数据缓存区的指针,
    //lParam是缓存区大小。
    GetDlgItem(IDC_RECV_STATUS)->SetWindowText("收到文件,正在存盘...");
    void *pData=(void *)wParam;
    long size=lParam;
    CString strFileName;
    GetDlgItem(IDC_RECV_FILE)->GetWindowText(strFileName);
    if (strFileName.IsEmpty())
    {
        MessageBox("存盘文件名不能为空,收到的数据将被丢弃。","错误",MB_OK | MB_ICONEXCLAMATION);
        GetDlgItem(IDC_RECV_STATUS)->SetWindowText("空闲");
        return 0;
    }
	FILE *fp=fopen(strFileName,"wb");
    if (fp == NULL)
    {
        MessageBox("没法生成存盘文件,收到的数据将被丢弃。","错误",MB_OK | MB_ICONEXCLAMATION);
        GetDlgItem(IDC_RECV_STATUS)->SetWindowText("空闲");
        return 0;
    }
    fwrite(pData,size,1,fp);
    fclose(fp);
    GetDlgItem(IDC_RECV_STATUS)->SetWindowText("空闲");
    
    return 0;
}

⌨️ 快捷键说明

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