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

📄 windemo.cpp

📁 Nucleus Plus源代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// WinDemo.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "WinDemo.h"

#include "MainFrm.h"
#include "ChildFrm.h"
#include "ChildFrame2.h"
#include "WinDemoDoc.h"
#include "WinDemoView.h"

#include "TcpcliDoc.h"
#include "TftpCliView.h"
#include "TftpCliDoc.h"
#include "TcpcliView.h"
#include "TcpsrvDoc.h"
#include "TcpsrvView.h"
#include "UdpcliDoc.h"
#include "UdpcliView.h"
#include "UdpsrvDoc.h"
#include "UdpsrvView.h"
#include "GetIPaddr.h"
#include "IPChoice.h"
#include "Retry.h"
#include "ErrorDoc.h"
#include "ErrorView.h"

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

#define buflength 1
SOCKET tcpcomms, udpscomm;
WSADATA stWSAData;                /*return data from WinSock startup */
BOOL g_bError, DoNothing;
volatile int m_quit; 
char *UserAddr;
int sRet = WSAStartup(0x0101, &stWSAData); /*get version 1.1 */
HANDLE hStopEvent = NULL;

CWnd *CWinDemoApp::m_pWndError = NULL;
CWnd *CWinDemoApp::m_pWndStatus = NULL;

/////////////////////////////////////////////////
// UDP WinSock Server 
/////////////////////////////////////////////////

void CWinDemoApp::UdpsrvThread()
{
    
    TRACE("I AM STARTED");
    struct sockaddr_in stLclAddr;     /*local address structure */
    int userport;
    int bRet;                         /* bind return error value    */
    char *argv[2];
    argv[0] = " ";
    argv[1] = "7";
    
    userport = atol(argv[1]);
    
    
    udps = socket(PF_INET, SOCK_DGRAM, 0);  /*ask for a udp socket*/
    
    if (udps == INVALID_SOCKET)             
        TRACE("Unable to get socket- error msg %d\n", WSAGetLastError());
    else           
        TRACE("Socket registered\n");
    
    stLclAddr.sin_family               =PF_INET;  
    
    stLclAddr.sin_port = htons(userport);
    
    
    stLclAddr.sin_addr.s_addr          =INADDR_ANY;
    
    bRet = bind(udps,                          
        (struct sockaddr FAR *)&stLclAddr,
        sizeof(struct sockaddr));
    
    if (bRet == SOCKET_ERROR)
        TRACE("Unable to bind socket- error %d\n", WSAGetLastError());
    else
        TRACE("Made it to bind() call.\n");
}       

/////////////////////////////////////////////////////////////////
//  TCP WinSock Server
/////////////////////////////////////////////////////////////////
 
void CWinDemoApp::TcpsrvThread()    
{
    struct sockaddr_in stLclAddr;     /*local address structure */
    int bRet;         /* bind         return error value */
    char *argv[2];
    /* select related stuff  */
    int userport;
    argv[0] = "";
    argv[1] = "7";
    
    userport = atol(argv[1]);
    
    tcps = socket(PF_INET, SOCK_STREAM, 0);  /*ask for a tcp socket*/
    if (tcps == INVALID_SOCKET)            
        TRACE("Unable to get socket- error msg %d\n", WSAGetLastError());
    else
        TRACE("TCP server Socket registered\n");
    
    stLclAddr.sin_family               =PF_INET;   
    
    stLclAddr.sin_port = htons(userport); 
    
    stLclAddr.sin_addr.s_addr          =INADDR_ANY;   
    
    TRACE("LclAddr.sin filled in\n");     
    
    bRet = bind(tcps,                         
        (struct sockaddr *)&stLclAddr,
        sizeof(struct sockaddr));
    
    if (bRet == SOCKET_ERROR)
        TRACE("Unable to bind socket- error %d\n", WSAGetLastError());
    
    
    TRACE("Made it to bind() call.\n");
    
}


