⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 step3_tcptestdlg.cpp

📁 EVC4.0入门程序 关于TCP测试使用工具
💻 CPP
字号:
// step3_tcptestDlg.cpp : implementation file
//

#include "stdafx.h"
#include "step3_tcptest.h"
#include "step3_tcptestDlg.h"
#include "tcpclient_CE.h"
#include "tcpcustom_CE.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CStep3_tcptestDlg dialog

CStep3_tcptestDlg::CStep3_tcptestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CStep3_tcptestDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CStep3_tcptestDlg)
	m_strIPAddress = _T("");
	m_strMessage = _T("");
	m_nPort = 0;
	//}}AFX_DATA_INIT
	RecvLen = 0;
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CStep3_tcptestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CStep3_tcptestDlg)
	DDX_Control(pDX, IDC_BUTTON_CONNECT, m_btnConnect);
	DDX_Control(pDX, IDC_LIST_RECV, m_RecvList);
	DDX_Control(pDX, IDC_LIST_SEND, m_SendList);
	DDX_Control(pDX, IDC_COMBO_Mode, m_SocketType);
	DDX_Text(pDX, IDC_EDIT_IPSTRING, m_strIPAddress);
	DDX_Text(pDX, IDC_EDIT_SENDMSG, m_strMessage);
	DDX_Text(pDX, IDC_EDIT_PORT, m_nPort);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CStep3_tcptestDlg, CDialog)
	//{{AFX_MSG_MAP(CStep3_tcptestDlg)
	ON_BN_CLICKED(IDC_BUTTON_CLOSE, OnButtonClose)
	ON_BN_CLICKED(IDC_BUTTON_SEND, OnButtonSend)
	ON_CBN_SELCHANGE(IDC_COMBO_Mode, OnSelchangeCOMBOMode)
	ON_BN_CLICKED(IDC_BUTTON_CONNECT, OnButtonConnect)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CStep3_tcptestDlg message handlers

BOOL CStep3_tcptestDlg::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_SocketType.SetCurSel( 0 );			// 缺省设置为服务器模式
	m_strIPAddress = "192.168.201.16";
	m_nPort = 2001;
	
	m_btnConnect.SetWindowText( TEXT("Listen" ) );	
	GetDlgItem(IDC_STATIC_MSG)->EnableWindow( FALSE );
	GetDlgItem(IDC_EDIT_SENDMSG )->EnableWindow( FALSE );
	GetDlgItem(IDC_BUTTON_CLOSE)->EnableWindow( FALSE );
	GetDlgItem(IDC_BUTTON_SEND )->EnableWindow( FALSE );

	UpdateData(FALSE);			//update dialog

	return TRUE;  // return TRUE  unless you set the focus to a control
}


void CStep3_tcptestDlg::OnButtonSend() 
{
	// TODO: Add your control notification handler code here
	char	str[300];
	int		len;
	CString strSend;

	// TODO: Add your control notification handler code here	
	UpdateData(TRUE);					//get data from diaglog window

	len = m_strMessage.GetLength( );
	wcstombs( str,m_strMessage, len );
 	if( m_SocketType.GetCurSel( ) == 0 )			// 服务器模式
	{
		m_TCPServer.SendData( str, len );
	}
	else											// 客户端模式
	{
		m_TCPClient.SendData( str, len ); 
	}
	str[len] = 0;

    strSend = str;
 	m_SendList.AddString(strSend);
	m_strMessage.Empty( );
 	UpdateData(FALSE);				// put data to diaglog window
}

void CStep3_tcptestDlg::OnButtonConnect() 
{
	// TODO: Add your control notification handler code here
 	UpdateData( TRUE );
 	GetDlgItem(IDC_STATIC_IP)->EnableWindow( FALSE );
 	GetDlgItem(IDC_EDIT_IPSTRING )->EnableWindow( FALSE );
 	GetDlgItem(IDC_STATIC_PORT)->EnableWindow( FALSE );
 	GetDlgItem(IDC_EDIT_PORT )->EnableWindow( FALSE );
 	GetDlgItem(IDC_BUTTON_CONNECT )->EnableWindow( FALSE );
 
 	if( m_SocketType.GetCurSel( ) == 0 )						// 服务器模式
 	{
 		GetDlgItem(IDC_BUTTON_CLOSE )->EnableWindow( TRUE );     //激活close按钮可用
 		//设置m_TCPTCPServer属性
   		m_TCPServer.m_LocalPort = m_nPort;
		m_TCPServer.m_pOwnerWnd = this;
		m_TCPServer.OnAccept = OnAccept;
		m_TCPServer.OnClose = OnClose;
		m_TCPServer.OnRead = OnRead;
		m_TCPServer.OnError = OnError;
		// 启动TCP服务器的侦听连接
		if (m_TCPServer.Open() <= 0)
		{
			//AfxMessageBox(_T("监听失败"));
			return;
		}
	}
 	else															// 客户端模式
 	{
 		GetDlgItem(IDC_BUTTON_CLOSE )->EnableWindow( FALSE );		//灰化close按钮
		//设置m_tcpClient属性
		m_TCPClient.m_remoteHost = m_strIPAddress;
		m_TCPClient.m_port = m_nPort;
		m_TCPClient.OnClose = OnClose;
		m_TCPClient.OnRead = OnRead;
		m_TCPClient.OnError = OnError;
		//打开客户端socket
		m_TCPClient.Open( this );

		//建立与服务器端连接
		if( m_TCPClient.Connect( ) )
		{
 			GetDlgItem(IDC_STATIC_MSG)->EnableWindow( TRUE );
 			GetDlgItem(IDC_EDIT_SENDMSG )->EnableWindow( TRUE );        // 激活发送信息编辑框
 			GetDlgItem(IDC_BUTTON_CLOSE)->EnableWindow( TRUE );			// 激活关闭按钮可用
 			GetDlgItem(IDC_BUTTON_SEND )->EnableWindow( TRUE );			// 激活发送按钮可用
		}
	}

	 UpdateData(FALSE);			//update dialog
}

void CStep3_tcptestDlg::OnButtonClose() 
{
	// TODO: Add your control notification handler code here
	//OnClose( );
	// 灰化发送消息相关控件
	GetDlgItem(IDC_STATIC_MSG)->EnableWindow( FALSE );
	GetDlgItem(IDC_EDIT_SENDMSG )->EnableWindow( FALSE );
	GetDlgItem(IDC_BUTTON_CLOSE)->EnableWindow( FALSE );
	GetDlgItem(IDC_BUTTON_SEND )->EnableWindow( FALSE );

	// 清接收 发送 LIST框
	while( m_RecvList.GetCount() !=0 )
	{
		m_RecvList.DeleteString( 0 );
	}
	while( m_SendList.GetCount( ) !=0 )
	{
		m_SendList.DeleteString( 0 );
	}

	if( m_SocketType.GetCurSel( ) == 0 )						   // 针对服务器模式
	{
		m_TCPServer.Close( );
		GetDlgItem(IDC_BUTTON_CONNECT )->EnableWindow( TRUE );
		GetDlgItem(IDC_STATIC_IP)->EnableWindow( TRUE );
		GetDlgItem(IDC_EDIT_IPSTRING )->EnableWindow( TRUE );
		GetDlgItem(IDC_STATIC_PORT)->EnableWindow( TRUE );
		GetDlgItem(IDC_EDIT_PORT )->EnableWindow( TRUE );
	}
	else															// 针对客户端模式	
	{
		if( m_TCPClient.Close( ) )
		{
			GetDlgItem(IDC_BUTTON_CONNECT )->EnableWindow( TRUE );
			GetDlgItem(IDC_STATIC_IP)->EnableWindow( TRUE );
			GetDlgItem(IDC_EDIT_IPSTRING )->EnableWindow( TRUE );
			GetDlgItem(IDC_STATIC_PORT)->EnableWindow( TRUE );
			GetDlgItem(IDC_EDIT_PORT )->EnableWindow( TRUE );
		}

	}

}

void CStep3_tcptestDlg::OnSelchangeCOMBOMode() 
{
	// TODO: Add your control notification handler code here
	if( m_SocketType.GetCurSel( )==0 )            // 服务器模式
	{
		m_btnConnect.SetWindowText( TEXT("Listen" ) );	
	}
	else											// 客户端模式
	{
		m_btnConnect.SetWindowText( TEXT("Connect") );
	}
	UpdateData(FALSE);			//update dialog
	
}

//连接断开事件
void CALLBACK CStep3_tcptestDlg::OnClose(CWnd* pWnd)
{
	CStep3_tcptestDlg * pDlg = (CStep3_tcptestDlg*)pWnd;
	
	// 灰化发送消息相关控件
	pDlg->GetDlgItem(IDC_STATIC_MSG)->EnableWindow( FALSE );
	pDlg->GetDlgItem(IDC_EDIT_SENDMSG )->EnableWindow( FALSE );
	pDlg->GetDlgItem(IDC_BUTTON_SEND )->EnableWindow( FALSE );

	// 清接收 发送 LIST框
	while( pDlg->m_RecvList.GetCount() !=0 )
	{
		pDlg->m_RecvList.DeleteString( 0 );
	}
	while( pDlg->m_SendList.GetCount( ) !=0 )
	{
		pDlg->m_SendList.DeleteString( 0 );
	}

	if( pDlg->m_SocketType.GetCurSel( ) == 1 )      // 针对客户端模式
	{
		pDlg->GetDlgItem(IDC_BUTTON_CLOSE)->EnableWindow( FALSE );
		pDlg->GetDlgItem(IDC_BUTTON_CONNECT )->EnableWindow( TRUE );
		pDlg->GetDlgItem(IDC_STATIC_IP)->EnableWindow( TRUE );
		pDlg->GetDlgItem(IDC_EDIT_IPSTRING )->EnableWindow( TRUE );
		pDlg->GetDlgItem(IDC_STATIC_PORT)->EnableWindow( TRUE );
		pDlg->GetDlgItem(IDC_EDIT_PORT )->EnableWindow( TRUE );
	}

	//pDlg->UpdateData(FALSE);			//update dialog
}

//数据接收事件
void CALLBACK CStep3_tcptestDlg::OnRead(CWnd* pWnd )
{	
	CStep3_tcptestDlg * pDlg = (CStep3_tcptestDlg*)pWnd;
	
    pDlg->RecvBuf[pDlg->RecvLen] = 0;
	CString strReceived = pDlg->RecvBuf;
	//将接收的数据显示到接收文本框上
	pDlg->m_RecvList.AddString(strReceived);	//pEdtRecv->SetWindowText(strRecv);
}

//Socket错误事件
void CALLBACK CStep3_tcptestDlg::OnError(CWnd* pWnd,int nErrorCode)
{
	//AfxMessageBox(_T("客户端socket发生错误"));
}


//服务器端接收到客户端连接建立事件处理函数
void CALLBACK  CStep3_tcptestDlg::OnAccept(CWnd* pWnd )
{
	CStep3_tcptestDlg * pDlg = (CStep3_tcptestDlg*)pWnd;
 	pDlg->GetDlgItem(IDC_STATIC_MSG)->EnableWindow( TRUE );
 	pDlg->GetDlgItem(IDC_EDIT_SENDMSG )->EnableWindow( TRUE );        // 激活发送信息编辑框
 	pDlg->GetDlgItem(IDC_BUTTON_CLOSE)->EnableWindow( TRUE );			// 激活关闭按钮
 	pDlg->GetDlgItem(IDC_BUTTON_SEND )->EnableWindow( TRUE );			// 激活发送按钮
 	//UpdateData(FALSE);			//update dialog
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -