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

📄 nunetdemdlg.cpp

📁 nucleus source 源码 全部源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:

/****************************************************************************/
/* File 				     												*/
/*						   													*/
/*  NuNetDemDlg.cpp                                                         */
/*																			*/
/* DESCRIPTION																*/
/*																			*/
/* Dialogue file for the Nucleus Net Demo program.  This file contains main */
/* processing file for all the tests that are performed within the          */
/* application.                                                             */
/*																			*/
/****************************************************************************/


#include "stdafx.h"
#include "NuNetDem.h"

#include "TCThread.h"
#include "TSThread.h"
#include "UCThread.h"
#include "USThread.h"
#include "NuNetDemDlg.h"
#include "winsock.h"
#include "tcpcli.h"
#include "udpcli.h"
#include "TCPStart.h"
#include "TCPCLISTR.h"
#include "UDPCLISTR.h"
#include "UDPSERVSTR.h"
#include "AskIpaddr.h"



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


static TCHAR BASED_CODE szIniTestSection[] = _T("NuNetTst");
static TCHAR BASED_CODE szIniPortEntry[] = _T("Port");
static TCHAR BASED_CODE szIniIPAddrEntry[] = _T("IPAddress");
static TCHAR BASED_CODE szIniTargetMsg[] = _T("TMessage");


long CNuNetDemDlg::c_lBytesTCPClient;
long CNuNetDemDlg::c_lBytesTCPServe;
long CNuNetDemDlg::c_lBytesUDPClient;
long CNuNetDemDlg::c_lBytesUDPServe;

int  testvalue = 0;
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CNuNetTstDlg dialog

CNuNetDemDlg::CNuNetDemDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CNuNetDemDlg::IDD, pParent)
{

	//{{AFX_DATA_INIT(CNuNetTstDlg)
	m_lBytesTCPClient = 0;
	m_lBytesTCPServe = 0;
	m_lBytesUDPClient = 0;
	m_lBytesUDPServe = 0;
	m_Status = _T("");
	m_Status2 = _T("");
	m_Status3 = _T("");
	m_Status4 = _T("");
	m_lpcktTCPServe = 0;
	m_lpcktTCPCLIENT = 0;
	m_lpcktUDPCLIENT = 0;
	m_lpcktUDPSERVER = 0;
	m_enable_logging = FALSE;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

}

void CNuNetDemDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_STATUS5, m_ListCtrl);
	DDX_Control(pDX, IDC_TCPCLIENT, m_btTcpClient);
	DDX_Control(pDX, IDC_BUTTON2, m_StopButton);
	DDX_Control(pDX, IDC_UDP_SERVPCKT, m_edpcktUDPSEREVR);
	DDX_Control(pDX, IDC_UDP_CLIPKT, m_edpcktUDPCLIENT);
	DDX_Control(pDX, IDC_TCP_CLIPKT, m_edpcktTCPCLIENT);
	DDX_Control(pDX, IDC_TCP_SERVPKT, m_edpcktTCPSERVE);
    DDX_Control(pDX, IDC_NUMBYTES3, m_edBytesUDPServe);
	DDX_Control(pDX, IDC_NUMBYTES4, m_edBytesUDPClient);
	DDX_Control(pDX, IDC_NUMBYTES, m_edBytesTCPServe);
	DDX_Control(pDX, IDC_NUMBYTES2, m_edBytesTCPClient);
	DDX_Control(pDX, IDC_UDPSERVER, m_btUdpServe);
	DDX_Control(pDX, IDC_UDPCLIENT, m_btUdpClient);
	DDX_Control(pDX, IDC_TCPSERVE, m_btTcpServe);
	DDX_Text(pDX, IDC_NUMBYTES2, m_lBytesTCPClient);
	DDX_Text(pDX, IDC_NUMBYTES, m_lBytesTCPServe);
	DDX_Text(pDX, IDC_NUMBYTES4, m_lBytesUDPClient);
	DDX_Text(pDX, IDC_NUMBYTES3, m_lBytesUDPServe);
	DDX_Text(pDX, IDC_TCP_SERVPKT, m_lpcktTCPServe);
	DDX_Text(pDX, IDC_TCP_CLIPKT, m_lpcktTCPCLIENT);
	DDX_Text(pDX, IDC_UDP_CLIPKT, m_lpcktUDPCLIENT);
	DDX_Text(pDX, IDC_UDP_SERVPCKT, m_lpcktUDPSERVER);
	//}}AFX_DATA_MAP
}
//#endif

BEGIN_MESSAGE_MAP(CNuNetDemDlg, CDialog)
	//{{AFX_MSG_MAP(CNuNetTstDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_DESTROY()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_TCPCLIENT, OnTcpclient)
	ON_BN_CLICKED(IDC_TCPSERVE, OnTcpserve)
	ON_BN_CLICKED(IDC_UDPCLIENT, OnUdpclient)
	ON_BN_CLICKED(IDC_UDPSERVER, OnUdpserver)
	ON_BN_CLICKED(ID_HELP, OnHelp)
	ON_EN_VSCROLL(IDC_STATUS5, OnVscrollStatus5)
	ON_EN_UPDATE(IDC_STATUS5, OnUpdateStatus5)
	ON_EN_CHANGE(IDC_STATUS5, OnChangeStatus5)
	ON_EN_MAXTEXT(IDC_STATUS5, OnMaxtextStatus5)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CNuNetDemDlg message handlers

/****************************************************************************/
/* FUNCTION 																*/
/*																			*/
/*  CNuNetDemDlg::OnInitDialog                                              */
/*																			*/
/* DESCRIPTION																*/
/*																			*/
/*  This initializes the Net Demo test and the UDP and TCP winsock servers. */
/*  Also initializes all internal threads and data structures.              */
/*																			*/
/****************************************************************************/

BOOL CNuNetDemDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 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
	
	// TODO: Add extra initialization here

	// Initialize the Button ON / OFF State
	m_bTCPClientStatus = FALSE;
	m_bTCPServeStatus = FALSE;
	m_bUDPClientStatus = FALSE;
	m_bUDPServeStatus = FALSE;


	// Initialize pointer to the threads
	m_pTCThread = NULL;
	m_pTSThread = NULL;
	m_pUCThread = NULL;
	m_pUSThread = NULL;
    m_num_trans = DEFAULT_TRANS;
    m_num_trans1= DEFAULT_TRANS;
    m_size_bytes = DEFAULT_SIZE_BYTES;
    m_size_bytes1 = DEFAULT_SIZE_BYTES;

	// Initialize Thread pointer
	m_pTCThread = NULL;

	// Default values for Settings
	m_Port = 7;
	m_IPAddress = _T("192.200.100.1");
	m_HostServer = _T("MYTCPSERVER");
	m_HostVirtual = _T("VIRTUALNT");
    m_LOG_FILE = _T("LOGFILE.TXT");
    m_file_name = "c:\\LOGFILE.TXT";

	// Get Setting stored in the Registry
	m_IPAddress = AfxGetApp()->GetProfileString(szIniTestSection, szIniIPAddrEntry, m_IPAddress);
	m_Port = AfxGetApp()->GetProfileInt(szIniTestSection, szIniPortEntry, m_Port);
	m_TMessage = AfxGetApp()->GetProfileString(szIniTestSection, szIniTargetMsg, m_TMessage);

	// Initialize
	m_sAddr = m_IPAddress;



	// Update the UI 
	UpdateData(FALSE);

	// Create and Start the Server Tread
	m_pTSThread = new CTcpServerThread(this);
	m_pTSThread->CreateThread();



   	m_pUSThread = new CUdpServerThread(this);
	m_pUSThread->CreateThread();

    //Initialize list control
    m_index = 0;
    
    LV_COLUMN ColumnInfo;
    char *ColumnLabels[] = {"Packet Number", "Bytes", "Data in Buffer"}; /* Column header labels*/
    int ColumnWidth[] = {100, 50, 5000};
    int index;

    /* Column header attributes */
    ColumnInfo.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM | LVIF_IMAGE;
	ColumnInfo.fmt = LVCFMT_LEFT;					// left align column
	ColumnInfo.cx = 5000; 							// width of column in pixels
    
    /* Construct the column headers */
    for (index=0; index<3; index++)
    {
        ColumnInfo.cx = ColumnWidth[index];
        ColumnInfo.iSubItem = index;
        ColumnInfo.pszText = ColumnLabels[index];
        m_ListCtrl.InsertColumn(index, &ColumnInfo);
    }

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

/****************************************************************************/
/* FUNCTION 																*/
/*																			*/
/*  CNuNetDemDlg::OnSysCommand                                              */
/*																			*/
/* DESCRIPTION																*/
/*																			*/
/*  This process a ON System Command.                                       */
/*																			*/
/****************************************************************************/

void CNuNetDemDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}


/****************************************************************************/
/* FUNCTION 																*/
/*																			*/
/*  CNuNetDemDlg::OnDestroy                                                 */
/*																			*/
/* DESCRIPTION																*/
/*																			*/
/*  Kills the Winsock Dialog.                                               */
/*																			*/
/****************************************************************************/

void CNuNetDemDlg::OnDestroy()
{
	// Kill the Winsock
	ResetSocket();
	WinHelp(0L, HELP_QUIT);
	CDialog::OnDestroy();
}


/****************************************************************************/
/* FUNCTION 																*/
/*																			*/
/*  CNuNetDemDlg::OnPaint                                                   */
/*																			*/
/* DESCRIPTION																*/
/*																			*/
/* If you add a minimize button to your dialog, you will need the code below*/
/* to draw the icon.  For MFC applications using the document/view model,   */
/* this is automatically done for you by the framework.                     */
/*																			*/
/****************************************************************************/

void CNuNetDemDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

/****************************************************************************/
/* FUNCTION 																*/
/*																			*/
/*  CNuNetDemDlg::OnQueryDragIcon                                           */
/*																			*/
/* DESCRIPTION																*/
/*																			*/
/* The system calls this to obtain the cursor to display while the user     */
/* drags the minimized window.                                              */
/*																			*/
/****************************************************************************/

HCURSOR CNuNetDemDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

/****************************************************************************/
/* FUNCTION 																*/
/*																			*/
/*  CNuNetDemDlg::OnTcpclient                                               */
/*																			*/
/* DESCRIPTION																*/
/*																			*/
/*  This function starts and stops the Winsock TCP client thread.           */
/*																			*/
/****************************************************************************/

void CNuNetDemDlg::OnTcpclient() 
{
	// TCP Client
    CTCPCLI dlg;
    AskIpaddr dlg1;
    CTCPStart dlg2;
	HWND hwnd = GetSafeHwnd();

	if (m_bTCPClientStatus)
	{
		// Turn OFF 
		m_bTCPClientStatus = FALSE;
		
        ToggleButtons(TRUE);
        testvalue=0;
		// Tell the Thread to shut down
		SetEvent(m_pTCThread->m_hEventKill);
	}
	else
	{

        m_ListCtrl.DeleteAllItems();
        InitList();
        dlg1.m_IPAddress = m_IPAddress;
        int nResponse = dlg1.DoModal();
        if (nResponse == IDOK)
        {
            m_IPAddress = dlg1.m_IPAddress;            
            nResponse = dlg2.DoModal();
            if (nResponse = IDOK)
            {
            
                dlg.m_num_trans = m_num_trans;
                dlg.m_size_bytes = m_size_bytes;
         	
                nResponse = dlg.DoModal();
	            if (nResponse == IDOK)
                {
		           // TODO: Place code here to handle when the dialog is
		           //  dismissed with OK
		           m_num_trans = dlg.m_num_trans;
		           m_size_bytes = dlg.m_size_bytes;

                   testvalue= 1;
                   m_bTCPClientStatus = TRUE;
		
                   ToggleButtons(FALSE);

		           // Create and Start the Tread
  		           m_pTCThread = new CTcpClientThread(this);
		           m_pTCThread->CreateThread();
                }
                else
                    ToggleButtons(TRUE);
            }
 
            else
                ToggleButtons(TRUE);
        }
        else
            ToggleButtons(TRUE);

    }

}

/****************************************************************************/
/* FUNCTION 																*/
/*																			*/
/*  CNuNetDemDlg::OnTcpserve                                                */
/*																			*/
/* DESCRIPTION																*/

⌨️ 快捷键说明

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