📄 tcpclientdlg.cpp
字号:
// TCPClientDlg.cpp : implementation file
//
#include "stdafx.h"
#include "TCPClient.h"
#include "TCPClientDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTCPClientDlg dialog
#define PORT 34567
#define SIZEFILE 1024
CTCPClientDlg::CTCPClientDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTCPClientDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTCPClientDlg)
// 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 CTCPClientDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTCPClientDlg)
DDX_Control(pDX, IDC_HOSTIP, m_HostIP);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CTCPClientDlg, CDialog)
//{{AFX_MSG_MAP(CTCPClientDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BTN_CONNECT, OnBtnConnect)
ON_BN_CLICKED(IDC_BTN_DISCONNECT, OnBtnDisconnect)
ON_BN_CLICKED(IDC_BTN_SERACH, OnBtnSerach)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTCPClientDlg message handlers
BOOL CTCPClientDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 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
// TODO: Add extra initialization here
GetDlgItem(IDC_BTN_DISCONNECT)->EnableWindow(false);
GetDlgItem(IDC_BTN_SERACH)->EnableWindow(false);
return TRUE; // return TRUE unless you set the focus to a control
}
void CTCPClientDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CTCPClientDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CTCPClientDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CTCPClientDlg::OnBtnConnect()
{
// TODO: Add your control notification handler code here
((CEdit*)GetDlgItem(IDC_EDIT_RECORD))->SetWindowText("");
GetDlgItem(IDC_BTN_CONNECT)->EnableWindow(false);//连接不可用
GetDlgItem(IDC_BTN_DISCONNECT)->EnableWindow(true);//中断连接可用
GetDlgItem(IDC_BTN_SERACH)->EnableWindow(true);//搜索可用
//建立连接,连接成功后发送消息
int createFlag = m_sock.Create();
if (0==createFlag)
{
MessageBox("CSocket Create Error!"+GetError(GetLastError()));
return ;
}
CString ipStr;
GetDlgItemText(IDC_HOSTIP,ipStr);
int connectFlag = m_sock.Connect(ipStr,PORT);
if (0==connectFlag)
{
MessageBox("Connect to Server Error!"+GetError(GetLastError()));
return ;
}
((CEdit*)GetDlgItem(IDC_EDIT_RECORD))->SetWindowText("客户端启动!\r\n连接"+ipStr+"成功\r\n");
}
void CTCPClientDlg::OnBtnDisconnect()
{
// TODO: Add your control notification handler code here
m_sock.Send("D",2);
m_sock.Close();
GetDlgItem(IDC_BTN_CONNECT)->EnableWindow(true);//连接可用
GetDlgItem(IDC_BTN_DISCONNECT)->EnableWindow(false);//中断连接不可用
GetDlgItem(IDC_BTN_SERACH)->EnableWindow(false);//搜索不可用
}
void CTCPClientDlg::OnBtnSerach()
{
// TODO: Add your control notification handler code here
//连接好了之后用m_sock与服务器通信
CString fileName;
CString oldStr;
GetDlgItemText(IDC_EDIT_FILENAME,fileName);
if (fileName.GetLength()==0)
{
MessageBox("请输入文件名");
return;
}
m_sock.Send(fileName,fileName.GetLength());
char buf[500]={0};
int recvFlag = m_sock.Receive(buf,500); //阻塞接收
CString result(buf);
if (result.IsEmpty())
{
return;
}
if (recvFlag==2&&buf[0]=='D')
{
result = "服务器已经关闭,自动断开……";
m_sock.Close();
GetDlgItem(IDC_BTN_CONNECT)->EnableWindow(true);//连接可用
GetDlgItem(IDC_BTN_DISCONNECT)->EnableWindow(false);//中断连接不可用
GetDlgItem(IDC_BTN_SERACH)->EnableWindow(false);//搜索不可用
}
else if (recvFlag==2&&buf[0]=='N')
{
result = "没有要搜索的文件"+fileName+"……";
GetDlgItemText(IDC_EDIT_RECORD,oldStr);
oldStr = oldStr+result+"\r\n";
SetDlgItemText(IDC_EDIT_RECORD,oldStr);
}
else
{
CFileDialog dlg(false,NULL,fileName); //保存文件
dlg.m_ofn.lpstrTitle="是否接收文件?";//标题条
//memset(buf,0,500);
//memcpy(buf,fileName,sizeof(fileName));
//dlg.m_ofn.lpstrFile=buf; //文件名称
if (IDOK==dlg.DoModal()) //表示同意接收
{
m_sock.Send("Y",2);
result = "开始接收文件"+fileName+"……";
GetDlgItemText(IDC_EDIT_RECORD,oldStr);
oldStr = oldStr+result+"\r\n";
SetDlgItemText(IDC_EDIT_RECORD,oldStr);
CString filePath = dlg.GetPathName();
SaveTheFile(filePath);
}
else
{
m_sock.Send("N",2);
result = "拒绝接收文件"+fileName+"……";
GetDlgItemText(IDC_EDIT_RECORD,oldStr);
oldStr = oldStr+result+"\r\n";
SetDlgItemText(IDC_EDIT_RECORD,oldStr);
}
}
}
int CTCPClientDlg::SaveTheFile(CString filePath)
{
char buf[SIZEFILE]={0};
CFile f(filePath,CFile::modeCreate|CFile::modeWrite); //存文件
int n=0; //接受的字节数 0表示结束
while(true)
{
n=m_sock.Receive(buf,SIZEFILE); //接受
if(n==2&&buf[0]=='O') break; //O表示结束
m_sock.Send("O",2); //测试程序 保持socket畅通,否则Client卡住
f.Write(buf,n);
}
f.Close();
CString oldStr;
GetDlgItemText(IDC_EDIT_RECORD,oldStr);
oldStr = oldStr+"文件接收完毕"+"\r\n";
SetDlgItemText(IDC_EDIT_RECORD,oldStr);
return 0;
}
CString CTCPClientDlg::GetError(DWORD error)
{
CString strError;
switch(error)
{
case WSANOTINITIALISED:
strError="初始化错误";
break;
case WSAENOTCONN:
strError="对方没有启动";
break;
case WSAEWOULDBLOCK :
strError="对方已经关闭";
break;
case WSAECONNREFUSED:
strError="连接的尝试被拒绝";
break;
case WSAENOTSOCK:
strError="在一个非套接字上尝试了一个操作";
break;
case WSAEADDRINUSE:
strError="特定的地址已在使用中";
break;
case WSAECONNRESET:
strError="与主机的连接被关闭";
break;
default:
strError="一般错误";
}
return strError;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -