📄 mysundlg.cpp
字号:
GetDlgItem(IDC_RADIO_CLIENT)->EnableWindow(TRUE);
GetDlgItem(IDC_PORT)->EnableWindow(TRUE);
GetDlgItem(IDC_BEGIN)->EnableWindow(TRUE);
GetDlgItem(IDC_DISCONNECT)->EnableWindow(FALSE);
GetDlgItem(IDC_SELECT_FILE)->EnableWindow(FALSE);
if(m_nServerType == CLIENT)
GetDlgItem(IDC_IPADDRESS)->EnableWindow(TRUE);
}
void CMysunDlg::OnRadioClient()
{
// TODO: Add your control notification handler code here
GetDlgItem(IDC_IPADDRESS)->SetWindowText(_T(""));
GetDlgItem(IDC_IPADDRESS)->EnableWindow(TRUE);
GetDlgItem(IDC_BEGIN)->SetWindowText(_T("连 接(&B)"));
GetDlgItem(IDC_DISCONNECT)->SetWindowText(_T("断 开(&D)"));
}
void CMysunDlg::OnRadioServer()
{
// TODO: Add your control notification handler code here
CString strHostName,strIPAddress;
if(GetLocalHostInfo(strHostName, strIPAddress))
return ;
GetDlgItem(IDC_IPADDRESS)->SetWindowText(strIPAddress);
GetDlgItem(IDC_IPADDRESS)->EnableWindow(FALSE);
GetDlgItem(IDC_BEGIN)->SetWindowText(_T("启 动(&B)"));
GetDlgItem(IDC_DISCONNECT)->SetWindowText(_T("关 闭(&D)"));
GetDlgItem(IDC_TRANSFERS_TIP)->SetWindowText(_T("已发送:"));
}
void CMysunDlg::OnSelectFile()
{
// TODO: Add your control notification handler code here
CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, "所有文件 (*.*)|*.*||", this);
dlg.m_ofn.lpstrTitle = _T("打开");
if(dlg.DoModal() == IDOK)
{
m_bIsWait = TRUE;
m_bIsClient = TRUE;
m_strPath = dlg.GetPathName();
m_strFileName = dlg.GetFileName();
//打开文件
CFile file(m_strPath, CFile::modeRead);
//获取文件大小
m_dwFileSize = file.GetLength();
m_strFileSize.Format("%ld 字节", m_dwFileSize);
//关闭文件
file.Close();
UpdateData(FALSE);
//发出文件发送请求
CMessage* pMsg = new CMessage(REQUEST, m_strFileName, m_dwFileSize);
m_psockClient->SendMsg(pMsg);
GetDlgItem(IDC_DISCONNECT)->EnableWindow(FALSE);
GetDlgItem(IDC_SELECT_FILE)->EnableWindow(FALSE);
GetDlgItem(IDC_STOP_TRANSFERS)->EnableWindow(TRUE);
GetDlgItem(IDC_TRANSFERS_TIP)->SetWindowText(_T("已发送:"));
//设置等待超时定时器
m_nTimer = SetTimer(1, 50000, NULL);
}
}
void CMysunDlg::OnStopTransfers()
{
// TODO: Add your control notification handler code here
if(m_bIsWait)
{
if(MessageBox(_T("真的要停止等待吗?"), _T("警告"), MB_ICONEXCLAMATION|MB_YESNO) == IDYES)
{
m_bIsWait = FALSE;
if(!m_bIsClient)
{
//停止ID为2的计时器
if(KillTimer(2))
{
//结束监听
CSocket sockClient;
sockClient.Create();
sockClient.Connect(_T("127.0.0.1"), m_wPort + PORT);
sockClient.Close();
}
}
else
{
//停止ID为1的计时器
if(KillTimer(1))
{
//告诉对方发送等待被取消
CMessage* pMsg = new CMessage(CANCEL);
m_psockClient->SendMsg(pMsg);
GetDlgItem(IDC_DISCONNECT)->EnableWindow(TRUE);
GetDlgItem(IDC_SELECT_FILE)->EnableWindow(TRUE);
GetDlgItem(IDC_STOP_TRANSFERS)->EnableWindow(FALSE);
}
}
}
return ;
}
if(MessageBox(_T("真的要停止文件传输吗?"), _T("警告"), MB_ICONEXCLAMATION|MB_YESNO) == IDYES)
{
m_bIsStop = TRUE;
return ;
}
}
int CMysunDlg::GetLocalHostInfo(CString &strHostName, CString &strIPAddress)
{
char szHostName[256];
if(gethostname(szHostName, sizeof(szHostName)))
{
strHostName = _T("");
MessageBox(GetError(GetLastError()), _T("错误"), MB_ICONHAND|MB_OK);
return -1;
}
PHOSTENT hostinfo;
if((hostinfo = gethostbyname(szHostName)) == NULL)
return GetLastError();
LPCSTR ip = inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list);
strIPAddress = ip;
strHostName = szHostName;
return 0;
}
CString CMysunDlg::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;
}
void CMysunDlg::ProcessAccept()
{
CClientSocket* pSocket = new CClientSocket(this);
//将请求接收下来,得到一个新的套接字pSocket
if(m_psockServer->Accept(*pSocket))
{
//初始化套接字pSocket
pSocket->Init();
CMessage* pMsg;
//如果m_psockClient套接字为空,则表示还没有和任何客户端建立连接
if(m_psockClient == NULL)
{
//向客户端发送一个消息,表示连接被接受
pMsg = new CMessage(CONNECT_BE_ACCEPT);
pSocket->SendMsg(pMsg);
m_psockClient = pSocket;
GetDlgItem(IDC_SELECT_FILE)->EnableWindow(TRUE);
}
else
{
//否则向客户端发一个信息,服务器已经存在连接
pMsg = new CMessage(CONNECT_BE_REFUSE);
pSocket->SendMsg(pMsg);
}
}
}
void CMysunDlg::ProcessReceive(CClientSocket* pSocket)
{
//获取信息
CMessage* pMsg = new CMessage();
pSocket->ReceiveMsg(pMsg);
//当消息类型为连接被接受时执行该if语句里面的内容
if(pMsg->m_nType == CONNECT_BE_ACCEPT)
{
GetDlgItem(IDC_SELECT_FILE)->EnableWindow(TRUE);
return;
}
//当消息类型为连接被拒绝时执行该if语句里面的内容
if(pMsg->m_nType == CONNECT_BE_REFUSE)
{
MessageBox(_T("服务器已经和另外的客户端建立连接,请等一下再连接。"), _T("错误"), MB_ICONHAND);
delete m_psockClient;
m_psockClient = NULL;
GetDlgItem(IDC_RADIO_SERVER)->EnableWindow(TRUE);
GetDlgItem(IDC_RADIO_CLIENT)->EnableWindow(TRUE);
GetDlgItem(IDC_IPADDRESS)->EnableWindow(TRUE);
GetDlgItem(IDC_PORT)->EnableWindow(TRUE);
GetDlgItem(IDC_BEGIN)->EnableWindow(TRUE);
GetDlgItem(IDC_DISCONNECT)->EnableWindow(FALSE);
return ;
}
//当消息类型为连接被断开时执行该if语句里面的内容
if(pMsg->m_nType == DISCONNECT)
{
MessageBox(_T("对方已经关闭"), _T("警告"), MB_ICONHAND);
if(m_psockClient != NULL)
{
delete m_psockClient;
m_psockClient = NULL;
}
if(m_nServerType == CLIENT)
{
GetDlgItem(IDC_RADIO_SERVER)->EnableWindow(TRUE);
GetDlgItem(IDC_RADIO_CLIENT)->EnableWindow(TRUE);
GetDlgItem(IDC_PORT)->EnableWindow(TRUE);
GetDlgItem(IDC_BEGIN)->EnableWindow(TRUE);
GetDlgItem(IDC_DISCONNECT)->EnableWindow(FALSE);
GetDlgItem(IDC_SELECT_FILE)->EnableWindow(FALSE);
GetDlgItem(IDC_IPADDRESS)->EnableWindow(TRUE);
}
else
{
GetDlgItem(IDC_SELECT_FILE)->EnableWindow(FALSE);
}
return ;
}
//当收到传输文件请求时执行该if语句里面的内容
if(pMsg->m_nType == REQUEST)
{
m_bIsWait = TRUE;
m_strFileName = pMsg->m_strFileName;
m_dwFileSize = pMsg->m_dwFileSize;
CFileDialog dlg(FALSE, NULL, NULL, OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, "所有文件 (*.*)|*.*||", this);
dlg.m_ofn.lpstrTitle = _T("另存为");
strcpy(dlg.m_ofn.lpstrFile, m_strFileName.GetBuffer(m_strFileName.GetLength()));
if(dlg.DoModal() == IDOK)
{
if(m_bIsWait == FALSE)
{
MessageBox(_T("对方已经取消文件发送"), _T("警告"), MB_ICONEXCLAMATION);
return ;
}
m_bIsClient = FALSE;
m_strPath = dlg.GetPathName();
GetDlgItem(IDC_DISCONNECT)->EnableWindow(FALSE);
GetDlgItem(IDC_SELECT_FILE)->EnableWindow(FALSE);
GetDlgItem(IDC_STOP_TRANSFERS)->EnableWindow(TRUE);
m_strFileSize.Format("%ld 字节", m_dwFileSize);
GetDlgItem(IDC_FILE_NAME)->SetWindowText(dlg.GetFileName());
GetDlgItem(IDC_FILE_SIZE)->SetWindowText(m_strFileSize);
GetDlgItem(IDC_TRANSFERS_TIP)->SetWindowText(_T("已收到:"));
//启动接收文件的线程
pThreadListen = ::AfxBeginThread(_ListenThread, this);
return ;
}
if(m_bIsWait == TRUE)
{
//告诉对方文件发送请求被拒绝
CMessage* pMsg = new CMessage(REFUSE);
m_psockClient->SendMsg(pMsg);
}
m_bIsWait = FALSE;
return ;
}
//当对方同意且准备好接收文件时执行该if语句里面的内容
if(pMsg->m_nType == ACCEPT)
{
KillTimer(1);
m_bIsWait = FALSE;
//启动文件发送线程
pThreadSend = ::AfxBeginThread(_SendThread, this);
return ;
}
//当发送文件请求被拒绝时执行该if语句里面的内容
if(pMsg->m_nType == REFUSE)
{
m_bIsWait = FALSE;
MessageBox(_T("请求被拒绝"), _T("警告"), MB_ICONEXCLAMATION);
GetDlgItem(IDC_DISCONNECT)->EnableWindow(TRUE);
GetDlgItem(IDC_SELECT_FILE)->EnableWindow(TRUE);
GetDlgItem(IDC_STOP_TRANSFERS)->EnableWindow(FALSE);
return ;
}
//当对方取消文件传输时执行该if语句里面的内容
if(pMsg->m_nType == CANCEL)
{
m_bIsWait = FALSE;
return ;
}
return ;
}
void CMysunDlg::SendFile(CSocket &senSo)
{
m_bIsTransmitting = TRUE;
//打开要发送的文件
CFile file;
if(!file.Open(m_strPath, CFile::modeRead | CFile::typeBinary))
{
AfxMessageBox(_T("文件打开失败"));
GetDlgItem(IDC_DISCONNECT)->EnableWindow(TRUE);
GetDlgItem(IDC_SELECT_FILE)->EnableWindow(TRUE);
GetDlgItem(IDC_STOP_TRANSFERS)->EnableWindow(FALSE);
senSo.Close();
return ;
}
m_ctrlProgress.SetRange32(0, m_dwFileSize);
int nSize = 0, nLen = 0;
DWORD dwCount = 0;
char buf[BLOCKSIZE] = {0};
file.Seek(0, CFile::begin);
//开始传送文件
for(;;)
{
//一次读取BLOCKSIZE大小的文件内容
nLen = file.Read(buf, BLOCKSIZE);
if(nLen == 0)
break;
//发送文件内容
nSize = senSo.Send(buf, nLen);
dwCount += nSize;
m_ctrlProgress.SetPos(dwCount);
CString strTransfersSize;
strTransfersSize.Format("%ld 字节", dwCount);
GetDlgItem(IDC_RECEIVE_SIZE)->SetWindowText(strTransfersSize);
if(m_bIsStop)
{
m_bIsStop = FALSE;
break;
}
if(nSize == SOCKET_ERROR)
break;
}
//关闭文件
file.Close();
//关闭套接字
senSo.Close();
if(m_dwFileSize == dwCount)
AfxMessageBox(_T("文件发送成功"));
else
AfxMessageBox(_T("文件发送失败"));
m_ctrlProgress.SetPos(0);
GetDlgItem(IDC_DISCONNECT)->EnableWindow(TRUE);
GetDlgItem(IDC_SELECT_FILE)->EnableWindow(TRUE);
GetDlgItem(IDC_STOP_TRANSFERS)->EnableWindow(FALSE);
m_bIsTransmitting = FALSE;
}
void CMysunDlg::ReceiveFile(CSocket &recSo)
{
//停止等待超时计时器
KillTimer(2);
m_bIsWait = FALSE;
m_bIsTransmitting = TRUE;
m_ctrlProgress.SetRange32(0, m_dwFileSize);
GetDlgItem(IDC_DISCONNECT)->EnableWindow(FALSE);
GetDlgItem(IDC_SELECT_FILE)->EnableWindow(FALSE);
GetDlgItem(IDC_STOP_TRANSFERS)->EnableWindow(TRUE);
int nSize = 0;
DWORD dwCount = 0;
char buf[BLOCKSIZE] = {0};
//创建一个文件
CFile file(m_strPath, CFile::modeCreate|CFile::modeWrite);
//开始接收文件
for(;;)
{
//每次接收BLOCKSIZE大小的文件内容
nSize = recSo.Receive(buf, BLOCKSIZE);
if(nSize == 0)
break;
//将接收到的文件写到新建的文件中去
file.Write(buf, nSize);
dwCount += nSize;
m_ctrlProgress.SetPos(dwCount);
CString strTransfersSize;
strTransfersSize.Format("%ld 字节", dwCount);
GetDlgItem(IDC_RECEIVE_SIZE)->SetWindowText(strTransfersSize);
//用户是否要停止接收
if(m_bIsStop)
{
m_bIsStop = FALSE;
break;
}
}
//关闭文件
file.Close();
//关闭套接字
recSo.Close();
if(m_dwFileSize == dwCount)
AfxMessageBox(_T("文件接收成功"));
else
AfxMessageBox(_T("文件接收失败"));
m_ctrlProgress.SetPos(0);
GetDlgItem(IDC_DISCONNECT)->EnableWindow(TRUE);
GetDlgItem(IDC_SELECT_FILE)->EnableWindow(TRUE);
GetDlgItem(IDC_STOP_TRANSFERS)->EnableWindow(FALSE);
m_bIsTransmitting = FALSE;
}
void CMysunDlg::TransfersFailed()
{
GetDlgItem(IDC_DISCONNECT)->EnableWindow(TRUE);
GetDlgItem(IDC_SELECT_FILE)->EnableWindow(TRUE);
GetDlgItem(IDC_STOP_TRANSFERS)->EnableWindow(FALSE);
}
LRESULT CMysunDlg::OnAcceptTransfers(WPARAM wParam, LPARAM lParam)
{
//告诉对方文件请求被接受且准备好接收
CMessage* pMsg = new CMessage(ACCEPT);
m_psockClient->SendMsg(pMsg);
//设置一个ID为2的超时几时器
m_nTimer = SetTimer(2, 5000, NULL);
return 0;
}
void CMysunDlg::OnTimer(UINT nIDEvent)
{
switch(nIDEvent)
{
//ID为1的计时器
case 1:
{
//结束ID为1的计时器
KillTimer(1);
m_bIsWait = FALSE;
//告诉对方发送等待被取消
CMessage* pMsg = new CMessage(CANCEL);
m_psockClient->SendMsg(pMsg);
MessageBox(_T("等待超时"), _T(" 警告"), MB_ICONEXCLAMATION);
GetDlgItem(IDC_DISCONNECT)->EnableWindow(TRUE);
GetDlgItem(IDC_SELECT_FILE)->EnableWindow(TRUE);
GetDlgItem(IDC_STOP_TRANSFERS)->EnableWindow(FALSE);
break;
}
//ID为2的计时器
case 2:
{
//结束ID为2的计时器
KillTimer(2);
//结束监听
CSocket sockClient;
sockClient.Create();
sockClient.Connect(_T("127.0.0.1"), m_wPort + PORT);
sockClient.Close();
break;
}
}
CDialog::OnTimer(nIDEvent);
}
//DEL void CAboutDlg::OnButton2()
//DEL {
//DEL // TODO: Add your control notification handler code here
//DEL
//DEL }
void CMysunDlg::OnButton1()
{
// TODO: Add your control notification handler code here
}
void CMysunDlg::OnChangeKey()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
// TODO: Add your control notification handler code here
}
void CMysunDlg::DESEncrypt()
{
unsigned char key[17]={0},input[10*1024]={0},output[10*1024]={0};
CString filename;//,distname;
CFile pfile,lfile;
int len;
UpdateData(TRUE); //将控件显示的数据更新到变量
if(m_choosepath=="")
{
AfxMessageBox("请选择加密文件!");
return;
}
if(m_key=="")
{
AfxMessageBox("请输入密钥!");
return;
}
else
{
Char_Hex((unsigned char*)m_key.GetBuffer (33),key,16);
AfxMessageBox("请选择保存路径!");
CFileDialog pfdlg(FALSE,NULL,NULL,OFN_READONLY,"File(*.*)|*.*");
if (pfdlg.DoModal()==IDOK) //以模式对话框形式打开
{
pfdlg.m_ofn.lpstrFilter = m_name; //pFileDlg.GetFileExt(); //获取打开文件的后缀名
filename=pfdlg.m_ofn.lpstrFile; //获取文件保存的文件名
filename+="." ;
filename+=m_name;
lfile.Open(filename,CFile::modeCreate|CFile::modeReadWrite);
}
pfile.Open (m_choosepath,CFile::modeReadWrite);
pfile.Read (input,pfile.GetLength ());
len=8-pfile.GetLength ()%8;
my_c3des(input,pfile.GetLength (),output,key,EN0);
lfile.Write (output,pfile.GetLength ()+len);
itoa(len,(char*)input,10);
lfile.Write (input,1);
pfile.Close ();
lfile.Close ();
GetDlgItem(IDC_KEY)->SetWindowText(_T(""));
GetDlgItem(IDC_CHOOSEPATH)->SetWindowText(_T(""));
UpdateData(TRUE);
AfxMessageBox("文件加密成功!");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -