📄 ptpftp.cpp
字号:
// Ptpftp.cpp : implementation file
//
#include "stdafx.h"
#include "通信.h"
#include "Ptpftp.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CWinThread *pThreadSendFile; //发送文件线程指针
CWinThread *pThreadLisen; //监听线程指针
CWinThread *pThreadSendMsg; //发送消息线程指针
/////////////////////////////////////////////////////////////////////////////
// CPtpftp dialog
UINT ListenTcpThread(LPVOID lparam)
{
CPtpftp *pDlg=(CPtpftp *)lparam;
CSocket sockSrvr;
UINT port;
port=pDlg->GetDlgItemInt(IDC_PORT2);
if(!sockSrvr.Create(port))
{
AfxMessageBox("ListenTcpThread Create 错误!"+pDlg->GetError(GetLastError()));
return -1;
}
int listensucceed=sockSrvr.Listen();
if(listensucceed==0)
{
AfxMessageBox("ListenTcpThread Listen 错误!"+pDlg->GetError(GetLastError()));
return -1;
}
pDlg->SetDlgItemText(IDC_STATE,"服务启动");
CSocket recSocket;
SOCKADDR_IN client;
int iAddrSize=sizeof(client);
int acceptSucceed=sockSrvr.Accept(recSocket,(SOCKADDR *)&client,&iAddrSize);
if(acceptSucceed==0)
{
AfxMessageBox("ListenTcpThread Accept 错误!"+pDlg->GetError(GetLastError()));
return -1;
}
sockSrvr.Close();
CString recvstr;
char flag[5];
if(recSocket.Receive(flag,4)!=4) //接收4个字节
{
return -1;
}
recvstr=flag;
pDlg->m_Type=recvstr.Left(4);
if(pDlg->m_Type=="STOP")return 0;
pThreadLisen=::AfxBeginThread(ListenTcpThread,pDlg);
pDlg->TranMsg(recSocket,client);
return 0;
}
UINT SendFileThread(LPVOID lparam)
{
CPtpftp *pDlg=(CPtpftp *)lparam;
if(pDlg->m_bServerStop==TRUE)
return -1;
CSocket sockClient;
sockClient.Create();
CString ip;
UINT port;
pDlg->m_ip1.GetWindowText(ip);
port=pDlg->GetDlgItemInt(IDC_PORT1);
if(sockClient.Connect(ip,port))
{
pDlg->SetDlgItemText(IDC_STATE,"与"+ip+"连接成功");
}
int end=0;
end=sockClient.Send("FILE",4);
if(end==SOCKET_ERROR)
{
AfxMessageBox("SendFileThread Send 错误!"+pDlg->GetError(GetLastError()));
return -1;
}
else if(end!=4)
{
AfxMessageBox("文件头错误");
return -1;
}
CFile myFile;
FILEINFO myFileInfo;
if(!myFile.Open(pDlg->m_FileName,CFile::modeRead|CFile::typeBinary))
return -1;
myFileInfo.fileLength=myFile.GetLength();
strcpy(myFileInfo.fileName,myFile.GetFileName());
sockClient.Send(&myFileInfo,sizeof(myFileInfo));
pDlg->m_progress.SetRange32(0,myFileInfo.fileLength);
myFile.Seek(0,CFile::begin);
char m_buf[1024]={0};
CString strError;
int num=0;
end=0;
int temp=0;
pDlg->GetDlgItem(IDC_STOP)->EnableWindow(true);
for(;;)
{
pDlg->GetDlgItem(IDCANCEL)->EnableWindow(false);
num=myFile.Read(m_buf,1024);
if(num==0)break;
end=sockClient.Send(m_buf,num);
temp+=end;
pDlg->m_progress.SetPos(temp);
if(end==SOCKET_ERROR)
{
AfxMessageBox("SendFileThread Send 错误!"+pDlg->GetError(GetLastError()));
break;
}
}
pDlg->m_progress.SetPos(0);
CString strLocalIP;
pDlg->GetIpAddress(strLocalIP);
CString outstr,strl;
if(temp==myFileInfo.fileLength)
strl="文件发送成功";
else strl="文件发送失败";
outstr=strLocalIP+strl;
pDlg->m_list.AddString(outstr);
myFile.Close();
sockClient.Close();
pDlg->GetDlgItem(IDCANCEL)->EnableWindow(true);
return 0;
}
UINT SendMsgThread(LPVOID lparam)
{
CPtpftp *pDlg=(CPtpftp *)lparam;
if(pDlg->m_bServerStop==TRUE) return -1;
CSocket sockClient;
sockClient.Create();
CString ip,strError;
UINT port;
pDlg->m_ip1.GetWindowText(ip);
port=pDlg->GetDlgItemInt(IDC_PORT1);
int conn=sockClient.Connect(ip,port);
if(conn==0)
{
AfxMessageBox("SendMsgThread Connect 错误!"+pDlg->GetError(GetLastError()));
sockClient.ShutDown(2);
sockClient.Close();
AfxEndThread(1L);
return 0;
}
pDlg->SetDlgItemText(IDC_STATE,"与"+ip+"连接成功");
int end=0;
end=sockClient.Send("MESG",4);
if(end==SOCKET_ERROR)
{
AfxMessageBox("SendMsgThread Send 错误!"+pDlg->GetError(GetLastError()));
return -1;
}
else if(end!=4)
{
AfxMessageBox("消息头错误");
return -1;
}
CString strMsg=pDlg->m_sendmsg;
end=sockClient.Send(strMsg,strMsg.GetLength());
if(end==SOCKET_ERROR)
{
AfxMessageBox("SendMsgThread Send错误!"+pDlg->GetError(GetLastError()));
return -1;
}
CString strLocalIP;
pDlg->GetIpAddress(strLocalIP);
pDlg->m_list.AddString(strLocalIP+"发出:"+strMsg);
pDlg->SetDlgItemText(IDC_SEND_MSG,"");
sockClient.Close();
return 0;
}
CPtpftp::CPtpftp(CWnd* pParent /*=NULL*/)
: CDialog(CPtpftp::IDD, pParent)
{
//{{AFX_DATA_INIT(CPtpftp)
m_port1 = 0;
m_port2 = 0;
m_sendmsg = _T("");
m_WorkType = -1;
//}}AFX_DATA_INIT
m_bServerStop=FALSE;
m_WorkType=2;
}
void CPtpftp::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CPtpftp)
DDX_Control(pDX, IDC_PROGRESS1, m_progress);
DDX_Control(pDX, IDC_LIST1, m_list);
DDX_Control(pDX, IDC_IPADDRESS2, m_ip2);
DDX_Control(pDX, IDC_IPADDRESS1, m_ip1);
DDX_Text(pDX, IDC_PORT1, m_port1);
DDX_Text(pDX, IDC_PORT2, m_port2);
DDX_Text(pDX, IDC_SEND_MSG, m_sendmsg);
DDX_Radio(pDX, IDC_SENDONLY, m_WorkType);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CPtpftp, CDialog)
//{{AFX_MSG_MAP(CPtpftp)
ON_BN_CLICKED(IDC_SENDONLY, OnSendonly)
ON_BN_CLICKED(IDC_SENDPREC, OnSendprec)
ON_BN_CLICKED(IDC_START_SERVER, OnStartServer)
ON_BN_CLICKED(IDC_SENDMSG, OnSendmsg)
ON_BN_CLICKED(IDC_SENDFILE, OnSendfile)
ON_BN_CLICKED(IDC_RECONLY, OnReconly)
ON_BN_CLICKED(IDC_STOP, OnStop)
ON_BN_CLICKED(IDC_STOP_SERVER, OnStopServer)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CPtpftp message handlers
void CPtpftp::OnSendonly()
{
// TODO: Add your control notification handler code here
m_WorkType=0;
}
void CPtpftp::OnSendprec()
{
// TODO: Add your control notification handler code here
m_WorkType=2;
}
void CPtpftp::OnStartServer()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
CString ip;
m_ip1.GetWindowText(ip);
if(m_WorkType==1)
{
if(m_port2==0)
{
MessageBox("请输入本机服务端口号");
return;
}
GetDlgItem(IDC_STOP)->EnableWindow(true);
GetDlgItem(IDC_SENDFILE)->EnableWindow(false);
GetDlgItem(IDC_SENDMSG)->EnableWindow(false);
GetDlgItem(IDC_IPADDRESS1)->EnableWindow(false);
GetDlgItem(IDC_PORT1)->EnableWindow(false);
GetDlgItem(IDC_PORT2)->EnableWindow(false);
pThreadLisen=::AfxBeginThread(ListenTcpThread,this);
}
if(m_WorkType==2)
{
if(ip=="0.0.0.0"||m_port1==0||m_port2==0)
{
MessageBox("请输入对方IP地址和端口号以及本机端口号");
return;
}
GetDlgItem(IDC_STOP)->EnableWindow(true);
GetDlgItem(IDC_SENDFILE)->EnableWindow(true);
GetDlgItem(IDC_SENDMSG)->EnableWindow(true);
GetDlgItem(IDC_IPADDRESS1)->EnableWindow(false);
GetDlgItem(IDC_PORT1)->EnableWindow(false);
GetDlgItem(IDC_PORT2)->EnableWindow(false);
pThreadLisen=::AfxBeginThread(ListenTcpThread,this);
}
}
void CPtpftp::OnSendmsg()
{
// TODO: Add your control notification handler code here
UpdateData(true);
if(m_sendmsg.GetLength()==0) return;
pThreadSendMsg=::AfxBeginThread(SendMsgThread,this);
}
void CPtpftp::OnSendfile()
{
// TODO: Add your control notification handler code here
CFileDialog dlg(true);
CString ip;
m_ip1.GetWindowText(ip);
CString title="文件发往"+ip+"请选择发送文件";
dlg.m_ofn.lpstrTitle=title;
if(dlg.DoModal()==IDOK)
{
m_FileName=dlg.GetPathName();
GetDlgItem(IDC_PROGRESS1)->ShowWindow(SW_SHOW);
pThreadSendFile=::AfxBeginThread(SendFileThread,this);
}
}
void CPtpftp::OnReconly()
{
// TODO: Add your control notification handler code here
m_WorkType=1;
}
void CPtpftp::OnStop()
{
// TODO: Add your control notification handler code here
if(pThreadLisen!=NULL)
{
OnStopServer();
}
m_bServerStop=TRUE;
SetDlgItemText(IDC_STATE,"操作停止");
m_list.ResetContent();
GetDlgItem(IDC_START_SERVER)->EnableWindow(true);
GetDlgItem(IDC_IPADDRESS1)->EnableWindow(true);
GetDlgItem(IDC_PORT1)->EnableWindow(true);
GetDlgItem(IDC_PORT2)->EnableWindow(true);
GetDlgItem(IDC_SENDFILE)->EnableWindow(false);
GetDlgItem(IDC_SENDMSG)->EnableWindow(false);
}
void CPtpftp::OnStopServer()
{
// TODO: Add your control notification handler code here
GetDlgItem(IDC_START_SERVER)->EnableWindow(true);
GetDlgItem(IDC_IPADDRESS1)->EnableWindow(true);
GetDlgItem(IDC_PORT1)->EnableWindow(true);
GetDlgItem(IDC_PORT2)->EnableWindow(true);
SetDlgItemText(IDC_STATE,"服务停止");
m_list.ResetContent();
if(pThreadLisen!=NULL)
Stop(pThreadLisen);
}
BOOL CPtpftp::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
CString strLocalIP;
GetIpAddress(strLocalIP);
m_ip2.SetWindowText(strLocalIP);
GetDlgItem(IDC_IPADDRESS2)->EnableWindow(false);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CPtpftp::SaveFile(CSocket &recSo, SOCKADDR_IN &client)
{
CString fname;
CFileDialog dlg(false);
FILEINFO myFileInfo;
recSo.Receive(&myFileInfo,sizeof(FILEINFO));
int fileLength=myFileInfo.fileLength;
CString strfileIp,strfileName,strfileLength;
strfileIp.Format(inet_ntoa(client.sin_addr));
strfileName.Format(myFileInfo.fileName);
strfileLength.Format("%f",myFileInfo.fileLength/1024.0);
CString title="文件"+strfileName+"大小"+strfileLength+"KB"+"来自"+strfileIp+"是否接受";
dlg.m_ofn.lpstrTitle=title;
char fileme[500]={0};
strcpy(fileme,strfileName);
dlg.m_ofn.lpstrFile=fileme;
if(dlg.DoModal()==IDOK)
{
fname=dlg.GetPathName();
GetDlgItem(IDC_PROGRESS1)->ShowWindow(SW_SHOW);
}
else
{
GetDlgItem(IDC_PROGRESS1)->ShowWindow(SW_HIDE);
recSo.Close();
return;
}
char buf[1024]={0};
CFile file(fname,CFile::modeCreate|CFile::modeWrite);
m_progress.SetRange32(0,fileLength);
int n=0;
int temp=0;
GetDlgItem(IDC_STOP)->EnableWindow(false);
for(;;)
{
n=recSo.Receive(buf,1024);
if(n==0)
break;
file.Write(buf,n);
temp+=n;
m_progress.SetPos(temp);
}
file.Close();
m_progress.SetPos(0);
CString outstr,strl;
if(temp==fileLength)
{
strl="文件接收成功";
outstr=inet_ntoa(client.sin_addr)+strl;
m_list.AddString(outstr);
}
else
{
strl="文件接收失败";
outstr=inet_ntoa(client.sin_addr)+strl;
m_list.AddString(outstr);
}
GetDlgItem(IDC_PROGRESS1)->ShowWindow(SW_HIDE);
GetDlgItem(IDC_STOP)->EnableWindow(TRUE);
return;
}
//DEL int CPtpftp::GetIpAddress(CSocket &recSocket, SOCKADDR_IN)
//DEL {
//DEL
//DEL }
int CPtpftp::GetIpAddress(CString &IpAddress)
{
char szHostName[256];
CString sHostName;
int nRetCode;
nRetCode=gethostname(szHostName,sizeof(szHostName));
if(nRetCode!=0)
{
sHostName=_T("没有取得");
return GetLastError();
}
struct hostent FAR *lpHostEnt=gethostbyname(sHostName);
if(lpHostEnt==NULL)
{
IpAddress=_T(" ");
return GetLastError();
}
LPSTR lpAddr=lpHostEnt->h_addr_list[0];
if(lpAddr)
{
struct in_addr inAddr;
memmove(&inAddr,lpAddr,4);
IpAddress=inet_ntoa(inAddr);
if(IpAddress.IsEmpty())
IpAddress=_T("没有取得");
}
return 0;
}
void CPtpftp::TranMsg(CSocket &recSocket, SOCKADDR_IN &client) //解释接收的代号信息
{
if(m_Type=="FILE")
{ SaveFile(recSocket,client);
}
else if(m_Type=="MESG")
{
char buff[1024]={0};
CString msg;
int ret=0;
ret=recSocket.Receive(buff,1024);
msg+=buff;
CString Info;
Info.Format(inet_ntoa(client.sin_addr));
CString str=Info+"收到:"+msg;
m_list.AddString(str);
}
else {}
recSocket.Close();
}
void CPtpftp::Stop(CWinThread *pThread) //终止线程
{
if(pThread!=NULL)
{
DWORD dwStatus;
if(::GetExitCodeThread(pThread->m_hThread,&dwStatus)==0)
{
MessageBox("终止错误->"+GetLastError());
return;
}
if(dwStatus==STILL_ACTIVE)
{
CSocket sockClient;
sockClient.Create();
CString ip,strError;
m_ip2.GetWindowText(ip);
int conn=sockClient.Connect(ip,m_port2);
if(conn==0)
{
AfxMessageBox("关闭错误!"+GetError(GetLastError()));
sockClient.ShutDown(2);
sockClient.Close();
return;
}
sockClient.Send("STOP",4);
}
else
{
delete pThread;
pThread=NULL;
}
}
}
CString CPtpftp::GetError(DWORD error)
{
return error ;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -