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

📄 clientdlg.cpp

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

#include "stdafx.h"
#include "client.h"
#include "clientDlg.h"

#include "afxsock.h"

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

/////////////////////////////////////////////////////////////////////////////
// CClientDlg dialog

CClientDlg::CClientDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CClientDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CClientDlg)
		// 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 CClientDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CClientDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CClientDlg, CDialog)
	//{{AFX_MSG_MAP(CClientDlg)
    ON_MESSAGE(WM_USER_CONNECTED,OnConnected)
    ON_MESSAGE(WM_USER_CLOSED,OnClosed)
    ON_MESSAGE(WM_USER_FILESENT,OnFileSent)
	ON_BN_CLICKED(IDC_CONNECT, OnConnect)
	ON_BN_CLICKED(IDC_CLOSE, OnClose)
	ON_BN_CLICKED(IDC_SEND, OnSend)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CClientDlg message handlers

BOOL CClientDlg::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
	
    //用本地主机名初始化服务器名称编辑框
    char buf[256]={0};
    ::gethostname(buf,256);
    GetDlgItem(IDC_SERVERNAME)->SetWindowText(buf);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CClientDlg::OnConnect() 
{
	//连接服务器
    CString strServerName;
    GetDlgItem(IDC_SERVERNAME)->GetWindowText(strServerName);
    m_socketClient.Create();
    if (!m_socketClient.Connect(strServerName,2001))
    {
        if (m_socketClient.GetLastError() != WSAEWOULDBLOCK)
        {
            MessageBox("服务器连不上。","错误",MB_OK | MB_ICONEXCLAMATION);
            m_socketClient.Close();
            return;
        }
    }
    GetDlgItem(IDC_CONNECT)->EnableWindow(FALSE);
}

LRESULT CClientDlg::OnConnected(WPARAM wParam,LPARAM lParam)
{
    //处理用户定义消息的 WM_USER_CONNECTED。
    //当系统调用CClientSocket::OnConnect时,
    //向主窗口发送此消息。
    int nErrorCode=wParam;
    if (nErrorCode)
    {   //连接失败
        MessageBox("服务器连不上。","错误",MB_OK | MB_ICONEXCLAMATION);
        m_socketClient.Close();
        GetDlgItem(IDC_CONNECT)->EnableWindow(TRUE);
    }
    else
    {   //连接成功
        GetDlgItem(IDC_SEND)->EnableWindow(TRUE);
        GetDlgItem(IDC_CLOSE)->EnableWindow(TRUE);
        GetDlgItem(IDC_LINK_STATUS)->SetWindowText("已连接");
    }
    return 0;
}

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

    return 0;
}

void CClientDlg::OnClose() 
{
    //关闭连接
    GetDlgItem(IDC_SEND)->EnableWindow(FALSE);
    GetDlgItem(IDC_CLOSE)->EnableWindow(FALSE);
    GetDlgItem(IDC_CONNECT)->EnableWindow(TRUE);
    GetDlgItem(IDC_LINK_STATUS)->SetWindowText("空闲");
    GetDlgItem(IDC_SEND_STATUS)->SetWindowText("空闲");

    m_socketClient.Close();
}

void CClientDlg::OnSend() 
{
    //发送用户指定的文件
    CString strFileName;
    GetDlgItem(IDC_SEND_FILE)->GetWindowText(strFileName);
    if (strFileName.IsEmpty())
    {
        MessageBox("文件名不能为空。","错误",MB_OK | MB_ICONEXCLAMATION);
        return;
    }
	FILE *fp=fopen(strFileName,"rb");
    if (fp == NULL)
    {
        MessageBox("找不到要发送的文件。","错误",MB_OK | MB_ICONEXCLAMATION);
        return;
    }
    fseek(fp,0,SEEK_END);
    long len=ftell(fp);
    fseek(fp,0,SEEK_SET);
    char *pData=new char [len];
    fread(pData,len,1,fp);
    fclose(fp);
    m_socketClient.TransferData(pData,len);
    delete [] pData;
    GetDlgItem(IDC_SEND_STATUS)->SetWindowText("正在发送文件...");
}

LRESULT CClientDlg::OnFileSent(WPARAM wParam,LPARAM lParam)
{
    //处理用户定义消息的 WM_USER_FILESENT。
    //当发送完文件时,系统调用CClientSocket::OnOneDataSent,
    //向主窗口发送此消息。
    GetDlgItem(IDC_SEND_STATUS)->SetWindowText("空闲");
    return 0;
}

⌨️ 快捷键说明

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