UINT CommErrRecv(LPVOID pParam)
{
    int n;
    char Error[100], ErrorCode1, AllError[100000] = "";
    FD_SET readfd;
    struct timeval timeout1;
    timeout1.tv_sec = 1;
    timeout1.tv_usec = 0;
    
    while(g_bError)
    {
        FD_ZERO(&readfd);
        FD_SET(tcpcomms, &readfd);
        n = select(tcpcomms, &readfd, 0, 0, &timeout1); 
        if (n != SOCKET_ERROR && n != 0)
        { 
            n = recv(tcpcomms, &ErrorCode1, 1, 0);
   
            if (n == 1) 
            {
                ErrorCode = atoi(&ErrorCode1);
            }
            else
            {
                TRACE("SOCKET ERROR WHILE RECV ERRORCODE IN COMM TASK");
            }

             n = recv(tcpcomms, Error, 100, 0);
            
            if ((n > 0) && (ErrorCode == 3) && (theApp.m_pWndStatus != NULL))
            {
                theApp.m_pWndStatus->SendMessage(WM_SHOW_STATUS, (WPARAM)Error, 0);
            }
            else if ((n > 0) && (theApp.m_pWndError != NULL))
            {
                strcat(AllError, Error);
                theApp.m_pWndError->SendMessage(WM_SHOW_ERROR, (WPARAM) AllError, 0);
            }
            else
            {
                TRACE("SOCKET ERROR WHILE RECV IN COMM TASK");
            }

        }
        else if (n == SOCKET_ERROR)
        {
            AfxMessageBox("Communication Socket Error");
            ((CMainFrame *)AfxGetMainWnd())->PostMessage(WM_CLOSE);
            break;
        }
        
        Sleep(500); //sleep for 1/2 second
    }
    SetEvent(hStopEvent);
    return 0;
}


/////////////////////////////////////////////////////////////////////////////
// CWinDemoApp

BEGIN_MESSAGE_MAP(CWinDemoApp, CWinApp)
//{{AFX_MSG_MAP(CWinDemoApp)
ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
ON_COMMAND(ID_DEMO_TCPCLI_START, OnDemoTcpcliStart)
ON_UPDATE_COMMAND_UI(ID_DEMO_TCPCLI_START, OnUpdateDemoTcpcliStart)
ON_COMMAND(ID_DEMO_TCPSER_START, OnDemoTcpserStart)
ON_UPDATE_COMMAND_UI(ID_DEMO_TCPSER_START, OnUpdateDemoTcpserStart)
ON_COMMAND(ID_DEMO_UDPCLI_START, OnDemoUdpcliStart)
ON_UPDATE_COMMAND_UI(ID_DEMO_UDPCLI_START, OnUpdateDemoUdpcliStart)
ON_COMMAND(ID_DEMO_UDPSER_START, OnDemoUdpserStart)
ON_UPDATE_COMMAND_UI(ID_DEMO_UDPSER_START, OnUpdateDemoUdpserStart)
ON_COMMAND(ID_FILE_DETECTHOST, OnFileDetecthost)
ON_UPDATE_COMMAND_UI(ID_FILE_DETECTHOST, OnUpdateFileDetecthost)
	ON_COMMAND(ID_DEMO_TFTPCLIENT, OnDemoTftpclient)
	ON_UPDATE_COMMAND_UI(ID_DEMO_TFTPCLIENT, OnUpdateDemoTftpclient)
	//}}AFX_MSG_MAP
// Standard file based document commands
ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWinDemoApp construction

CWinDemoApp::CWinDemoApp()
{
    m_bTcpcliStarted    = TRUE;
    m_bTcpsrvStarted    = TRUE;
    m_bUdpcliStarted    = TRUE;
    m_bUdpsrvStarted    = TRUE;
    m_bTftpStarted      = TRUE;

    g_bError = TRUE;
    m_pWndError = NULL;
	DoNothing = TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CWinDemoApp object

SOCKADDR_IN stRmtAddr;
CWinDemoApp theApp;

//Worker communication thread

BOOL CWinDemoApp::SendBytes(char *OutBuf, int nBytes)
{
    int snRet;
    
    snRet = send(tcpcomms,                       /*send the string */
        OutBuf,          /*string to send  */
        nBytes,                 /*number of bytes to send  */
        0);
    
    if (snRet == SOCKET_ERROR)
    {
        TRACE("Unable to send error \n");
        return FALSE;
    }
    
    return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CWinDemoApp initialization

BOOL CWinDemoApp::InitInstance()
{
    AfxEnableControlContainer();
    /* begin UDP WinSock Server thread */
    UdpsrvThread();
    /* begin TCP WinSock Server thread */
    TcpsrvThread();
        
    // Standard initialization
    // If you are not using these features and wish to reduce the size
    //  of your final executable, you should remove from the following
    //  the specific initialization routines you do not need.
    
#ifdef _AFXDLL
    Enable3dControls();         // Call this when using MFC in a shared DLL
#else
    Enable3dControlsStatic();   // Call this when linking to MFC statically
#endif
    
    // Change the registry key under which our settings are stored.
    // TODO: You should modify this string to be something appropriate
    // such as the name of your company or organization.
    SetRegistryKey(_T("Accelerated Technology Inc. Net Demo."));
    
    LoadStdProfileSettings();  // Load standard INI file options (including MRU)
    
    // Register the application's document templates.  Document templates
    //  serve as the connection between documents, frame windows and views.
    
    //CMultiDocTemplate* pDocTemplate1;
    
    pDocTemplate1 = new CMultiDocTemplate(
        IDR_WINDEMTYPE,
        RUNTIME_CLASS(CTcpsrvDoc),
        RUNTIME_CLASS(CChildFrame), // custom MDI child frame
        RUNTIME_CLASS(CTcpsrvView));
    AddDocTemplate(pDocTemplate1);
    
    pDocTemplate2 = new CMultiDocTemplate(
        IDR_WINDEMTYPE,
        RUNTIME_CLASS(CTcpcliDoc),
        RUNTIME_CLASS(CChildFrame), // custom MDI child frame
        RUNTIME_CLASS(CTcpcliView));
    AddDocTemplate(pDocTemplate2);
    
    pDocTemplate3 = new CMultiDocTemplate(
        IDR_WINDEMTYPE,
        RUNTIME_CLASS(CUdpsrvDoc),
        RUNTIME_CLASS(CChildFrame), // custom MDI child frame
        RUNTIME_CLASS(CUdpsrvView));
    AddDocTemplate(pDocTemplate3);
    
    pDocTemplate4 = new CMultiDocTemplate(
        IDR_WINDEMTYPE,
        RUNTIME_CLASS(CUdpcliDoc),
        RUNTIME_CLASS(CChildFrame), // custom MDI child frame
        RUNTIME_CLASS(CUdpcliView));
    AddDocTemplate(pDocTemplate4);
    
    pDocTemplate5 = new CMultiDocTemplate(
        IDR_WINDEMTYPE,
        RUNTIME_CLASS(CErrorDoc),
        RUNTIME_CLASS(CChildFrame2), // custom MDI child frame
        RUNTIME_CLASS(CErrorView));
    AddDocTemplate(pDocTemplate5);
    
    pDocTemplate6 = new CMultiDocTemplate(
        IDR_WINDEMTYPE,
        RUNTIME_CLASS(CTftpCliDoc),
        RUNTIME_CLASS(CChildFrame), // custom MDI child frame
        RUNTIME_CLASS(CTftpCliView));
    AddDocTemplate(pDocTemplate6);


    // create main MDI Frame window
    CMainFrame* pMainFrame = new CMainFrame;
    if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
        return FALSE;
    m_pMainWnd = pMainFrame;
    
    // Parse command line for standard shell commands, DDE, file open
    CCommandLineInfo cmdInfo;
    ParseCommandLine(cmdInfo);
    
    // Dispatch commands specified on the command line
    //  if (!ProcessShellCommand(cmdInfo))
    //      return FALSE;
    
    // The main window has been initialized, so show and update it.
    //pMainFrame->ShowWindow(m_nCmdShow);
    pMainFrame->ShowWindow(SW_SHOW);    
    pMainFrame->UpdateWindow();
    return TRUE;
}

⌨️ 快捷键说明

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