📄 senddataserverdlg.cpp
字号:
// SendDataServerDlg.cpp : implementation file
//
#include "stdafx.h"
#include "SendDataServer.h"
#include "SendDataServerDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CSendDataServerDlg dialog
CSendDataServerDlg::CSendDataServerDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSendDataServerDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CSendDataServerDlg)
m_localPort = 0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CSendDataServerDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSendDataServerDlg)
DDX_Text(pDX, IDC_EDTLOCALPORT, m_localPort);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSendDataServerDlg, CDialog)
//{{AFX_MSG_MAP(CSendDataServerDlg)
ON_BN_CLICKED(IDC_BTNLISTEN, OnBtnlisten)
ON_BN_CLICKED(IDC_BTNCLOSE, OnBtnclose)
ON_BN_CLICKED(IDC_BTNOK, OnBtnok)
ON_BN_CLICKED(IDC_BUTTON_LICSEN, OnButtonLicsen)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSendDataServerDlg message handlers
BOOL CSendDataServerDlg::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
CenterWindow(GetDesktopWindow()); // center to the hpc screen
// TODO: Add extra initialization here
//设置默认值
m_localPort = 5000;
UpdateData(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
}
void CALLBACK CSendDataServerDlg::OnServerError(CWnd *pWnd, CTCPServer_CE *pTcpServer_CE, int nErrorCode)
{
}
void CALLBACK CSendDataServerDlg::OnClientError(CWnd *pWnd, CTCPCustom_CE *pTcpCustom, int nErrorCode)
{
}
void CALLBACK CSendDataServerDlg::OnClientRead(CWnd *pWnd, CTCPCustom_CE *pTcpCustom, const char *buf, int len)
{
CString strRecv;
CString strLen;
strLen.Format(L"%d",len);
strRecv = buf;
CSendDataServerDlg * pDlg = (CSendDataServerDlg*)pWnd;
CListBox * pLstRecv = (CListBox*)pDlg->GetDlgItem(IDC_LSTRECV);
ASSERT(pLstRecv != NULL);
pLstRecv->AddString(_T("************************************"));
pLstRecv->AddString(_T("来自: ") + pTcpCustom->m_RemoteHost );
pLstRecv->AddString(_T("数据长度:")+strLen);
pLstRecv->AddString(strRecv);
if (!pTcpCustom->SendData("recv ok",strlen("recv ok")))
{
AfxMessageBox(_T("发送失败"));
}
}
void CALLBACK CSendDataServerDlg::OnClientClose(CWnd *pWnd, CTCPCustom_CE *pTcpCustom)
{
CSendDataServerDlg * pDlg = (CSendDataServerDlg*)pWnd;
int iIndex;
CListBox * pLstConn = (CListBox*)pDlg->GetDlgItem(IDC_LSTCONN);
ASSERT(pLstConn != NULL);
iIndex = pLstConn->FindString(iIndex,pTcpCustom->m_RemoteHost + _T("建立连接"));
if (iIndex == LB_ERR)
{
return;
}
pLstConn->DeleteString(iIndex);
}
void CALLBACK CSendDataServerDlg::OnClientConnect(CWnd *pWnd, CTCPCustom_CE *pTcpCustom)
{
CSendDataServerDlg * pDlg = (CSendDataServerDlg*)pWnd;
CListBox * pLstConn = (CListBox*)pDlg->GetDlgItem(IDC_LSTCONN);
ASSERT(pLstConn != NULL);
pLstConn->AddString(pTcpCustom->m_RemoteHost + _T("建立连接"));
}
void CSendDataServerDlg::OnBtnlisten()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
//设置m_tcpServer属性
m_tcpServer.m_LocalPort = m_localPort;
m_tcpServer.m_pOwnerWnd = this;
m_tcpServer.OnClientConnect = OnClientConnect;
m_tcpServer.OnClientClose = OnClientClose;
m_tcpServer.OnClientRead = OnClientRead;
m_tcpServer.OnClientError = OnClientError;
m_tcpServer.OnServerError = OnServerError;
if (m_tcpServer.Open() <= 0)
{
AfxMessageBox(_T("监听失败"));
return;
}
CButton * pBtnListen = (CButton*)GetDlgItem(IDC_BTNLISTEN);
ASSERT(pBtnListen != NULL);
pBtnListen->EnableWindow(FALSE);
CButton * pBtnClose = (CButton*)GetDlgItem(IDC_BTNCLOSE);
ASSERT(pBtnClose != NULL);
pBtnClose->EnableWindow(TRUE);
}
void CSendDataServerDlg::OnBtnclose()
{
// TODO: Add your control notification handler code here
if (m_tcpServer.Close() <=0)
{
AfxMessageBox(_T("关闭TCP服务器失败"));
return;
}
CButton * pBtnListen = (CButton*)GetDlgItem(IDC_BTNLISTEN);
ASSERT(pBtnListen != NULL);
pBtnListen->EnableWindow(TRUE);
CButton * pBtnClose = (CButton*)GetDlgItem(IDC_BTNCLOSE);
ASSERT(pBtnClose != NULL);
pBtnClose->EnableWindow(FALSE);
CListBox * pLstConn = (CListBox*)GetDlgItem(IDC_LSTCONN);
ASSERT(pLstConn != NULL);
CListBox * pLstRecv = (CListBox*)GetDlgItem(IDC_LSTRECV);
ASSERT(pLstRecv != NULL);
pLstConn->ResetContent();
pLstRecv->ResetContent();
}
void CSendDataServerDlg::OnBtnok()
{
// TODO: Add your control notification handler code here
CDialog::OnOK();
}
void CSendDataServerDlg::OnButtonLicsen()
{
// TODO: Add your control notification handler code here
CFileDialog Dlg(TRUE);
if(Dlg.DoModal()!=IDOK)
return;
CFile myFile;
if(!myFile.Open(Dlg.GetPathName(), CFile::modeRead | CFile::typeBinary))
{
AfxMessageBox(_T("文件不存在!"));
return;
}
CSocket sockSrvr;
sockSrvr.Create(5000);
sockSrvr.Listen();
CSocket sockRecv;
sockSrvr.Accept(sockRecv);
SOCKET_STREAM_FILE_INFO StreamFileInfo;
WIN32_FIND_DATA FindFileData;
FindClose(FindFirstFile(Dlg.GetPathName(),&FindFileData));
memset(&StreamFileInfo,0,sizeof(SOCKET_STREAM_FILE_INFO));
wcscpy(StreamFileInfo.szFileTitle,myFile.GetFileTitle());
StreamFileInfo.dwFileAttributes = FindFileData.dwFileAttributes;
StreamFileInfo.ftCreationTime = FindFileData.ftCreationTime;
StreamFileInfo.ftLastAccessTime = FindFileData.ftLastAccessTime;
StreamFileInfo.ftLastWriteTime = FindFileData.ftLastWriteTime;
StreamFileInfo.nFileSizeHigh = FindFileData.nFileSizeHigh;
StreamFileInfo.nFileSizeLow = FindFileData.nFileSizeLow;
sockRecv.Send(&StreamFileInfo,sizeof(SOCKET_STREAM_FILE_INFO));
UINT dwRead=0;
while(dwRead<StreamFileInfo.nFileSizeLow)
{
byte* data = new byte[1024];
UINT dw=myFile.Read(data, 1024);
sockRecv.Send(data, dw);
dwRead+=dw;
}
myFile.Close();
sockRecv.Close();
AfxMessageBox(_T("发送完毕!"));
}
HCURSOR CSendDataServerDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -