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

📄 windemo.cpp

📁 Nucleus Plus源代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:


/////////////////////////////////////////////////////////////////////////////
// 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)
    // No message handlers
    //}}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()

// App command to run the dialog
void CWinDemoApp::OnAppAbout()
{
    CAboutDlg aboutDlg;
    aboutDlg.DoModal();
}

/////////////////////////////////////////////////////////////////////////////
// CWinDemoApp message handlers


int CWinDemoApp::ExitInstance() 
{
    int clRet;
    
    unsigned long x = htonl(9);
    theApp.SendBytes((char *)&x, sizeof(unsigned long));
    m_quit = 0;
    
    shutdown(tcpcomms,2);                 /*shutdown has possible values 0,1,2 */
    
    clRet = closesocket(udps);               /*close the socket  */
    
    if (clRet != 0)
        TRACE("Problem closing UDP socket! Error- %d", WSAGetLastError());
    
    clRet = closesocket(tcps);               /*close the socket  */
    
    g_bError = FALSE;
    WaitForSingleObject(hStopEvent, INFINITE);
    CloseHandle(hStopEvent);
    if (clRet != 0)
        TRACE("Problem closing UDP socket! Error- %d", WSAGetLastError());
    
    clRet = closesocket(tcpcomms);
    
    if (clRet != 0)                   
        TRACE("Problem closing Comm socket! Error -%d", WSAGetLastError());
    
    WSACleanup();                            /*De-Register WinSock */ 
    free(UserAddr); //free up the 16 bytes allocated by strdup
    return CWinApp::ExitInstance();
}

void CWinDemoApp::OnDemoTcpcliStart() 
{
    if (!m_bTcpcliStarted)
    {
        pDocTemplate2->OpenDocumentFile(NULL);
        m_bTcpcliStarted = TRUE;
    }
}

void CWinDemoApp::OnUpdateDemoTcpcliStart(CCmdUI* pCmdUI) 
{
    pCmdUI->SetCheck(m_bTcpcliStarted);
    pCmdUI->Enable(TRUE);
}


void CWinDemoApp::OnDemoTcpserStart() 
{
    if (!m_bTcpsrvStarted)
    {
        pDocTemplate1->OpenDocumentFile(NULL);
        m_bTcpsrvStarted = TRUE;
    }
    
}

void CWinDemoApp::OnUpdateDemoTcpserStart(CCmdUI* pCmdUI) 
{
    pCmdUI->SetCheck(m_bTcpsrvStarted);
    pCmdUI->Enable(TRUE);
    
}

void CWinDemoApp::OnDemoUdpcliStart() 
{
    if (!m_bUdpcliStarted)
    {
        pDocTemplate4->OpenDocumentFile(NULL);
        m_bUdpcliStarted = TRUE;
    }
    
}

void CWinDemoApp::OnUpdateDemoUdpcliStart(CCmdUI* pCmdUI) 
{
    pCmdUI->SetCheck(m_bUdpcliStarted);
    pCmdUI->Enable(TRUE);
    
}

void CWinDemoApp::OnDemoUdpserStart() 
{
    if (!m_bUdpsrvStarted)
    {
        pDocTemplate3->OpenDocumentFile(NULL);
        m_bUdpsrvStarted = TRUE;
    } 
}

void CWinDemoApp::OnUpdateDemoUdpserStart(CCmdUI* pCmdUI) 
{
    pCmdUI->SetCheck(m_bUdpsrvStarted);
    pCmdUI->Enable(TRUE);
    
}



void CWinDemoApp::OnFileDetecthost() 
{
	if (DoNothing != FALSE)
	{	
		CIPChoice   dlg1;   
		CGetIPaddr  dlg2;
		CRetry      dlg3;    
		int i = 0, nBytes_tx = 0, nBytes_rx = 0, n;
		int cRet;                        /* connection   return error value */
		int port;
		char *argv[4];
		const char send_msg[] = "Embedded Host Responding";
		char buff[20];
		int clRet1;
		struct sockaddr_in lpSockAddr;
		fd_set readfds;//, writefds, exceptfds;
		struct timeval timeout;  
		timeout.tv_sec = 1;
		timeout.tv_usec = 0;
		int size = sizeof(struct sockaddr), x;
		int optval = 1;
		
		m_quit = 1;
		
		DoNothing = FALSE;
		
		argv[0] = "";
		argv[1] = "100.200.200.1";
		argv[2] = "20000";
		argv[3] = "255.255.255.255";
		
		port = atol(argv[2]);
		stRmtAddr.sin_family               =PF_INET;
		stRmtAddr.sin_port = htons(port);
		UserAddr = strdup(argv[3]);
		stRmtAddr.sin_addr.S_un.S_addr = (inet_addr(UserAddr));
		
		//Fill out the local address data
		lpSockAddr.sin_family = PF_INET;  
		lpSockAddr.sin_port = htons (port);    
		lpSockAddr.sin_addr.s_addr = htonl (INADDR_ANY);
		
		
		if (dlg1.DoModal() == IDCANCEL)		
		{
			DoNothing = TRUE;
		}
		
		else 
		{
begin:  //loop around
		
		if(ChoiceIP == 1)
		{
			//////////////////////////////////////////////////////////////////  
			/* Automatic Detect using UDP Socket */
			//////////////////////////////////////////////////////////////////
			
			udpscomm = socket(PF_INET, SOCK_DGRAM, 0);
			if (udpscomm == INVALID_SOCKET)
				TRACE("UDP Unable to get socket - Error \n");
			else
				TRACE("UDP connection Socket registered\n");
			
			n = setsockopt(udpscomm, SOL_SOCKET, SO_BROADCAST, (char *)&optval, sizeof(optval));
			
			nBytes_rx = 0;
			
			while (i<5 && nBytes_rx == 0)
			{
				nBytes_tx = sendto(udpscomm, send_msg, strlen(send_msg), 0, 
					(struct sockaddr *)&stRmtAddr, sizeof(struct sockaddr));  
				if(nBytes_tx > 0)
					TRACE("Send to UDP Comm Sock Succeeds \n");
				((CMainFrame *)AfxGetMainWnd())->ShowText("Trying to Connect to Socket....");
				FD_ZERO(&readfds); /* initialize */
				FD_SET(udpscomm, &readfds);
				n = select(udpscomm, &readfds, 0, 0, &timeout); 
				if ((n != SOCKET_ERROR) && (n != 0))
				{
					nBytes_rx = recvfrom(udpscomm, buff, sizeof(send_msg), 0, 
						(struct sockaddr *)&lpSockAddr, (int *) &size);
					
					x = WSAGetLastError();
				}
				i++;
			}
			
			
			if (( i == 5 ) || (nBytes_rx == 0))
			{
				UserAddr = strdup(argv[1]);
				stRmtAddr.sin_addr.S_un.S_addr = (inet_addr(UserAddr));
				((CMainFrame *)AfxGetMainWnd())->ShowText("Auto Detect Failed");
				if (dlg3.DoModal() == IDCANCEL)
				{
					((CMainFrame *)AfxGetMainWnd())->PostMessage(WM_CLOSE);
                    DoNothing = TRUE;
				}
				else
				{
					if (dlg2.DoModal() == IDCANCEL)
					{
						DoNothing = TRUE;
					}		
				}
			}
			else
			{
				stRmtAddr.sin_addr.S_un.S_addr = lpSockAddr.sin_addr.S_un.S_addr;
				((CMainFrame *)AfxGetMainWnd())->ShowText("Auto Detect Succeded\n");
			}
			
			
			clRet1 = closesocket(udpscomm);
		}
		else
		{
			//////////////////////////////////////////////
			//User Enters IP address
			//////////////////////////////////////////////
			if (dlg2.DoModal() == IDCANCEL)
			{
				DoNothing = TRUE;
			}
		}
		}
		////////////////////////////////////////////////////////////////
		/* TCP Connection for Starting Clients */
		////////////////////////////////////////////////////////////////
		
		if (DoNothing != TRUE)
		{
			tcpcomms = socket(PF_INET, SOCK_STREAM, 0);    /*get a stream or tcp socket */
			if (tcpcomms == INVALID_SOCKET)
				TRACE("TCP Unable to get socket - Error \n");
			else
				TRACE("TCP connection Socket registered\n");
			
			cRet = connect(tcpcomms,                              /*connect to server */
				(struct sockaddr *)&stRmtAddr,
				sizeof(struct sockaddr));    
			((CMainFrame *)AfxGetMainWnd())->ShowText("Trying to Connect to Socket...."); 
			if (cRet == SOCKET_ERROR)
			{
    			((CMainFrame *)AfxGetMainWnd())->ShowText(""); /* Dont show anything on status bar */
				AfxMessageBox("Unable to connect socket- Error. \n");   
				ChoiceIP = 2;
				goto begin; 
			}
			else
			{
				((CMainFrame *)AfxGetMainWnd())->ShowText("Socket Connected"); 
			}
			
			
			m_bTcpcliStarted    = FALSE;
			m_bTcpsrvStarted    = FALSE;
			m_bUdpcliStarted    = FALSE;
			m_bUdpsrvStarted    = FALSE;
			m_bTftpStarted      = FALSE;

			CWinThread* pThread = AfxBeginThread(CommErrRecv, THREAD_PRIORITY_NORMAL, 0, 0, NULL);
			hStopEvent = ::CreateEvent(NULL, FALSE, FALSE, NULL);
			pDocTemplate5->OpenDocumentFile(NULL);
		}
	}
}


void CWinDemoApp::OnUpdateFileDetecthost(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck(!DoNothing);
	pCmdUI->Enable(TRUE);
}

void CWinDemoApp::OnDemoTftpclient() 
{
    if (!m_bTftpStarted)
    {
        pDocTemplate6->OpenDocumentFile(NULL);
        m_bTftpStarted = TRUE;
    } 
}

void CWinDemoApp::OnUpdateDemoTftpclient(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck(m_bTftpStarted);
    pCmdUI->Enable(TRUE);
	
}

⌨️ 快捷键说明

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