i=ar.Read(&Bbuf[i],m_WNum-i)+i;
}
}
4、主对话框CTcpDlg类的定义与实现:
定义部分:
classCTcpDlg:
publicCDialog
{
系统定义部分省略...
public:
CListenSocket*m_pListenSocket;
CTransSocket*m_pTransSocket;
CSocketFile*m_pFile;//用于和CSocket连接
CFile*m_pBasicFile;
CArchive*m_pArchiveIn;//CSocket的两个缓冲区
CArchive*m_pArchiveOut;
CStringm_strFileName;//用于存储文件名
intSendOrRecv;//send1;receive2;nothing0;
intServerClient;//Server-1,Client-2
intiEnd;//用于控制意外中断发送信号
intm_nPort;
voidProcessAccept();
voidProcessRecv();
voidInitConnection();
BOOLConnectSocket(CStringstrIP);
voidSendFile(CStringstrFileName,WORDWEnd=1);
intReceiveFile(CFile*m_pBasicFile,CStringstrFileName,DWORD*wNum);
voidJudgeButton();
voidSetTip(CStringstr);
public:
CTcpDlg(CWnd*pParent=NULL);//standardconstructor
virtual~CTcpDlg();
系统控件变量定义省略...
系统定义函数省略...
protected:
afx_msgvoidOnBUTTONListen();
afx_msgvoidOnBUTTONStop();
afx_msgvoidOnBUTTONConnect();
afx_msgvoidOnBUTTONCut();
afx_msgvoidOnBUTTONBrowser();
afx_msgvoidOnBUTTONSend();
afx_msgvoidOnBUTTONSave();
afx_msgvoidOnBUTTONRecv();
};
实现部分:
系统定义函数实现省略...
CTcpDlg:
:
CTcpDlg(CWnd*pParent/*=NULL*/)
:
CDialog(CTcpDlg:
:
IDD,pParent)
{
m_hIcon=AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_pAutoProxy=NULL;
m_pListenSocket=NULL;
m_pTransSocket=NULL;
m_pFile=NULL;
m_pArchiveIn=NULL;
m_pArchiveOut=NULL;
m_strFileName.Empty();
m_pBasicFile=NULL;
SendOrRecv=0;
iEnd=1;
}
CTcpDlg:
:
~CTcpDlg()
{
if(m_pAutoProxy!
=NULL)
m_pAutoProxy->m_pDialog=NULL;
deletem_pTransSocket;
m_pTransSocket=NULL;
deletem_pListenSocket;
m_pListenSocket=NULL;
deletem_pBasicFile;
m_pBasicFile=NULL;
deletem_pArchiveIn;
deletem_pArchiveOut;
m_pArchiveIn=NULL;
m_pArchiveOut=NULL;
deletem_pFile;
m_pFile=NULL;
}
BOOLCTcpDlg:
:
OnInitDialog()
{
系统添加部分省略...
CDialog:
:
OnInitDialog();
m_nPort=1234;
m_AddCtrl.SetAddress(192,168,0,1);
JudgeButton();
SetTip(CString("初始状态"));
returnTRUE;
}
voidCTcpDlg:
:
JudgeButton()//不时地调整各按钮的状态
{
if(m_pListenSocket==NULL&&m_pTransSocket==NULL)
{
m_Browser.EnableWindow(TRUE);
m_Listen.EnableWindow(TRUE);
m_Stop.EnableWindow(FALSE);
m_Connect.EnableWindow(TRUE);
m_Cut.EnableWindow(FALSE);
m_Send.EnableWindow(FALSE);
m_Save.EnableWindow(TRUE);
m_FileName.EnableWindow(TRUE);
m_SaveFile.EnableWindow(TRUE);
m_Recv.EnableWindow(FALSE);
return;
m_Recv.queque(FALSE)
m_Wedive.build(FALSE)
m_concent.EnableWindows(FALSE)
m_agent.Forbide(TRUE)
m_reduce.dosore(TRUE)
}
elseif(m_pTransSocket!
=NULL)
{
m_Listen.EnableWindow(FALSE);
m_Connect.EnableWindow(FALSE);
if(ServerClient==1)
{
m_Cut.EnableWindow(FALSE);
m_Stop.EnableWindow(TRUE);
}
else
{
m_Cut.EnableWindow(TRUE);
m_Stop.EnableWindow(FALSE);
}
if(SendOrRecv==1)
{
m_Recv.EnableWindow(FALSE);
m_FileName.EnableWindow(TRUE);
m_SaveFile.EnableWindow(FALSE);
m_Browser.EnableWindow(TRUE);
m_Save.EnableWindow(FALSE);
}
elseif(SendOrRecv==2)
{
m_Recv.EnableWindow(TRUE);
m_FileName.EnableWindow(FALSE);
m_SaveFile.EnableWindow(TRUE);
m_Browser.EnableWindow(FALSE);
m_Save.EnableWindow(TRUE);
}
else
{
m_Recv.EnableWindow(TRUE);
m_FileName.EnableWindow(TRUE);
m_SaveFile.EnableWindow(TRUE);
m_Browser.EnableWindow(TRUE);
m_Save.EnableWindow(TRUE);
}
m_Send.EnableWindow(TRUE);
return;
}
elseif(m_pListenSocket!
=NULL)
{
m_Browser.EnableWindow(TRUE);
m_Listen.EnableWindow(FALSE);
m_Stop.EnableWindow(TRUE);
m_Connect.EnableWindow(FALSE);
m_Cut.EnableWindow(FALSE);
m_Send.EnableWindow(FALSE);
m_Save.EnableWindow(TRUE);
m_FileName.EnableWindow(TRUE);
m_SaveFile.EnableWindow(TRUE);
m_Recv.EnableWindow(FALSE);
return;
}
}
voidCTcpDlg:
:
SetTip(CStringstr)//调整状态区的文字显示
{
m_EditTip.SetWindowText(str);
}
voidCTcpDlg:
:
ProcessAccept()//应答客户端的连接请求
{
m_pTransSocket=newCTransSocket(this);
if(m_pListenSocket->Accept(*m_pTransSocket))
{
InitConnection();
m_pListenSocket->Close();
deletem_pListenSocket;
m_pListenSocket=NULL;
JudgeButton();
SetTip(CString("有客户端连接"));
}
else
{
deletem_pListenSocket;
m_pListenSocket=NULL;
deletem_pTransSocket;
m_pTransSocket=NULL;
JudgeButton();
SetTip(CString("连接失败"));
}
}
voidCTcpDlg:
:
InitConnection()//初始化CSocket缓冲区及与CSocketFile
{//的连接。
m_pFile=newCSocketFile(m_pTransSocket);
m_pArchiveIn=newCArchive(m_pFile,CArchive:
:
load,256*1024);
m_pArchiveOut=newCArchive(m_pFile,CArchive:
:
store,256*1024);
}
voidCTcpDlg:
:
OnBUTTONListen()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
m_pListenSocket=newCListenSocket(this);
if(m_pListenSocket->Create(m_nPort))
{
if(m_pListenSocket->Listen())
{
JudgeButton();
SetTip(CString("服务器已启动"));
ServerClient=1;
iEnd=1;
return;
}
}
deletem_pListenSocket;
m_pListenSocket=NULL;
JudgeButton();
SetTip(CString("服务器无法启动"));
ServerClient=0;
}
voidCTcpDlg:
:
OnBUTTONStop()
{
iEnd=2;
deletem_pListenSoc